Documentation
¶
Overview ¶
Package deep copies and searches nested data: the maps and slices that a JSON or database decoder returns, at every level of nesting.
Copy returns a copy that shares no map or slice with the original, so that a caller can change one without changing the other. Every container in the copy keeps its own type, and the copy is returned as the type that Copy was given.
Remove deletes the named properties from every map within a value, in place. RemoveFunc does the same for the properties that a Matcher selects, such as HasPrefix. To leave the original untouched, Copy it first and Remove from the copy.
Both functions list the common types for speed, and reach every other map, slice, and array by reflection. A value that neither can walk, such as a function or a struct that holds a map, is an error, unless it implements Copier or Remover.
Both stop with an error at 10,000 levels of nesting, which is what stops a map or slice that contains itself.
Index ¶
- func Copy[T any](original T) (T, error)
- func CopyWithDepth[T any](original T, depth int) (T, error)
- func Remove(value any, propertyNames ...string) error
- func RemoveFunc(value any, match Matcher) error
- func RemoveWithDepth(value any, depth int, match Matcher) error
- type Copier
- type Matcher
- type Remover
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Copy ¶
Copy returns a deep copy of a value, which shares no map or slice with the original. A value that it cannot copy, such as a function or a struct that holds a map, is an error.
Example ¶
ExampleCopy copies a document, then changes the copy without changing the original
original := map[string]any{
"type": "Note",
"to": []any{"alice"},
}
duplicate, err := Copy(original)
if err != nil {
fmt.Println(err)
return
}
duplicate["to"].([]any)[0] = "bob"
fmt.Println(original["to"])
fmt.Println(duplicate["to"])
Output: [alice] [bob]
func CopyWithDepth ¶
CopyWithDepth continues a copy that is already under way, at the given level of nesting. It is for a Copier, which passes its own depth plus one for each value that it holds.
func Remove ¶
Remove deletes the named properties from every map within a value, changing it in place. A value that it cannot search, such as a function or a struct that holds a map, is an error.
Example ¶
ExampleRemove deletes two property names from every level of a document
document := map[string]any{
"type": "Create",
"bcc": []any{"carol"},
"object": map[string]any{
"type": "Note",
"bto": "dave",
},
}
if err := Remove(document, "bto", "bcc"); err != nil {
fmt.Println(err)
return
}
fmt.Println(document)
Output: map[object:map[type:Note] type:Create]
func RemoveFunc ¶
RemoveFunc deletes every property that the Matcher selects from every map within a value, changing it in place. It returns the same errors as Remove, and one for a nil Matcher.
Example ¶
ExampleRemoveFunc deletes every property in a reserved namespace
document := map[string]any{
"type": "Note",
"emissary:token": "secret",
"tag": []any{map[string]any{"name": "go", "x-internal": true}},
}
if err := RemoveFunc(document, HasPrefix("emissary:", "x-")); err != nil {
fmt.Println(err)
return
}
fmt.Println(document)
Output: map[tag:[map[name:go]] type:Note]
Types ¶
type Copier ¶
Copier is implemented by a value that returns a deep copy of itself. It copies what it holds with CopyWithDepth and depth+1.
type Matcher ¶
Matcher returns TRUE for the name of a property that Remove should delete
func AnyOf ¶
AnyOf returns a Matcher for properties that at least one of the given Matchers selects. A nil Matcher is ignored.
Example ¶
ExampleAnyOf combines the Matchers that come with the package with one of the caller's own
match := AnyOf(
Named("bto", "bcc"),
HasPrefix("x-"),
func(name string) bool { return strings.HasSuffix(name, "_private") },
)
fmt.Println(match("bcc"), match("x-debug"), match("notes_private"), match("to"))
Output: true true true false