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

@@ -70,10 +70,5 @@ func ExtractBlocks(template string) ([]blocks.TemplateBlock, error) {
curlyIndex = nextCurlyIndex curlyIndex = nextCurlyIndex
} }
// var lastBlock = out[len(out)-1]
// if lastBlock.End == 0 {
// out = out[:len(out)-1]
// }
return out, nil return out, nil
} }

View File

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

View File

@@ -1,7 +1,6 @@
package blocks package blocks
import ( import (
"fmt"
"strings" "strings"
) )
@@ -11,7 +10,7 @@ type TemplateType int
const ( const (
InvalidTemplate TemplateType = iota InvalidTemplate TemplateType = iota
ShortTemplate ShortTemplate
LongTemplate ExtendedTemplate
) )
// DetectTemplateType checks if the template is short or long. // DetectTemplateType checks if the template is short or long.
@@ -36,36 +35,12 @@ func DetectTemplateType(tmpl string) TemplateType {
// } // }
if strings.Contains(trimmed, "\n") && if strings.Contains(trimmed, "\n") &&
(strings.Contains(trimmed, "path:") || strings.Contains(trimmed, "codec:")) { (strings.Contains(trimmed, "path:") || strings.Contains(trimmed, "codec:")) {
return LongTemplate return ExtendedTemplate
} }
return InvalidTemplate 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 type BlockType string
const ( const (
@@ -73,11 +48,18 @@ const (
MatchingBlock BlockType = "matching" // everything outside data blocks MatchingBlock BlockType = "matching" // everything outside data blocks
) )
type BlockField struct {
Path string
CodecType CodecType
Required bool
}
type TemplateBlock struct { type TemplateBlock struct {
Type BlockType Type BlockType
Path string Path string
Codec CodecType Codec CodecType
Required bool Required bool
Fields []BlockField
content string 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) dec.KnownFields(true)
if err := dec.Decode(&blk); err != nil { if err := dec.Decode(&blk); err != nil {
fmt.Printf("Failed to parse:\n---\n%s\n---\n", cleaned)
return block, err return block, err
} }
@@ -79,18 +78,46 @@ func parseYamlTemplate(input string) (block TemplateBlock, err error) {
return block, fmt.Errorf("missing top-level 'path'") return block, fmt.Errorf("missing top-level 'path'")
} }
if blk.Codec == "" {
blk.Codec = "text"
}
codec, err := parseCodecType(blk.Codec) codec, err := parseCodecType(blk.Codec)
if err != nil { if err != nil {
return block, fmt.Errorf("failed to parse codec: %w", err) 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{ return TemplateBlock{
Type: DataBlock, Type: DataBlock,
Path: blk.Path, Path: blk.Path,
Codec: codec, Codec: codec,
Fields: fields,
content: input, content: input,
}, nil }, nil
} }
func ParseTemplateBlock(template string, blockType BlockType) (block TemplateBlock, err error) { func ParseTemplateBlock(template string, blockType BlockType) (block TemplateBlock, err error) {
@@ -102,17 +129,13 @@ func ParseTemplateBlock(template string, blockType BlockType) (block TemplateBlo
}, nil }, nil
} }
block.Type = DataBlock switch DetectTemplateType(template) {
block.content = template case ShortTemplate:
templateType := DetectTemplateType(template)
if templateType == InvalidTemplate {
return block, fmt.Errorf("Invalid Template")
}
if templateType == ShortTemplate {
return parseShortTemplate(template) return parseShortTemplate(template)
case ExtendedTemplate:
return parseYamlTemplate(template)
} }
return parseYamlTemplate(template) return block, fmt.Errorf("Invalid Template")
} }

View File

@@ -4,26 +4,15 @@
codec: yaml codec: yaml
fields: fields:
- path: name - path: name
codec: text
required: true
- path: image - path: image
codec: text
required: true
- path: author.@type - path: author.@type
codec: const codec: const
value: Person value: Person
- path: author.name - path: author.name
codec: text
- path: datePublished - path: datePublished
codec: text
- path: description
codec: text
- path: prepTime - path: prepTime
codec: text
- path: cookTime - path: cookTime
codec: text
- path: recipeYield - path: recipeYield
codec: text
} }
--- ---