some updates

This commit is contained in:
Max Richter
2025-08-17 01:21:15 +02:00
parent cc8f967f07
commit 40b9be887d
7 changed files with 74 additions and 60 deletions

View File

@@ -1,10 +1,5 @@
package blocks
import "fmt"
func (b TemplateBlock) ParseListBlock(input string) (key string, value any, error error) {
fmt.Printf("Parsing List: '%q'", input)
return "", nil, nil
}

View File

@@ -1,7 +1,6 @@
package blocks
import (
"fmt"
"strings"
)
@@ -11,7 +10,7 @@ type TemplateType int
const (
InvalidTemplate TemplateType = iota
ShortTemplate
LongTemplate
ExtendedTemplate
)
// DetectTemplateType checks if the template is short or long.
@@ -36,36 +35,12 @@ func DetectTemplateType(tmpl string) TemplateType {
// }
if strings.Contains(trimmed, "\n") &&
(strings.Contains(trimmed, "path:") || strings.Contains(trimmed, "codec:")) {
return LongTemplate
return ExtendedTemplate
}
return InvalidTemplate
}
// CodecType represents the type of codec used to encode/render a value
type CodecType string
const (
CodecText CodecType = "text"
CodecNumber CodecType = "number"
CodecYaml CodecType = "yaml"
CodecList CodecType = "list"
)
func parseCodecType(input string) (CodecType, error) {
switch input {
case "number":
return CodecNumber, nil
case "yaml":
return CodecYaml, nil
case "list":
return CodecList, nil
case "text":
return CodecText, nil
}
return CodecText, fmt.Errorf("unknown codec: '%s'", input)
}
type BlockType string
const (
@@ -73,11 +48,18 @@ const (
MatchingBlock BlockType = "matching" // everything outside data blocks
)
type BlockField struct {
Path string
CodecType CodecType
Required bool
}
type TemplateBlock struct {
Type BlockType
Path string
Codec CodecType
Required bool
Fields []BlockField
content string
}

30
parser/blocks/codecs.go Normal file
View File

@@ -0,0 +1,30 @@
package blocks
import "fmt"
// CodecType represents the type of codec used to encode/render a value
type CodecType string
const (
CodecText CodecType = "text"
CodecNumber CodecType = "number"
CodecYaml CodecType = "yaml"
CodecList CodecType = "list"
CodecConst CodecType = "const"
)
func parseCodecType(input string) (CodecType, error) {
switch input {
case "number":
return CodecNumber, nil
case "yaml":
return CodecYaml, nil
case "list":
return CodecList, nil
case "text":
return CodecText, nil
case "const":
return CodecConst, nil
}
return CodecText, fmt.Errorf("unknown codec: '%s'", input)
}

View File

@@ -71,7 +71,6 @@ func parseYamlTemplate(input string) (block TemplateBlock, err error) {
dec.KnownFields(true)
if err := dec.Decode(&blk); err != nil {
fmt.Printf("Failed to parse:\n---\n%s\n---\n", cleaned)
return block, err
}
@@ -79,18 +78,46 @@ func parseYamlTemplate(input string) (block TemplateBlock, err error) {
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,
Required: field.Required,
})
}
return TemplateBlock{
Type: DataBlock,
Path: blk.Path,
Codec: codec,
Fields: fields,
content: input,
}, nil
}
func ParseTemplateBlock(template string, blockType BlockType) (block TemplateBlock, err error) {
@@ -102,17 +129,13 @@ func ParseTemplateBlock(template string, blockType BlockType) (block TemplateBlo
}, nil
}
block.Type = DataBlock
block.content = template
templateType := DetectTemplateType(template)
if templateType == InvalidTemplate {
return block, fmt.Errorf("Invalid Template")
}
if templateType == ShortTemplate {
switch DetectTemplateType(template) {
case ShortTemplate:
return parseShortTemplate(template)
case ExtendedTemplate:
return parseYamlTemplate(template)
}
return parseYamlTemplate(template)
return block, fmt.Errorf("Invalid Template")
}