some updates
This commit is contained in:
@@ -70,10 +70,5 @@ func ExtractBlocks(template string) ([]blocks.TemplateBlock, error) {
|
||||
curlyIndex = nextCurlyIndex
|
||||
}
|
||||
|
||||
// var lastBlock = out[len(out)-1]
|
||||
// if lastBlock.End == 0 {
|
||||
// out = out[:len(out)-1]
|
||||
// }
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
@@ -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
30
parser/blocks/codecs.go
Normal 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)
|
||||
}
|
@@ -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")
|
||||
|
||||
}
|
||||
|
@@ -4,26 +4,15 @@
|
||||
codec: yaml
|
||||
fields:
|
||||
- path: name
|
||||
codec: text
|
||||
required: true
|
||||
- path: image
|
||||
codec: text
|
||||
required: true
|
||||
- path: author.@type
|
||||
codec: const
|
||||
value: Person
|
||||
- path: author.name
|
||||
codec: text
|
||||
- path: datePublished
|
||||
codec: text
|
||||
- path: description
|
||||
codec: text
|
||||
- path: prepTime
|
||||
codec: text
|
||||
- path: cookTime
|
||||
codec: text
|
||||
- path: recipeYield
|
||||
codec: text
|
||||
}
|
||||
---
|
||||
|
||||
|
Reference in New Issue
Block a user