mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
loader: allow extensions (experimental) (#5940)
This extension to the loader package allows experimenting with data formats that are not JSON, but pretend to be. Signed-off-by: Stephan Renatus <stephan@styra.com> Co-authored-by: kevinstyra <83973046+kevinstyra@users.noreply.github.com>
This commit is contained in:
+18
-5
@@ -12,6 +12,8 @@ import (
|
||||
"reflect"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
"github.com/open-policy-agent/opa/loader/extension"
|
||||
)
|
||||
|
||||
// UnmarshalJSON parses the JSON encoded data and stores the result in the value
|
||||
@@ -103,14 +105,25 @@ func Reference(x interface{}) *interface{} {
|
||||
return &x
|
||||
}
|
||||
|
||||
// Unmarshal decodes a YAML or JSON value into the specified type.
|
||||
// Unmarshal decodes a YAML, JSON or JSON extension value into the specified type.
|
||||
func Unmarshal(bs []byte, v interface{}) error {
|
||||
if json.Valid(bs) {
|
||||
return UnmarshalJSON(bs, v)
|
||||
}
|
||||
bs, err := yaml.YAMLToJSON(bs)
|
||||
if err != nil {
|
||||
return err
|
||||
nbs, err := yaml.YAMLToJSON(bs)
|
||||
if err == nil {
|
||||
return UnmarshalJSON(nbs, v)
|
||||
}
|
||||
return UnmarshalJSON(bs, v)
|
||||
// not json or yaml: try extensions
|
||||
if value, ok := v.(*any); ok {
|
||||
if handler := extension.FindExtension(".json"); handler != nil {
|
||||
retval, err := handler(bs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*value = retval
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user