public ObservationTransformer() { JUSTContext context = new JUSTContext { EvaluationMode = EvaluationMode.Strict, DefaultDecimalPlaces = 4 }; JsonTransformer = new JsonTransformer(context); using (var stream = GetType().Assembly.GetManifestResourceStream("DIPS.Fhir.Service.Transformers.observation-transformer1.json")) { var streamReader = new StreamReader(stream); Transform1 = streamReader.ReadToEnd(); } using (var stream = GetType().Assembly.GetManifestResourceStream("DIPS.Fhir.Service.Transformers.observation-transformer2.json")) { var streamReader = new StreamReader(stream); Transform2 = streamReader.ReadToEnd(); } using (var stream = GetType().Assembly.GetManifestResourceStream("DIPS.Fhir.Service.Transformers.observation-transformer3.json")) { var streamReader = new StreamReader(stream); Transform3 = streamReader.ReadToEnd(); } }
public void UnregisterCustomFunction() { const string unregisteredFunction = "NavigateTypedNullParameters"; const string input = "{ \"lvl1\": { \"some-bool\": true } }"; const string transformer = "{ \"instance\": \"#InstanceMethod()\", \"navigated\": \"#NavigateTypedNullParameters(#valueof($.non-existent))\" }"; var context = new JUSTContext(new[] { new CustomFunction { AssemblyName = "ExternalMethods", Namespace = "ExternalMethods.ExternalClass", MethodName = "InstanceMethod" }, new CustomFunction { AssemblyName = "ExternalMethods", Namespace = "ExternalMethods.ExternalClass", MethodName = unregisteredFunction } }); context.UnregisterCustomFunction(unregisteredFunction); var result = Assert.Throws <Exception>(() => new JsonTransformer(context).Transform(transformer, input)); Assert.AreEqual($"Error while calling function : #{unregisteredFunction}(#valueof($.non-existent)) - Invalid function: #{unregisteredFunction}", result.Message); }
public void SimpleArrayElement() { var input = "{\"Food\": {\"Desserts\": {\"item\": [{\"name\": \"carrot cake\",\"price\": 5},{\"name\": \"ice cream\",\"price\": 10}]}}}"; var transformer = "[ \"#valueof($.Food.Desserts.item[*].name)\" ]"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("[\"carrot cake\",\"ice cream\"]", result); }
public void String() { var input = "{\"Food\": {\"Desserts\": {\"item\": [{\"name\": \"carrot cake\",\"price\": 5},{\"name\": \"ice cream\",\"price\": 10}]}}}"; var transformer = "\"abc\""; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("\"abc\"", result); }
public void RegisteredCustomFunction() { const string input = "{ \"ExcessQuoteAmendments\": [{ \"Name\": \"test_name\", \"Attributes\": [{ \"CoverageDataType\": \"test_coverage\", \"Value\": \"test_value\"}, { }] }]"; string transformer = "{ \"ExcessFormsList\": { \"#loop($.ExcessQuoteAmendments)\": { \"AttributesList\": { \"#loop($.Attributes)\": { \"Value\":\"#aliascustomfunction(#currentvalueatpath($.Value), #currentvalueatpath($.CoverageDataType))\" }}}}}"; var context = new JUSTContext(); context.RegisterCustomFunction("ExternalMethods", "ExternalMethods.ExternalClass", "CheckNullParameters", "aliascustomfunction"); var actual = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"ExcessFormsList\":[{\"AttributesList\":[{\"Value\":\"test_value\"},{\"Value\":null}]}]}", actual); }
public void EvalWithObjectInside() { var input = "{ \"sections\": [ { \"id\": \"first\", \"label\": \"First section\" }, { \"id\": \"second\", \"label\": \"Second section\" } ] }"; var transformer = "{ \"areas\": { \"#loop($.sections)\": { \"#eval(#currentvalueatpath($.id))\": { \"description\": \"#currentvalueatpath($.label)\" } } } }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"areas\":[{\"first\":{\"description\":\"First section\"}},{\"second\":{\"description\":\"Second section\"}}]}", result); }
public void ConditionalGroupNonExistingWithLoopInside() { var input = "{ }"; var transformer = "{ \"Result\": { \"#ifgroup(#exists($.errors.account))\": { \"#loop($.errors.account)\": { \"ValidationMessage\": \"#currentvalueatpath($)\" } } }, \"Other\": \"property\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.FallbackToDefault }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"Result\":{},\"Other\":\"property\"}", result); }
public void JmesPath() { var input = "{\"locations\": [{\"name\": \"Seattle\", \"state\": \"WA\"},{\"name\": \"New York\", \"state\": \"NY\"},{\"name\": \"Bellevue\", \"state\": \"WA\"},{\"name\": \"Olympia\", \"state\": \"WA\"}]}"; var transformer = "{ \"result\": \"#valueof(locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)})\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer <JmesPathSelectable>(context).Transform(transformer, input); Assert.AreEqual("{\"result\":{\"WashingtonCities\":\"Bellevue, Olympia, Seattle\"}}", result); }
public void ArrayConcatenation() { var input = "{ \"drugs\": [{ \"code\": \"001\", \"display\": \"Drug1\" },{ \"code\": \"002\", \"display\": \"Drug2\" }],\"pa\": [{ \"code\": \"pa1\", \"display\": \"PA1\" },{ \"code\": \"pa2\", \"display\": \"PA2\" }], \"sa\": [{ \"code\": \"sa1\", \"display\": \"SA1\" },{ \"code\": \"sa2\", \"display\": \"SA2\" }]}"; var transformer = "{ \"concat\": \"#concat(#valueof($.drugs), #valueof($.pa))\", \"multipleConcat\": \"#concat(#concat(#valueof($.drugs), #valueof($.pa)), #valueof($.sa))\", \"xconcat\": \"#xconcat(#valueof($.drugs), #valueof($.pa), #valueof($.sa))\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"concat\":[{\"code\":\"001\",\"display\":\"Drug1\"},{\"code\":\"002\",\"display\":\"Drug2\"},{\"code\":\"pa1\",\"display\":\"PA1\"},{\"code\":\"pa2\",\"display\":\"PA2\"}],\"multipleConcat\":[{\"code\":\"001\",\"display\":\"Drug1\"},{\"code\":\"002\",\"display\":\"Drug2\"},{\"code\":\"pa1\",\"display\":\"PA1\"},{\"code\":\"pa2\",\"display\":\"PA2\"},{\"code\":\"sa1\",\"display\":\"SA1\"},{\"code\":\"sa2\",\"display\":\"SA2\"}],\"xconcat\":[{\"code\":\"001\",\"display\":\"Drug1\"},{\"code\":\"002\",\"display\":\"Drug2\"},{\"code\":\"pa1\",\"display\":\"PA1\"},{\"code\":\"pa2\",\"display\":\"PA2\"},{\"code\":\"sa1\",\"display\":\"SA1\"},{\"code\":\"sa2\",\"display\":\"SA2\"}]}", result); }
public void EmptyPropertiesLooping() { var input = "{ \"animals\": { } }"; var transformer = "{ \"sounds\": { \"#loop($.animals)\": { \"#eval(#currentproperty())\": \"#currentvalueatpath($..sound)\" } } }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"sounds\":{}}", result); }
public void SingleResultFilter() { var input = "{\"array\":[{\"resource\":\"Location\",\"number\":\"3\" },{\"resource\":\"Organization\",\"number\":\"10\"}] }"; var transformer = "{\"result\":{\"#loop($.array[?(@resource=='Location')])\":{\"existsLocation\":true}}}"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"result\":[{\"existsLocation\":true}]}", result); }
public void EmptyArrayLooping() { const string input = "{ \"Sex\": \"Female\", \"Colors\": [ ]}"; const string transformer = "{ \"Colors\": { \"#loop($.Colors)\": \"#currentvalue()\" } }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"Colors\":[]}", result); }
public void NullLooping() { var input = "{ \"spell_numbers\": null }"; var transformer = "{ \"number_index\": { \"#loop($.spell_numbers)\": { \"#eval(#currentindex())\": \"#currentvalueatpath(#concat($.,#currentproperty()))\" } } }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"number_index\":null}", result); }
public void ConditionalGroupSingleValue() { var input = "{ \"arr\": [\"1234\", \"5678\" ]}"; var transformer = "{\"result\": [ \"#ifgroup(False,#valueof($.arr[0]))\", \"#ifgroup(True,#valueof($.arr[1]))\" ] }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"result\":[\"5678\"]}", result); }
public void LoopOverProperties() { var input = "{ \"animals\": { \"cat\": { \"legs\": 4, \"sound\": \"meow\" }, \"dog\": { \"legs\": 4, \"sound\": \"woof\" } }, \"spell_numbers\": { \"3\": \"three\", \"2\": \"two\", \"1\": \"one\" } }"; var transformer = "{ \"sounds\": { \"#loop($.animals)\": { \"#eval(#currentproperty())\": \"#currentvalueatpath($..sound)\" } }, \"number_index\": { \"#loop($.spell_numbers)\": { \"#eval(#currentindex())\": \"#currentvalueatpath(#concat($.,#currentproperty()))\" } }}"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"sounds\":{\"cat\":\"meow\",\"dog\":\"woof\"},\"number_index\":{\"0\":\"three\",\"1\":\"two\",\"2\":\"one\"}}", result); }
public void ApplyOver() { var input = "{\"d\": [ \"one\", \"two\", \"three\" ], \"values\": [ \"z\", \"c\", \"n\" ]}"; var transformer = "{ \"result\": \"#applyover({ 'condition': { '#loop($.values)': { 'test': '#ifcondition(#stringcontains(#valueof($.d[0]),#currentvalue()),true,yes,no)' } } }, '#exists($.condition[?(@.test=='yes')])')\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"result\":true}", result); }
public void IsArray(string typedValue, bool expectedResult) { var input = $"{{ \"value\": {typedValue} }}"; var transformer = "{ \"result\": \"#isarray(#valueof($.value))\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual($"{{\"result\":{expectedResult.ToString().ToLower()}}}", result); }
public void Escape() { var input = "{ \"arg\": 1, \"arr\": [{ \"id\": 1, \"val\": 100 }, { \"id\": 2, \"val\": 200 }] }"; var transformer = "{ \"sharp\": \"/#not_a_function\", \"sharp_arg\": \"#xconcat(/#not,_a_function_arg)\", \"parentheses\": \"#xconcat(func/(',#valueof($.arg),'/))\", \"comma\": \"#xconcat(func/(',#valueof($.arg),'/,'other_value'/))\", \"dynamic_expr\": \"#valueof(#xconcat($.arr[?/(@.id==,#valueof($.arg),/)].val))\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"sharp\":\"#not_a_function\",\"sharp_arg\":\"#not_a_function_arg\",\"parentheses\":\"func('1')\",\"comma\":\"func('1','other_value')\",\"dynamic_expr\":100}", result); }
public void MultipleEvals() { const string input = "{\"people\": [{ \"name\": \"Jim\", \"number\": \"0123-4567-8888\" }, { \"name\": \"John\", \"number\": \"0134523-4567-8910\" }]}"; const string transformer = "{ \"root\": { \"#loop($.people)\": { \"#eval(#xconcat(name, #add(#currentindex(),1)))\": \"#currentvalueatpath($.name)\", \"#eval(#xconcat(number, #add(#currentindex(),1)))\": \"#currentvalueatpath($.number)\" } } }"; var context = new JUSTContext(); context.EvaluationMode = EvaluationMode.Strict; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"root\":[{\"name1\":\"Jim\",\"number1\":\"0123-4567-8888\"},{\"name2\":\"John\",\"number2\":\"0134523-4567-8910\"}]}", result); }
public void EqualsCaseSensitive() { var transformer = "{ \"stringresult\": { \"stringequals\": \"#stringequals(#valueof($.d[0]),oNe)\" }}"; var context = new JUSTContext() { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, ExampleInputs.StringsArray); Assert.AreEqual("{\"stringresult\":{\"stringequals\":false}}", result); }
public void StringEmpty() { var input = "{ \"empty\": \"\", \"not_empty\": \"not empty\" }"; var transformer = "{ \"test_empty\": \"#ifcondition(#valueof($.empty),#stringempty(),is empty,not empty)\", \"test_not_empty\": \"#ifcondition(#valueof($.not_empty),#stringempty(),empty,is not empty)\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"test_empty\":\"is empty\",\"test_not_empty\":\"is not empty\"}", result); }
public void SingleIndexReference() { var input = "{\"array\":[{\"resource\":\"Location\",\"number\":\"3\" },{\"resource\":\"Organization\",\"number\":\"10\"}] }"; var transformer = "{\"result\": {\"#loop($.array[1])\": {\"number\":\"#currentvalueatpath($.number)\"} }}"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = JsonTransformer.Transform(transformer, input, context); Assert.AreEqual("{\"result\":[{\"number\":\"10\"}]}", result); }
public void Escape() { var input = "{ \"arg\": \"some_value\" }"; var transformer = "{ \"sharp\": \"/#not_a_function\", \"parentheses\": \"#xconcat(func/(',#valueof($.arg),'/))\", \"comma\": \"#xconcat(func/(',#valueof($.arg),'/,'other_value'/))\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"sharp\":\"#not_a_function\",\"parentheses\":\"func('some_value')\",\"comma\":\"func('some_value','other_value')\"}", result); }
public void ArrayWithNullValue() { var input = "{ \"Systems\": [ ] }"; var transformer = "{ \"systemIds\": [ \"#valueof($.Systems[:].Id)\" ] }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"systemIds\":[]}", result); }
public void TypedValuesInsideEval() { const string input = "{ \"a\": [{ \"name\": \"prop1\", \"val\": 1 }, { \"name\": \"prop2\", \"val\": 2 }] }"; string transformer = "{ \"a\": { \"#loop($.a)\": { \"#eval(#currentvalueatpath($.name))\": \"#currentvalueatpath($.val)\" } } }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"a\":[{\"prop1\":1},{\"prop2\":2}]}", result); }
public void LoopOverPropertiesInsideLoop() { var input = "{ \"Systems\": [ { \"Id\": \"SystemId1\", \"Name\": \"Name of system 1\", \"Components\": [ { \"Id\": \"CompId1\", \"Name\": \"comp 1\", \"Properties\": { \"PropA\": \"valueA\", \"Prop2\": 2.2 } }, { \"Id\": \"CompId2\", \"Name\": \"comp 2\", \"Properties\": { \"PropC\": \"valuec\", \"Prop2\": 222.222 } } ] }, { \"Id\": \"SystemId2\", \"Name\": \"Name of system 2\", \"Components\": [ { \"Id\": \"CompId3\", \"Name\": \"comp 3\", \"Properties\": { \"PropD\": \"valueD\" } }, { \"Id\": \"CompId4\", \"Name\": \"comp 4\", \"Properties\": { \"Prop1\": 11, \"Prop2\": 22.22 } } ] } ]}"; var transformer = "{ \"result\": { \"#loop($.Systems,outer)\": { \"#eval(#xconcat(System.,#currentvalueatpath($.Id),.Name))\": \"#currentvalueatpath($.Name)\", \"components\": { \"#loop($.Components,inner)\": { \"#eval(#xconcat(System.,#currentvalueatpath($.Id,outer),.Componets.,#currentvalueatpath($.Id,inner)))\": \"#currentvalueatpath($.Name,inner)\", \"properties\": { \"#loop($.Properties)\": { \"#eval(#xconcat(System.,#currentvalueatpath($.Id,outer),.Components.,#currentvalueatpath($.Id,inner),.,#currentproperty()))\": \"#currentvalueatpath(#xconcat($.,#currentproperty()))\" } } } } } } }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"result\":[{\"components\":[{\"properties\":{\"System.SystemId1.Components.CompId1.PropA\":\"valueA\",\"System.SystemId1.Components.CompId1.Prop2\":2.2},\"System.SystemId1.Componets.CompId1\":\"comp 1\"},{\"properties\":{\"System.SystemId1.Components.CompId2.PropC\":\"valuec\",\"System.SystemId1.Components.CompId2.Prop2\":222.222},\"System.SystemId1.Componets.CompId2\":\"comp 2\"}],\"System.SystemId1.Name\":\"Name of system 1\"},{\"components\":[{\"properties\":{\"System.SystemId2.Components.CompId3.PropD\":\"valueD\"},\"System.SystemId2.Componets.CompId3\":\"comp 3\"},{\"properties\":{\"System.SystemId2.Components.CompId4.Prop1\":11,\"System.SystemId2.Components.CompId4.Prop2\":22.22},\"System.SystemId2.Componets.CompId4\":\"comp 4\"}],\"System.SystemId2.Name\":\"Name of system 2\"}]}", result); }
public void LoopingRoot() { var input = "[{\"Id\":1,\"Fields\":{\"Email\":{\"Name\":\"Email\",\"FieldType\":3,\"Value\":\"[email protected]\"}},\"CreatedAt\":\"2021-10-07T13:40:14.813Z\"},{\"Id\":2,\"Fields\":{\"Email\":{\"Name\":\"Email\",\"FieldType\":3,\"Value\":\"[email protected]\"}},\"CreatedAt\":\"2021-10-07T13:44:24.48Z\"},{\"Id\":3,\"Fields\":{\"Email\":{\"Name\":\"Email\",\"FieldType\":3,\"Value\":\"[email protected]\"}},\"CreatedAt\":\"2021-10-07T13:45:09.417Z\"}]"; var transformer = "{\"#loop($)\":{\"#eval(#currentvalueatpath($.Id))\":\"#currentvalueatpath($.Fields['Email'].Value)\"}}"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("[{\"1\":\"[email protected]\"},{\"2\":\"[email protected]\"},{\"3\":\"[email protected]\"}]", result); }
public void OneArgumentFunctionWithEscapedChars() { const string input = "{ \"creditsDebits\": [{ \"ratingModifierValue\": null, \"name\": \"Qualifications\", \"id\": \"3d4f2273-edb3-4959-b232-e7386e8dca1e\", \"factorValue\": null, \"factorMin\": null, \"factorMax\": null, \"defaultValue\": null, \"code\": \"ECC002\" }, { \"ratingModifierValue\": \"Need this value\", \"name\": \"Loss Experience\", \"id\": \"af0324f3-6676-4faf-a9e9-c14f1eaa2fee\", \"factorValue\": null, \"factorMin\": null, \"factorMax\": null, \"defaultValue\": null, \"code\": \"ECC100\" }] }"; string transformer = "{ \"someNewNode\": \"#valueof($.creditsDebits[?/(@.code == 'ECC100'/)].ratingModifierValue)\" }"; var context = new JUSTContext { EvaluationMode = EvaluationMode.Strict }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"someNewNode\":\"Need this value\"}", result); }
public void OtherEscapeChar() { const char escapeChar = '§'; string args = $"#arg1,#xconcat({escapeChar}#notfunc{escapeChar}({escapeChar}), #constant_comma(),#xconcat({escapeChar},arg2.3.1,#constant_hash(),'arg2.3.3),{escapeChar},,#add(3,2))"; string input = "{ \"test\": \"" + args + "\" }"; string transformer = "{ \"result\": \"#xconcat(" + args + ",#constant_hash())\" }"; var context = new JUSTContext { EscapeChar = '§' }; var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"result\":\"#arg1#notfunc(),,arg2.3.1#'arg2.3.3,5#\"}", result); }
public void RegisterInContextConstructor() { const string input = "{ \"lvl1\": { \"some-bool\": true } }"; const string transformer = "{ \"result\": \"#NavigateTypedNullParameters(#valueof($.non-existent))\" }"; var context = new JUSTContext(new[] { new CustomFunction { AssemblyName = "ExternalMethods", Namespace = "ExternalMethods.ExternalClass", MethodName = "NavigateTypedNullParameters" } }); var result = new JsonTransformer(context).Transform(transformer, input); Assert.AreEqual("{\"result\":null}", result); }