-
Notifications
You must be signed in to change notification settings - Fork 229
Implement IgnoreExtraFieldsFromTextual
#303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rockwotj
merged 4 commits into
linkedin:master
from
passuied:feature/ignore-additional-fields
Jan 14, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| // Copyright [2019] LinkedIn Corp. Licensed under the Apache License, Version | ||
| // 2.0 (the "License"); you may not use this file except in compliance with the | ||
| // License. You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
|
|
||
| package goavro | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| ) | ||
|
|
||
| // skipJSONValue advances the buffer past a single JSON value (object, array, string, number, bool, null). | ||
| // This is used when IgnoreExtraFieldsFromTextual is enabled to skip over values of unknown fields. | ||
| func skipJSONValue(buf []byte) ([]byte, error) { | ||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
|
|
||
| switch buf[0] { | ||
| case '{': | ||
| // Skip object: find matching closing brace | ||
| return skipJSONObject(buf) | ||
| case '[': | ||
| // Skip array: find matching closing bracket | ||
| return skipJSONArray(buf) | ||
| case '"': | ||
| // Skip string: find closing quote (handling escapes) | ||
| return skipJSONString(buf) | ||
| case 't': | ||
| // true | ||
| if len(buf) >= 4 && string(buf[:4]) == "true" { | ||
| return buf[4:], nil | ||
| } | ||
| return nil, fmt.Errorf("cannot skip JSON value: invalid literal starting with 't'") | ||
| case 'f': | ||
| // false | ||
| if len(buf) >= 5 && string(buf[:5]) == "false" { | ||
| return buf[5:], nil | ||
| } | ||
| return nil, fmt.Errorf("cannot skip JSON value: invalid literal starting with 'f'") | ||
| case 'n': | ||
| // null | ||
| if len(buf) >= 4 && string(buf[:4]) == "null" { | ||
| return buf[4:], nil | ||
| } | ||
| return nil, fmt.Errorf("cannot skip JSON value: invalid literal starting with 'n'") | ||
| default: | ||
| // Must be a number (or invalid) | ||
| if buf[0] == '-' || (buf[0] >= '0' && buf[0] <= '9') { | ||
| return skipJSONNumber(buf) | ||
| } | ||
| return nil, fmt.Errorf("cannot skip JSON value: unexpected character: %q", buf[0]) | ||
| } | ||
| } | ||
|
|
||
| // skipJSONObject skips a JSON object starting with '{' and returns the buffer after the closing '}'. | ||
| func skipJSONObject(buf []byte) ([]byte, error) { | ||
| if len(buf) == 0 || buf[0] != '{' { | ||
| return nil, fmt.Errorf("cannot skip JSON object: expected '{'") | ||
| } | ||
| buf = buf[1:] // consume '{' | ||
| var err error | ||
|
|
||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
|
|
||
| // Handle empty object | ||
| if buf[0] == '}' { | ||
| return buf[1:], nil | ||
| } | ||
|
|
||
| for len(buf) > 0 { | ||
| // Skip key (string) | ||
| if buf, err = skipJSONString(buf); err != nil { | ||
| return nil, err | ||
| } | ||
| // Skip colon | ||
| if buf, err = advanceAndConsume(buf, ':'); err != nil { | ||
| return nil, err | ||
| } | ||
| // Skip value | ||
| if buf, err = skipJSONValue(buf); err != nil { | ||
| return nil, err | ||
| } | ||
| // Check for comma or closing brace | ||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| switch buf[0] { | ||
| case '}': | ||
| return buf[1:], nil | ||
| case ',': | ||
| buf = buf[1:] | ||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| default: | ||
| return nil, fmt.Errorf("cannot skip JSON object: expected ',' or '}'; received: %q", buf[0]) | ||
| } | ||
| } | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
|
|
||
| // skipJSONArray skips a JSON array starting with '[' and returns the buffer after the closing ']'. | ||
| func skipJSONArray(buf []byte) ([]byte, error) { | ||
| if len(buf) == 0 || buf[0] != '[' { | ||
| return nil, fmt.Errorf("cannot skip JSON array: expected '['") | ||
| } | ||
| buf = buf[1:] // consume '[' | ||
| var err error | ||
|
|
||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
|
|
||
| // Handle empty array | ||
| if buf[0] == ']' { | ||
| return buf[1:], nil | ||
| } | ||
|
|
||
| for len(buf) > 0 { | ||
| // Skip value | ||
| if buf, err = skipJSONValue(buf); err != nil { | ||
| return nil, err | ||
| } | ||
| // Check for comma or closing bracket | ||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| switch buf[0] { | ||
| case ']': | ||
| return buf[1:], nil | ||
| case ',': | ||
| buf = buf[1:] | ||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| default: | ||
| return nil, fmt.Errorf("cannot skip JSON array: expected ',' or ']'; received: %q", buf[0]) | ||
| } | ||
| } | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
|
|
||
| // skipJSONString skips a JSON string starting with '"' and returns the buffer after the closing '"'. | ||
| func skipJSONString(buf []byte) ([]byte, error) { | ||
| if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| if buf[0] != '"' { | ||
| return nil, fmt.Errorf("cannot skip JSON string: expected '\"'") | ||
| } | ||
| buf = buf[1:] // consume opening quote | ||
|
|
||
| for i := 0; i < len(buf); i++ { | ||
| switch buf[i] { | ||
| case '\\': | ||
| // Skip the next character (escaped) | ||
| i++ | ||
| case '"': | ||
| // Found closing quote | ||
| return buf[i+1:], nil | ||
| } | ||
| } | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
|
|
||
| // skipJSONNumber skips a JSON number and returns the buffer after the number. | ||
| func skipJSONNumber(buf []byte) ([]byte, error) { | ||
| if len(buf) == 0 { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
|
|
||
| i := 0 | ||
|
|
||
| // Optional minus sign | ||
| if buf[i] == '-' { | ||
| i++ | ||
| if i >= len(buf) { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| } | ||
|
|
||
| // Integer part | ||
| switch { | ||
| case buf[i] == '0': | ||
| i++ | ||
| case buf[i] >= '1' && buf[i] <= '9': | ||
| for i < len(buf) && buf[i] >= '0' && buf[i] <= '9' { | ||
| i++ | ||
| } | ||
| default: | ||
| return nil, fmt.Errorf("cannot skip JSON number: invalid character: %q", buf[i]) | ||
| } | ||
|
|
||
| // Optional fractional part | ||
| if i < len(buf) && buf[i] == '.' { | ||
| i++ | ||
| if i >= len(buf) || buf[i] < '0' || buf[i] > '9' { | ||
| return nil, fmt.Errorf("cannot skip JSON number: expected digit after decimal point") | ||
| } | ||
| for i < len(buf) && buf[i] >= '0' && buf[i] <= '9' { | ||
| i++ | ||
| } | ||
| } | ||
|
|
||
| // Optional exponent part | ||
| if i < len(buf) && (buf[i] == 'e' || buf[i] == 'E') { | ||
| i++ | ||
| if i >= len(buf) { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| if buf[i] == '+' || buf[i] == '-' { | ||
| i++ | ||
| if i >= len(buf) { | ||
| return nil, io.ErrShortBuffer | ||
| } | ||
| } | ||
| if buf[i] < '0' || buf[i] > '9' { | ||
| return nil, fmt.Errorf("cannot skip JSON number: expected digit in exponent") | ||
| } | ||
| for i < len(buf) && buf[i] >= '0' && buf[i] <= '9' { | ||
| i++ | ||
| } | ||
| } | ||
|
|
||
| return buf[i:], nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's OK, but this doesn't validate the string