feat: some updates
This commit is contained in:
86
template/blocks_yaml.go
Normal file
86
template/blocks_yaml.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
type yamlBlock struct {
|
||||
Path string `yaml:"path"`
|
||||
Codec string `yaml:"codec"`
|
||||
Value any `yaml:"value,omitempty"`
|
||||
Fields []yamlField `yaml:"fields"`
|
||||
ListTemplate string `yaml:"listTemplate,omitempty"`
|
||||
Hidden bool `yaml:"hidden,omitempty"`
|
||||
PathAlias []string `yaml:"pathAlias,omitempty"`
|
||||
}
|
||||
|
||||
type yamlField struct {
|
||||
Path string `yaml:"path"`
|
||||
Value any `yaml:"value,omitempty"`
|
||||
Codec string `yaml:"codec"`
|
||||
Hidden bool `yaml:"hidden,omitempty"`
|
||||
PathAlias []string `yaml:"pathAlias,omitempty"`
|
||||
}
|
||||
|
||||
func parseYamlTemplate(input string) (block Block, err error) {
|
||||
var blk yamlBlock
|
||||
|
||||
cleaned := cleanTemplate(input)
|
||||
|
||||
dec := yaml.NewDecoder(strings.NewReader(cleaned))
|
||||
dec.KnownFields(true)
|
||||
|
||||
if err := dec.Decode(&blk); err != nil {
|
||||
return block, fmt.Errorf("content '%q': %w", cleaned, err)
|
||||
}
|
||||
|
||||
if blk.Path == "" {
|
||||
return block, fmt.Errorf("missing top-level 'path'")
|
||||
}
|
||||
|
||||
if blk.Codec == "" {
|
||||
blk.Codec = "text"
|
||||
}
|
||||
|
||||
codec, err := parseCodecType(blk.Codec)
|
||||
if err != nil {
|
||||
return block, fmt.Errorf("failed to parse codec: %w", err)
|
||||
}
|
||||
|
||||
var fields []BlockField
|
||||
|
||||
for _, field := range blk.Fields {
|
||||
if field.Path == "" {
|
||||
return block, fmt.Errorf("failed to parse field: %v", field)
|
||||
}
|
||||
|
||||
if field.Codec == "" {
|
||||
field.Codec = "text"
|
||||
}
|
||||
|
||||
fieldCodec, err := parseCodecType(field.Codec)
|
||||
if err != nil {
|
||||
return block, fmt.Errorf("failed to parse codec: %w", err)
|
||||
}
|
||||
|
||||
fields = append(fields, BlockField{
|
||||
Path: field.Path,
|
||||
CodecType: fieldCodec,
|
||||
Value: field.Value,
|
||||
Hidden: field.Hidden,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return Block{
|
||||
Type: DataBlock,
|
||||
Path: blk.Path,
|
||||
Codec: codec,
|
||||
Fields: fields,
|
||||
ListTemplate: blk.ListTemplate,
|
||||
content: input,
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user