Repository navigation
feat: add JSON parser implementation with switchable libraries - #809
Conversation
|
converting back to draft, I have some optimisations that I would try before requesting review again |
There was a problem hiding this comment.
Is the validation really necessary or should we leave this responsibility to the devs? Imagine having the same main JSON document and from the same data []byte you might want to get multiple values:
var data[]byte
name := jp.GetString(data, "firstName") // it will validate data
surname := jp.GetString(data, "lastName") // it will validate data again1There was a problem hiding this comment.
I'm thinking, instead of just getting, setting and deleting, perhaps this pacakge should expose also a validate function. It doesn't matter if not all libs support it. If you set the grafana one and it doesn't have it (just saying I haven't verified), under the hood it can simply fallback to tidwall which supports validations.
There was a problem hiding this comment.
we have validation support in jsonrs maybe we can add there. wdyt?
There was a problem hiding this comment.
Yeah it's cool if either one of the two libs have Valid([]byte) then we have a way to do it 👍
There was a problem hiding this comment.
@mihir20 can we have a Valid function then? I know jsonparser doesn't have it but we can use the tidwall one always, even if the user selected jsonparser as the preferred library.
fb894c7 to
42eb852
Compare
e9ccf4e to
a4d334c
Compare
- Introduce common error variables for consistent error reporting- Enhance error messages with more context for easier debugging - Refactor error checks to use new error variables - Update function comments to include error handling information
- Add new error types for specific parsing issues
- Refactor GetValue methods to use any instead of interface{}
- Remove unnecessary unmarshalling for complex types
- Update tests to reflect new error handling
- Set GrafanaLib as the default JSON parser
…dling - Simplify GetValue function signature to return []byte instead of any - Remove type conversion logic from grafanaJSONParser and tidwallJSONParser - Update jsonparser interface to reflect new []byte return type- Improve documentation for getter interface methods
- Modify tests to expect []byte instead of string/float64/bool(nil) results - Update Tidwall getter implementation to return []byte for string values- Adjust test cases to cover new byte slice return type for all value types
- Update GetValue to return raw string values including quotes - Add dtype parameter to identify string type in grafana.go - Update jsonparser.go with more detailed function comments - Adjust tests to reflect new string value handling - Remove unnecessary string handling in tidwall.go
- Add new SafeGetter interface with methods like GetValueOrEmpty and GetBooleanOrFalse - Implement SafeGetter methods in grafanaJSONParser and tidwallJSONParser - Update switcher to use SafeGetter - Rename 'keys' parameter to 'path' in all getter methods for consistency
…ndling - Add support for string values in soft getter methods - Implement GetValueOrEmpty, GetBooleanOrFalse, GetIntOrZero, GetFloatOrZero, and GetStringOrEmpty methods - Update tests to cover new soft getter functionality - Modify Switcher to support configurable soft getter library
…nt data types - Update GetValueOrEmpty to return raw value instead of string - Refactor GetBoolean, GetInt, GetFloat, and GetString to use new parsing functions - Add new functions to parse bool, int, float, and string values from raw data - Remove unnecessary type checks in Tidwall parser
- Add support for parsing string boolean values in Grafana library - Optimize boolean, integer and float parsing for both libraries - Update benchmarks to include new test cases - Improve test coverage for GetBooleanOrFalse, GetIntOrZero, GetFloatOrZero and GetStringOrEmpty methods
- Update comments for GetBooleanOrFalse, GetIntOrZero, GetFloatOrZero, and GetStringOrEmpty methods - Add examples to illustrate behavior with different input types - Clarify what is considered "parsable" for each method - Update documentation in both grafana.go and jsonparser.go files
…g keys - Add tests to check behavior when empty byte slice is provided - Add tests to check behavior when no key is provided - Cover edge cases for all data types (value, boolean, int, float, string)
- Add checks for empty path and keys in various JSON parsing functions - Return appropriate error messages for empty path and keys - Update tests to cover new validation cases - Refactor tidwallJSONParser to use path instead of keys
|
|
||
| // If the value is a string, wrap it in quotes as the library is specifically stripping quotes from string value | ||
| if dtype == jsonparser.String { | ||
| return []byte("\"" + string(value) + "\"") |
There was a problem hiding this comment.
do we need to escape doublequotes in the string?
There was a problem hiding this comment.
raw value would have quotes
fracasula
left a comment
There was a problem hiding this comment.
It looks pretty good, looking forward to use it. Well done 👍
- Add import for github.com/samber/lo package- Update error messages to be more informative - Refactor code to use more descriptive variable names - Improve comments and documentation - Simplify some conditional checks
…ions - Improve path construction logic to handle empty keys and array indices
…ON bytes- Update test cases to use raw string literals (`...`) instead of interpreted string literals ("...") for JSON bytes- This change improves code readability and eliminates the need for escape characters
Description
add JSON parser implementation with switchable libraries
Linear Ticket
Security