Documentation
¶
Overview ¶
Package mapsutil provides small map-copy helpers used across internal packages.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneOrEmpty ¶
CloneOrEmpty returns a copy of string values, or an initialized empty map for nil input.
Use when the result must always be a non-nil map, for example when the copy is serialized as a JSON object (an empty map renders as `{}` while nil renders as `null`) or when callers may write into it later.
Example ¶
CloneOrEmpty always yields a non-nil map, so nil input still serializes as a JSON object instead of null.
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/omarluq/librecode/internal/mapsutil"
)
// mustPrintln writes example output to stdout for godoc, panicking on write
// errors so example bodies stay focused on the behavior under demonstration.
func mustPrintln(values ...any) {
if _, err := fmt.Fprintln(os.Stdout, values...); err != nil {
panic(err)
}
}
func main() {
serialized, err := json.Marshal(mapsutil.CloneOrEmpty(map[string]int(nil)))
if err != nil {
mustPrintln("error:", err)
return
}
mustPrintln(string(serialized))
}
Output: {}
func CloneOrNil ¶
CloneOrNil returns a copy of string values, or nil for nil or empty input.
Use when nil and empty mean the same thing and nil is preferred, for example before storing the copy in a struct field with `omitempty` JSON tags so empty inputs stay out of serialized output.
Example ¶
CloneOrNil collapses nil and empty input to nil, keeping `omitempty` fields out of serialized output.
package main
import (
"fmt"
"os"
"github.com/omarluq/librecode/internal/mapsutil"
)
// mustPrintln writes example output to stdout for godoc, panicking on write
// errors so example bodies stay focused on the behavior under demonstration.
func mustPrintln(values ...any) {
if _, err := fmt.Fprintln(os.Stdout, values...); err != nil {
panic(err)
}
}
func main() {
empty := map[string]string{}
mustPrintln(mapsutil.CloneOrNil(map[string]string(nil)) == nil)
mustPrintln(mapsutil.CloneOrNil(empty) == nil)
mustPrintln(mapsutil.CloneOrNil(map[string]string{"k": "v"}))
}
Output: true true map[k:v]
func ClonePreserveNil ¶
ClonePreserveNil returns a copy of string values, or nil for nil input.
Use when the copy must distinguish "unset" (nil) from "set but empty", for example when copying optional request metadata whose absence carries meaning or when merging only if the source was configured.
Example ¶
ClonePreserveNil keeps the nil/empty distinction when copying optional maps.
package main
import (
"fmt"
"os"
"github.com/omarluq/librecode/internal/mapsutil"
)
// mustPrintln writes example output to stdout for godoc, panicking on write
// errors so example bodies stay focused on the behavior under demonstration.
func mustPrintln(values ...any) {
if _, err := fmt.Fprintln(os.Stdout, values...); err != nil {
panic(err)
}
}
func main() {
var unset map[string]string
empty := map[string]string{}
mustPrintln(mapsutil.ClonePreserveNil(unset) == nil)
mustPrintln(mapsutil.ClonePreserveNil(empty) == nil)
}
Output: true false
Types ¶
This section is empty.