package blocks import ( "fmt" "strings" ) // TemplateType represents whether a template is short, long, or invalid. type TemplateType int const ( InvalidTemplate TemplateType = iota ShortTemplate LongTemplate ) // DetectTemplateType checks if the template is short or long. func DetectTemplateType(tmpl string) TemplateType { trimmed := strings.TrimSpace(tmpl) // Short type: starts with "{" and ends with "}" on a single line, // and contains "|" or "," inside for inline definition // Matchs for example { name | text,required } if strings.HasPrefix(trimmed, "{") && strings.HasSuffix(trimmed, "}") && !strings.Contains(trimmed, "\n") { return ShortTemplate } // Long type: multiline and contains keys like "path:" or "codec:" inside // Matches for example: // { // path: name // codec: text // required: true // } if strings.Contains(trimmed, "\n") && (strings.Contains(trimmed, "path:") || strings.Contains(trimmed, "codec:")) { return LongTemplate } 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 ( DataBlock BlockType = "data" // content between lines "{" and "}" MatchingBlock BlockType = "matching" // everything outside data blocks ) type TemplateBlock struct { Type BlockType Path string Codec CodecType Required bool content string } func (b TemplateBlock) GetContent() string { return b.content } func (p *TemplateBlock) Parse(input string) (key string, value any, err error) { switch p.Codec { case CodecText: return p.Path, input, nil case CodecYaml: return p.ParseYamlBlock(input) case CodecList: return p.ParseListBlock(input) } return p.Path, "", nil }