Title: JSON errors: line numbers · Issue #24 · function61/gokit · GitHub
Open Graph Title: JSON errors: line numbers · Issue #24 · function61/gokit
X Title: JSON errors: line numbers · Issue #24 · function61/gokit
Description: Playground code: package main import ( "encoding/json" "fmt" "io" "strings" ) // (ab)using the fact that the JSON decoder has accumulated over all it's read plus it's working on the new buffer // it's about to receive now. type lineCount...
Open Graph Description: Playground code: package main import ( "encoding/json" "fmt" "io" "strings" ) // (ab)using the fact that the JSON decoder has accumulated over all it's read plus it's working on the new buffer // i...
X Description: Playground code: package main import ( "encoding/json" "fmt" "io" "strings" ) // (ab)using the fact that the JSON decoder has accumulated over all it's r...
Opengraph URL: https://github.com/function61/gokit/issues/24
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JSON errors: line numbers","articleBody":"Playground code:\r\n\r\n```go\r\npackage main\r\n\r\nimport (\r\n\t\"encoding/json\"\r\n\t\"fmt\"\r\n\t\"io\"\r\n\t\"strings\"\r\n)\r\n\r\n// (ab)using the fact that the JSON decoder has accumulated over all it's read plus it's working on the new buffer\r\n// it's about to receive now.\r\ntype lineCounter struct {\r\n\tcommittedLines int\r\n\tcommittedBytes int64\r\n\r\n\tuncommitted []byte\r\n\tuncommittedLines int\r\n}\r\n\r\nfunc (l *lineCounter) LineNumberFromOffset(offset int64) int {\r\n\tif offset \u003c l.committedBytes {\r\n\t\tpanic(\"offset \u003c l.committedBytes\")\r\n\t}\r\n\r\n\tintoUncommitted := offset - l.committedBytes\r\n\r\n\tlinesIntoUncommitted := countNewlines(l.uncommitted[:intoUncommitted])\r\n\r\n\treturn l.committedLines + linesIntoUncommitted\r\n}\r\n\r\nvar _ io.Writer = (*lineCounter)(nil)\r\n\r\nfunc (l *lineCounter) Write(buf []byte) (int, error) {\r\n\t// the fact that `Write()` was called means that the previous uncommitted buffer was consumed fully. \"commit\" now.\r\n\tl.committedBytes += int64(len(l.uncommitted))\r\n\tl.committedLines += l.uncommittedLines\r\n\r\n\tl.uncommittedLines = countNewlines(buf)\r\n\tl.uncommitted = buf\r\n\r\n\treturn len(buf), nil\r\n}\r\n\r\nfunc main() {\r\n\tlc := \u0026lineCounter{}\r\n\r\n\tjd := json.NewDecoder(io.TeeReader(strings.NewReader(`{\r\n\r\n\r\n\t\"Foo\": \"bar\"}`), lc))\r\n\terr := jd.Decode(\u0026struct{ Foo struct{} }{})\r\n\tif err != nil {\r\n\t\tfmt.Printf(\"err=%v pos=%d\\n\", err, lc.LineNumberFromOffset(jd.InputOffset()))\r\n\t}\r\n\r\n\tfmt.Println(\"Hello, 世界\")\r\n}\r\n\r\nfunc countNewlines(buf []byte) int {\r\n\tnewlines := 0\r\n\r\n\tfor _, ch := range buf {\r\n\t\tif ch == '\\n' {\r\n\t\t\tnewlines++\r\n\t\t}\r\n\t}\r\n\r\n\treturn newlines\r\n}\r\n\r\n```","author":{"url":"https://github.com/joonas-fi","@type":"Person","name":"joonas-fi"},"datePublished":"2023-09-11T12:21:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/24/gokit/issues/24"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f68bfa3d-3b21-1617-477d-3ca8e90c5862 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD66:2608B0:D38DC:13126E:6A4BBD6D |
| html-safe-nonce | 523da09d230f939e8c85f9122abf19e2d8572475b59261a09ef568e60e822ad3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDY2OjI2MDhCMDpEMzhEQzoxMzEyNkU6NkE0QkJENkQiLCJ2aXNpdG9yX2lkIjoiMjIxODkwODEyMjY5MjE3MzE2NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 94b62439d2adec68ce746ff9b6a63d07a189a5a17d6baaa58bb8b1e19d4f76fd |
| hovercard-subject-tag | issue:1890389195 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/function61/gokit/24/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ac2d56ec49f6ded54e341bde36eecab760d90baba028f1a6fed55883cebbf179/function61/gokit/issues/24 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ac2d56ec49f6ded54e341bde36eecab760d90baba028f1a6fed55883cebbf179/function61/gokit/issues/24 |
| og:image:alt | Playground code: package main import ( "encoding/json" "fmt" "io" "strings" ) // (ab)using the fact that the JSON decoder has accumulated over all it's read plus it's working on the new buffer // i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | joonas-fi |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae1c14875555a1dd92bdef45baa6dd9aa796df891e4e471989f7117fe3139fc1 |
| turbo-cache-control | no-preview |
| go-import | github.com/function61/gokit git https://github.com/function61/gokit.git |
| octolytics-dimension-user_id | 22049800 |
| octolytics-dimension-user_login | function61 |
| octolytics-dimension-repository_id | 151857198 |
| octolytics-dimension-repository_nwo | function61/gokit |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 151857198 |
| octolytics-dimension-repository_network_root_nwo | function61/gokit |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b15c57117aeff522995a9578e1185f6c45d6c562 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width