feat: some updates
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
// Package template contains the logic for parsing template blocks.
|
||||
// Package blocks contains the logic for parsing template blocks.
|
||||
package template
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
// TemplateType represents whether a template is short, long, or invalid.
|
||||
@@ -53,114 +51,6 @@ func cleanTemplate(input string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
func parseShortTemplate(input string) (Block, error) {
|
||||
split := strings.Split(cleanTemplate(input), "|")
|
||||
if len(split) < 1 {
|
||||
return Block{}, fmt.Errorf("invalid short template")
|
||||
}
|
||||
|
||||
block := Block{
|
||||
Type: DataBlock,
|
||||
Path: strings.TrimSpace(split[0]),
|
||||
Codec: CodecText,
|
||||
content: input,
|
||||
}
|
||||
|
||||
if len(split) > 1 {
|
||||
optionSplit := strings.SplitSeq(split[1], ",")
|
||||
for option := range optionSplit {
|
||||
switch strings.TrimSpace(option) {
|
||||
case "number":
|
||||
block.Codec = CodecNumber
|
||||
case "text":
|
||||
block.Codec = CodecText
|
||||
case "hashtags":
|
||||
block.Codec = CodecHashtags
|
||||
default:
|
||||
return block, fmt.Errorf("unknown codec option: %s", option)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return block, nil
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
type yamlField struct {
|
||||
Path string `yaml:"path"`
|
||||
Value any `yaml:"value,omitempty"`
|
||||
Codec string `yaml:"codec"`
|
||||
Hidden bool `yaml:"hidden,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
|
||||
}
|
||||
|
||||
func ParseTemplateBlock(template string, blockType BlockType) (block Block, err error) {
|
||||
if blockType == MatchingBlock {
|
||||
return Block{
|
||||
|
Reference in New Issue
Block a user