feat: reimplement pathAlias for yaml fields
All checks were successful
Build and Push Server / build-and-push (push) Successful in 7m4s

This commit is contained in:
Max Richter
2025-10-24 11:12:35 +02:00
parent fcbce7b443
commit 75d6dab008
10 changed files with 51 additions and 20 deletions

View File

@@ -33,6 +33,13 @@ func Yaml(input string, block template.Block) (value any, error error) {
continue continue
} }
for _, alias := range f.PathAliases {
if value, ok := res[alias]; ok {
out = utils.SetPathValue(f.Path, value, out)
continue
}
}
if value, ok := renderUtils.GetValueFromPath(res, f.Path); ok { if value, ok := renderUtils.GetValueFromPath(res, f.Path); ok {
out = utils.SetPathValue(f.Path, value, out) out = utils.SetPathValue(f.Path, value, out)
continue continue

View File

@@ -87,7 +87,8 @@ My favourite baguette recipe
return; return;
} }
try { try {
compileTemplate(templateValue); const compiledTemplate = compileTemplate(templateValue);
console.log({ compiledTemplate, templateValue });
const result = templateValue const result = templateValue
? parseMarkdownWithTemplate(markdownValue, templateValue) ? parseMarkdownWithTemplate(markdownValue, templateValue)

Binary file not shown.

View File

@@ -14,6 +14,7 @@
- path: about - path: about
- path: url - path: url
- path: author.name - path: author.name
pathAlias: author
- path: author._type - path: author._type
codec: const codec: const
value: Person value: Person
@@ -21,11 +22,14 @@
- path: datePublished - path: datePublished
- path: articleSection - path: articleSection
- path: reviewRating.ratingValue - path: reviewRating.ratingValue
pathAlias: rating pathAlias:
- rating
- path: reviewRating.bestRating - path: reviewRating.bestRating
value: 5
codec: const codec: const
hidden: true hidden: true
- path: reviewRating.worstRating - path: reviewRating.worstRating
value: 1
codec: const codec: const
hidden: true hidden: true
} }

View File

@@ -17,8 +17,12 @@
hidden: true hidden: true
value: Person value: Person
- path: author.name - path: author.name
pathAlias:
- author
- path: author.email - path: author.email
- path: datePublished - path: datePublished
pathAlias:
- date
- path: prepTime - path: prepTime
- path: cookTime - path: cookTime
- path: recipeYield - path: recipeYield

View File

@@ -13,13 +13,17 @@
- path: tmdbId - path: tmdbId
- path: image - path: image
- path: author.name - path: author.name
pathAlias: author
- path: author._type - path: author._type
codec: const codec: const
value: Person value: Person
hidden: true hidden: true
- path: datePublished - path: datePublished
- path: itemReviewed.name pathAlias:
- date
- path: reviewRating.ratingValue - path: reviewRating.ratingValue
pathAlias:
- rating
- path: reviewRating.bestRating - path: reviewRating.bestRating
codec: const codec: const
value: 5 value: 5

View File

@@ -9,11 +9,13 @@ cmd = "go build -o ./tmp/marka-server ./cmd/marka-server"
bin = "./tmp/marka-server" bin = "./tmp/marka-server"
# Command to run the application with arguments. # Command to run the application with arguments.
full_bin = "./tmp/marka-server -root=../examples -addr=:8080" full_bin = "./tmp/marka-server -root=/home/max/Notes/resources -addr=:8080 -playground-root=./playground"
# Watch these file extensions. # Watch these file extensions.
include_ext = ["go", "http"] include_ext = ["go", "http"]
include_dir = ["../registry","../renderer","../template"]
# Ignore these directories. # Ignore these directories.
exclude_dir = ["tmp"] exclude_dir = ["tmp"]

View File

@@ -22,7 +22,7 @@ type yamlField struct {
Value any `yaml:"value,omitempty"` Value any `yaml:"value,omitempty"`
Codec string `yaml:"codec"` Codec string `yaml:"codec"`
Hidden bool `yaml:"hidden,omitempty"` Hidden bool `yaml:"hidden,omitempty"`
PathAlias string `yaml:"pathAlias,omitempty"` PathAlias []string `yaml:"pathAlias,omitempty"`
} }
func parseYamlTemplate(input Slice) (block Block, err error) { func parseYamlTemplate(input Slice) (block Block, err error) {
@@ -53,6 +53,7 @@ func parseYamlTemplate(input Slice) (block Block, err error) {
var fields []BlockField var fields []BlockField
for _, field := range blk.Fields { for _, field := range blk.Fields {
if field.Path == "" { if field.Path == "" {
return block, fmt.Errorf("failed to parse field: %v", field) return block, fmt.Errorf("failed to parse field: %v", field)
} }
@@ -66,18 +67,22 @@ func parseYamlTemplate(input Slice) (block Block, err error) {
return block, fmt.Errorf("failed to parse codec -> %w", err) return block, fmt.Errorf("failed to parse codec -> %w", err)
} }
fields = append(fields, BlockField{ block := BlockField{
Path: field.Path, Path: field.Path,
PathAliases: field.PathAlias,
CodecType: fieldCodec, CodecType: fieldCodec,
Value: field.Value, Value: field.Value,
Hidden: field.Hidden, Hidden: field.Hidden,
}) }
fields = append(fields, block)
} }
return Block{ return Block{
Type: DataBlock, Type: DataBlock,
Path: blk.Path, Path: blk.Path,
PathAliases: blk.PathAlias,
Codec: codec, Codec: codec,
Fields: fields, Fields: fields,
ListTemplate: blk.ListTemplate, ListTemplate: blk.ListTemplate,

View File

@@ -1,6 +1,8 @@
package template package template
import "strings" import (
"strings"
)
// CompileTemplate scans once, emitting: // CompileTemplate scans once, emitting:
// - data blocks: inner content between a line that's exactly "{" and a line that's exactly "}" // - data blocks: inner content between a line that's exactly "{" and a line that's exactly "}"

View File

@@ -9,6 +9,7 @@ const (
type BlockField struct { type BlockField struct {
Path string Path string
PathAliases []string
CodecType CodecType CodecType CodecType
Value any Value any
Hidden bool Hidden bool
@@ -17,6 +18,7 @@ type BlockField struct {
type Block struct { type Block struct {
Type BlockType Type BlockType
Path string Path string
PathAliases []string
Codec CodecType Codec CodecType
ListTemplate string ListTemplate string
Fields []BlockField Fields []BlockField