Files
zeekayandHanzo Dev a5c5cc019e Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-16 22:42:15 -07:00

109 lines
2.7 KiB
Go

// SPDX-License-Identifier: Apache-2.0
/*
Package encoding provides basic decoding implementations.
Decode decodes HTTP responses:
resp, _ := http.Get("http://api.example.com/")
...
var data map[string]interface{}
err := JSONDecoder(resp.Body, &data)
*/
package encoding
import (
"encoding/json"
"io"
)
// Decoder is a function that reads from the reader and decodes it
// into an map of interfaces
type Decoder func(io.Reader, *map[string]any) error
// DecoderFactory is a function that returns CollectionDecoder or an EntityDecoder
type DecoderFactory func(bool) func(io.Reader, *map[string]any) error
// NOOP is the key for the NoOp encoding
const NOOP = "no-op"
// NoOpDecoder is a decoder that does nothing
func NoOpDecoder(_ io.Reader, _ *map[string]any) error { return nil }
func noOpDecoderFactory(_ bool) func(io.Reader, *map[string]any) error { return NoOpDecoder }
// JSON is the key for the json encoding
const JSON = "json"
// NewJSONDecoder returns the right JSON decoder
func NewJSONDecoder(isCollection bool) func(io.Reader, *map[string]any) error {
if isCollection {
return JSONCollectionDecoder
}
return JSONDecoder
}
// JSONDecoder decodes a json message into a map
func JSONDecoder(r io.Reader, v *map[string]any) error {
d := json.NewDecoder(r)
d.UseNumber()
return d.Decode(v)
}
// JSONCollectionDecoder decodes a json collection and returns a map with the array at the 'collection' key
func JSONCollectionDecoder(r io.Reader, v *map[string]any) error {
var collection []any
d := json.NewDecoder(r)
d.UseNumber()
if err := d.Decode(&collection); err != nil {
return err
}
*(v) = map[string]any{"collection": collection}
return nil
}
// SAFE_JSON is the key for the json encoding
const SAFE_JSON = "safejson"
// NewSafeJSONDecoder returns the universal json decoder
func NewSafeJSONDecoder(_ bool) func(io.Reader, *map[string]any) error {
return SafeJSONDecoder
}
// SafeJSONDecoder decodes both json objects and collections
func SafeJSONDecoder(r io.Reader, v *map[string]any) error {
d := json.NewDecoder(r)
d.UseNumber()
var t any
if err := d.Decode(&t); err != nil {
return err
}
switch tt := t.(type) {
case map[string]any:
*v = tt
case []any:
*v = map[string]any{"collection": tt}
default:
*v = map[string]any{"content": tt}
}
return nil
}
// STRING is the key for the string encoding
const STRING = "string"
// NewStringDecoder returns a String decoder
func NewStringDecoder(_ bool) func(io.Reader, *map[string]any) error {
return StringDecoder
}
// StringDecoder returns a map with the content of the reader under the key 'content'
func StringDecoder(r io.Reader, v *map[string]any) error {
data, err := io.ReadAll(r)
if err != nil {
return err
}
*(v) = map[string]any{"content": string(data)}
return nil
}