# The function takes as input an attribute name, as a list of name's components, # and the value of the attribute. # The accumulator is used for recursion. # # It returns the tree representation of the JSON object representing the lsit of attributes # eg: # Given the following attributes: # {"occi.example.attr1", "value1"} # {"occi.example.attr2.with.additional.namespace", "value2"} # {"occi.example.attr1.type", "value3"} # # 1/ attributes names are splitted: # {[ "occi", "example", "attr1" ], "value1"} # {[ "occi", "example", "attr2", "with", "additional", "namespace" ], "value2"} # {[ "occi", "example", "attr1", "type" ], "value3"} # # 2/ this pair list is given to insert_attr and should output: # {[ # { "occi", [ # { "example", [ # { "attr1", "value }, # { "attr2", [ # { "with", [ # { "additional", [ # {"namespace", "value2"} # ]} # ]} # ]}, # { "attr1", [ # { "type": "value3" } # ]} # ]} # ]} # ]} # # This structure of tuple and lists is then transformed into JSON syntax with a pretty # straightforward algorithm: # { "key", "value" } -> "key": "value" # [ {"key1", "value1"}, {"key2", "value2"}] -> { "key1": "value1", "key2": "value2" } # etc. # # Name: list of attribute name components # Value: any object # Acc: a list of { Key, Value } pairs function insert_attr(Name, Value, Acc): if length(Name) == 1: # Last name component, we insert the value return prepend({Name, Value}, Acc); { ActualNS, Children } = first(Acc); if first(Name) == ActualNS: # This name component is already present in Acc, # insert attribute in the existing tree Tree = insert_attr(tail(Name), Value, Children); return prepend({Name, Tree}, tail(Acc)); else: # This name component does not exist in Acc, # creates a new branch Tree = insert_attr(tail(Name), Value, []); return prepend({Name, Tree}, Acc);