CopyFromString for ast.Node
Is your feature request related to a problem? Please describe.
I have seen that using the searcher API or the top level sonic API you can get a copied node in order to not reference the entire JSON at all times.
But once you have an ast.Node you may want to query it's fields nested or not, and also get copied values.
someNode, _ := sonic.GetCopyFromString(jsonDocument, "path", "to", "node")
// This will hold a reference to the same string buffer as someNode
fieldString, _ := someNode.Get("field").String()
// This will also hold a reference to the same string buffer as someNode
nestedString, _ := someNode.GetByPath("path", "to", "nested").String()This means that if I want to get copied fields from someNode I either have to walk the entire path for each field using GetCopyFromString or I have to clone all strings myself (this would mean also cloning redundant strings, because if the field value is int, bool, float, etc. the string converted value is not a reference to the buffer anymore).
Maybe I have missed a way to do this using the ast.Node, otherwise it seems like an oversight.
Describe the solution you'd like
A method on ast.Node that could be func (self *Node) CopyString() (string, error) which would return a copy in case the underlying type is string instead of a pointer in the ast.Node buffer.
Describe alternatives you've considered
Right now the two alternatives as far as I have experimented with the API are either:
A) calling GetCopyFromString redudantly on the entire path each time:
n1, _ := sonic.GetCopyFromString(jsonDocument, "path", "to", "node", "field")and thenn1.String()n2, _ := sonic.GetCopyFromString(jsonDocument, "path", "to", "node", "path", "to", "nested").String()and thenn2.String()
B) manually cloning each string:
someNode, _ := sonic.GetCopyFromString(jsonDocument, "path", "to", "node")
fieldString, _ := someNode.Get("field").String()
nestedString, _ := someNode.GetByPath("path", "to", "nested").String()
// This copying is also redundant if the actual value was int, float, bool, etc
// because in conversions a string copy is created, but for some reason if the
// underlying value is string it's just referenced to it by a pointer,
// preventing the node from ever being garbage collected if you cache the string values
cachedFieldString := strings.Clone(fieldString)
cachedNestedString := strings.Clone(nestedString)Additional context
If there is another way to get copied strings from an ast.Node itself already maybe it could be added to the documentation as well, but I couldn't find one.
Source: bytedance/sonic