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.
Added a new Field struct which provides the ability to search through unstructed data (such as the data returned from a json call), and easily add or remove values to this data.
Related to #321.
Overview
Current State
Wrangler provides a
data
package which provides an easy way to manipulate values ofmap[string]interface{}
. This proviedes functionality to:This package requires that the path to the value be given as a list of strings, like below:
Similar functionality can also be used with
PutValue
andGetValue
to insert and get values from a map.Issues with the Current State
The current state has several issues:
There's another PR open which attempts to supplement these issues (#321), but there are a few limitations to the PR, namely:
Fixing some of these issues (like the first and second bullet point) would likely break backward compatibility, so there's only so much that can be done with the current framework.
Changes
This PR introduces a new struct and 3 new functions (
GetField
,PutField
,RemoveField
) which aim to mirror the current functionality while providing documentation, testing, and more exact typing.New Struct
The new struct is a
Field
. This represents the path in an unstructured map to a resource that users want to Get, Remove, or update. The Field has an immediate path (represented by theName
which is set for map entries andListIndex
which is set for list entries) and aSubField
(which indicates that the value at this path is a map or slice, and there are further entries in that value).Complex paths can be constructed by chaining calls to the
Child
andIndex
functions, which will setup the field for the user:New Methods
3 New methods have been added, that aim to behave the same as the existing methods with a few enhancements (namely, they use the new field type and support the use of
[]any
).GetField
returns the value for a given field (or an error if the field/data was invalid, or the field was not found).RemoveField
removes a given field (key + value for maps, just the entry for slices) and returns the new object, the removed value, and an error (only if the field/data was invalid or the field was not found).PutField
puts a value into a given field and returns the new object and an error (only if the field/data was invalid or the field was not found). Will recursively create missing entries.