diff --git a/package/hemar/parser/hemar.sh b/package/hemar/parser/hemar.sh index 43df862..0463015 100644 --- a/package/hemar/parser/hemar.sh +++ b/package/hemar/parser/hemar.sh @@ -108,7 +108,17 @@ # # Interpolation = { # "type": "interpolation", -# "path": string # path to variable in data model +# "path": [PathSegment, ...] # structured path to variable in data model +# } +# +# PathSegment = { +# "type": "root" # root path: "." +# } | { +# "type": "key", +# "key": string # key name (can contain spaces if quoted) +# } | { +# "type": "index", +# "index": number # array index (can be negative) # } # # Element types (planned for MVP): diff --git a/test/package/hemar/test/parser-interpolation.sh b/test/package/hemar/test/parser-interpolation.sh index 9e78b56..0ceca09 100644 --- a/test/package/hemar/test/parser-interpolation.sh +++ b/test/package/hemar/test/parser-interpolation.sh @@ -5,37 +5,37 @@ log notice "test case: ${WHITE}simple interpolation" answer="$(printf '%s' '{[hello]}' | hemar -c)" -expected='[{"type":"interpolation","path":"hello"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"hello"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}interpolation with text before and after" answer="$(printf '%s' 'foo {[bar]} baz' | hemar -c)" -expected='[{"type":"text","value":"foo "},{"type":"interpolation","path":"bar"},{"type":"text","value":" baz"}]' +expected='[{"type":"text","value":"foo "},{"type":"interpolation","path":[{"type":"key","key":"bar"}]},{"type":"text","value":" baz"}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}root path" answer="$(printf '%s' '{[.]}' | hemar -c)" -expected='[{"type":"interpolation","path":"."}]' +expected='[{"type":"interpolation","path":[{"type":"root"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}simple path" answer="$(printf '%s' '{[key]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}dot-separated path" answer="$(printf '%s' '{[key.subkey]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key.subkey"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key"},{"type":"key","key":"subkey"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}long path" answer="$(printf '%s' '{[key.subkey.subsubkey]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key.subkey.subsubkey"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key"},{"type":"key","key":"subkey"},{"type":"key","key":"subsubkey"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}multiple interpolations" answer="$(printf '%s' '{[a]} {[b]} {[c]}' | hemar -c)" -expected='[{"type":"interpolation","path":"a"},{"type":"text","value":" "},{"type":"interpolation","path":"b"},{"type":"text","value":" "},{"type":"interpolation","path":"c"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"a"}]},{"type":"text","value":" "},{"type":"interpolation","path":[{"type":"key","key":"b"}]},{"type":"text","value":" "},{"type":"interpolation","path":[{"type":"key","key":"c"}]}]' json_diff "$answer" "$expected" log notice "test passed" diff --git a/test/package/hemar/test/parser-paths.sh b/test/package/hemar/test/parser-paths.sh index 566d2f9..177173c 100644 --- a/test/package/hemar/test/parser-paths.sh +++ b/test/package/hemar/test/parser-paths.sh @@ -5,42 +5,42 @@ log notice "test case: ${WHITE}quoted string in path" answer="$(printf '%s' '{["key with spaces"]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key with spaces"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key with spaces"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}quoted dot in path" answer="$(printf '%s' '{[".key"]}' | hemar -c)" -expected='[{"type":"interpolation","path":".key"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":".key"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}quoted vs unquoted dot" answer="$(printf '%s' '{["."]} {[.]}' | hemar -c)" -expected='[{"type":"interpolation","path":"."},{"type":"text","value":" "},{"type":"interpolation","path":"."}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"."}]},{"type":"text","value":" "},{"type":"interpolation","path":[{"type":"root"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}path with index" answer="$(printf '%s' '{[key[0]]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key[0]"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key"},{"type":"index","index":0}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}path with multiple indices" answer="$(printf '%s' '{[[0][1][2]]}' | hemar -c)" -expected='[{"type":"interpolation","path":"[0][1][2]"}]' +expected='[{"type":"interpolation","path":[{"type":"index","index":0},{"type":"index","index":1},{"type":"index","index":2}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}path with negative index" answer="$(printf '%s' '{[key[-1]]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key[-1]"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key"},{"type":"index","index":-1}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}complex path with mixed segments" answer="$(printf '%s' '{["key".subkey[0]."subsubkey"]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key.subkey[0].subsubkey"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key"},{"type":"key","key":"subkey"},{"type":"index","index":0},{"type":"key","key":"subsubkey"}]}]' json_diff "$answer" "$expected" log notice "test case: ${WHITE}quoted string with escaped quote" answer="$(printf '%s' '{["key""with""quotes"]}' | hemar -c)" -expected='[{"type":"interpolation","path":"key\"with\"quotes"}]' +expected='[{"type":"interpolation","path":[{"type":"key","key":"key\"with\"quotes"}]}]' json_diff "$answer" "$expected" log notice "test passed"