bouncer/internal/schema/extend.go

37 lines
731 B
Go
Raw Permalink Normal View History

package schema
import (
"encoding/json"
"github.com/pkg/errors"
)
func Extend(base []byte, schema []byte) ([]byte, error) {
var (
extension map[string]any
extended map[string]any
)
if err := json.Unmarshal(base, &extended); err != nil {
return nil, errors.WithStack(err)
}
if err := json.Unmarshal(schema, &extension); err != nil {
return nil, errors.WithStack(err)
}
props := extension["properties"].(map[string]any)
extendedProps := extended["properties"].(map[string]any)
for key, val := range props {
extendedProps[key] = val
}
extended["properties"] = extendedProps
data, err := json.MarshalIndent(extended, "", " ")
if err != nil {
return nil, errors.WithStack(err)
}
return data, nil
}