5454
5555-include (" avro_internal.hrl" ).
5656
57- -type id2type () :: gb_trees : tree ( union_index (), type_or_name ()) .
58- -type name2id () :: gb_trees : tree ( name (), {union_index (), boolean ()}) .
57+ -type id2type () :: #{ union_index () => type_or_name ()} .
58+ -type name2id () :: #{ name () => {union_index (), boolean ()}} .
5959-type encode_result () :: avro_binary () | avro_json ().
6060-type encode_fun () :: fun ((avro_type (), avro :in (),
6161 union_index ()) -> encode_result ()).
6464
6565% % @doc Define a union type.
6666% % Exception when any of the below constraints is violated:
67- % % 1. A union should have at least one member and no duplication
68- % % 2. Union should no have union as direct member
69- % % 3. No duplicated types are allowed in members
67+ % % 1. Union should no have union as direct member
68+ % % 2. No duplicated types are allowed in members
7069% % @end
7170-spec type ([type_or_name ()]) -> union_type () | no_return ().
7271type ([]) ->
73- erlang :error (<<" union should have at least one member type" >>);
72+ # avro_union_type
73+ { id2type = #{}
74+ , name2id = #{}
75+ };
7476type ([_ | _ ] = Types0 ) ->
7577 IsUnion = fun (T ) -> ? IS_UNION_TYPE (T ) end ,
7678 lists :any (IsUnion , Types0 ) andalso
@@ -81,8 +83,8 @@ type([_ | _ ] = Types0) ->
8183 Name2Id = build_name_to_id (IndexedTypes ),
8284 ok = assert_no_duplicated_names (Name2Id , []),
8385 # avro_union_type
84- { id2type = gb_trees : from_orddict (IndexedTypes )
85- , name2id = gb_trees : from_orddict ( orddict : from_list (Name2Id ) )
86+ { id2type = maps : from_list (IndexedTypes )
87+ , name2id = maps : from_list (Name2Id )
8688 }.
8789
8890% % @doc Resolve fullname by newly discovered enclosing namespace.
@@ -103,17 +105,14 @@ update_member_types(T0, F) ->
103105% % @doc Get the union member types in a list.
104106- spec get_types (union_type ()) -> [avro_type ()].
105107get_types (# avro_union_type {id2type = IndexedTypes }) ->
106- {_Ids , Types } = lists :unzip (gb_trees : to_list (IndexedTypes )),
108+ {_Ids , Types } = lists :unzip (lists : keysort ( 1 , maps : to_list (IndexedTypes ) )),
107109 Types .
108110
109111% % @doc Search for a union member by its index or full name.
110112- spec lookup_type (name_raw () | union_index (), union_type ()) ->
111113 {ok , avro_type () | name ()} | false .
112114lookup_type (Id , # avro_union_type {id2type = Types }) when is_integer (Id ) ->
113- case gb_trees :lookup (Id , Types ) of
114- {value , Type } -> {ok , Type };
115- none -> false
116- end ;
115+ maps :is_key (Id , Types ) andalso {ok , maps :get (Id , Types )};
117116lookup_type (Name , Union ) when ? IS_NAME (Name ) ->
118117 case lookup_index (Name , Union ) of
119118 {ok , {_Id , true }} -> {ok , avro :name2type (Name )};
@@ -241,10 +240,7 @@ try_encode_union_loop(UnionType, [MemberT | Rest], Value, Index, EncodeFun) ->
241240- spec lookup_index (name (), union_type ()) ->
242241 {ok , {union_index (), boolean ()}} | false .
243242lookup_index (Name , # avro_union_type {name2id = Ids }) when ? IS_NAME_RAW (Name ) ->
244- case gb_trees :lookup (? NAME (Name ), Ids ) of
245- {value , Id } -> {ok , Id };
246- none -> false
247- end .
243+ maps :is_key (? NAME (Name ), Ids ) andalso {ok , maps :get (? NAME (Name ), Ids )}.
248244
249245% % @private
250246- spec do_cast (union_type (), {name (), avro_value ()} | avro :in ()) ->
0 commit comments