big tings
This commit is contained in:
32
parser/decoders/yaml.go
Normal file
32
parser/decoders/yaml.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package decoders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.max-richter.dev/max/marka/parser/utils"
|
||||
"git.max-richter.dev/max/marka/template"
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
func Yaml(input string, block template.Block) (value any, error error) {
|
||||
res := make(map[string]any)
|
||||
err := yaml.Unmarshal([]byte(input), &res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse yaml '%q': %w", input, err)
|
||||
}
|
||||
|
||||
var out any
|
||||
for _, f := range block.Fields {
|
||||
if f.CodecType == template.CodecConst {
|
||||
if f.Value != nil {
|
||||
out = utils.SetPathValue(f.Path, f.Value, out)
|
||||
}
|
||||
} else {
|
||||
if value, ok := res[f.Path]; ok {
|
||||
out = utils.SetPathValue(f.Path, value, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
Reference in New Issue
Block a user