diff --git a/parser/main_test.go b/parser/main_test.go index 403bb10..70d8202 100644 --- a/parser/main_test.go +++ b/parser/main_test.go @@ -38,3 +38,32 @@ func TestParseRecipe_Golden(t *testing.T) { t.Fatalf("JSON mismatch (-want +got):\n%s", diff) } } + +func TestParseRecipe_NoDescription(t *testing.T) { + td := filepath.Join("testdata", "recipe_no_description") + input := filepath.Join(td, "input.md") + output := filepath.Join(td, "output.json") + + inputContent, err := os.ReadFile(input) + if err != nil { + t.Fatalf("read input.md: %v", err) + } + + got, err := parser.ParseFile(string(inputContent)) + if err != nil { + t.Fatalf("ParseFile: %v", err) + } + + var want map[string]any + b, err := os.ReadFile(output) + if err != nil { + t.Fatalf("read expected.json: %v", err) + } + if err := json.Unmarshal(b, &want); err != nil { + t.Fatalf("unmarshal expected.json: %v", err) + } + + if diff := cmp.Diff(want, got); diff != "" { + t.Fatalf("JSON mismatch (-want +got):\n%s", diff) + } +} diff --git a/parser/testdata/recipe_no_description/input.md b/parser/testdata/recipe_no_description/input.md new file mode 100644 index 0000000..89f55c0 --- /dev/null +++ b/parser/testdata/recipe_no_description/input.md @@ -0,0 +1,22 @@ +--- +@type: Recipe +image: https://example.com/salad.jpg +author.name: Alex Chef +prepTime: PT10M +cookTime: PT0M +recipeYield: 2 servings +--- + +# Simple Salad + +## Ingredients +- 100 g lettuce +- 5 cherry tomatoes +- 1 tbsp olive oil +- Pinch of salt + +## Steps +1. Wash and dry the lettuce. +2. Halve the cherry tomatoes. +3. Toss with olive oil and salt. + diff --git a/parser/testdata/recipe_no_description/output.json b/parser/testdata/recipe_no_description/output.json new file mode 100644 index 0000000..d67cc65 --- /dev/null +++ b/parser/testdata/recipe_no_description/output.json @@ -0,0 +1,24 @@ +{ + "@context": "https://schema.org", + "@type": "Recipe", + "name": "Simple Salad", + "image": "https://example.com/salad.jpg", + "author": { + "@type": "Person", + "name": "Alex Chef" + }, + "prepTime": "PT10M", + "cookTime": "PT0M", + "recipeYield": "2 servings", + "recipeIngredient": [ + "100 g lettuce", + "5 cherry tomatoes", + "1 tbsp olive oil", + "Pinch of salt" + ], + "recipeInstructions": [ + "Wash and dry the lettuce.", + "Halve the cherry tomatoes.", + "Toss with olive oil and salt." + ] +} diff --git a/parser/utils/set_path_value.go b/parser/utils/set_path_value.go index 2ebbae2..5900f1a 100644 --- a/parser/utils/set_path_value.go +++ b/parser/utils/set_path_value.go @@ -12,6 +12,11 @@ import ( // - when obj is a map -> if value is a map[string]any, merge into obj; otherwise obj is unchanged. // - otherwise -> returns obj unchanged. func SetPathValue(path string, value any, obj any) any { + // check if value is empty string and if so continue + if value == "" || value == nil { + return obj + } + // Split and drop empty segments (so ".", "..", "" become no keys) raw := strings.Split(path, ".") keys := raw[:0]