Skip to content

Commit

Permalink
Migrate deprecated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bettio committed Jul 22, 2023
1 parent d66f40e commit 8eb9ef9
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion libs/eavmlib/src/spi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ validate_params(Params) ->
throw({error, {not_a_map_or_list, Params}}).

%% @private
validate_bus_config(BusConfig) when is_map(BusConfig) orelse is_list(BusConfig) ->
validate_bus_config(MaybeOldBusConfig) when is_map(MaybeOldBusConfig) orelse is_list(MaybeOldBusConfig) ->
BusConfig = migrate_deprecated(MaybeOldBusConfig),
#{
miso => validate_integer_entry(miso, BusConfig),
mosi => validate_integer_entry(mosi, BusConfig),
Expand All @@ -310,6 +311,46 @@ validate_bus_config(undefined) ->
validate_bus_config(BusConfig) ->
throw({badarg, {not_a_map_or_list, BusConfig}}).

migrate_deprecated(MaybeDeprecated) when is_map(MaybeDeprecated) ->
Iter = maps:iterator(MaybeDeprecated),
migrate_deprecated_iter(maps:next(Iter), #{});
migrate_deprecated(MaybeDeprecated) when is_list(MaybeDeprecated) ->
lists:foldl(
fun migrate_deprecated_fold/2,
[],
MaybeDeprecated
);
migrate_deprecated(MaybeDeprecated) ->
throw({bardarg, {not_a_map_or_list, MaybeDeprecated}}).

%% @private
migrate_deprecated_iter(none, Accum) ->
Accum;
migrate_deprecated_iter({K, V, Iter}, Accum) ->
NewK = replace_key(K),
warn_deprecated(K, NewK),
migrate_deprecated_iter(maps:next(Iter), Accum#{NewK => V}).

%% @private
migrate_deprecated_fold({K, V}, Accum) ->
NewK = replace_key(K),
warn_deprecated(K, NewK),
[{NewK, V} | Accum];
migrate_deprecated_fold(E, Accum) ->
[E | Accum].

%% @private
replace_key(Key) ->
case Key of
miso_io_num -> miso;
Any -> Any
end.

warn_deprecated(Key, Key) ->
ok;
warn_deprecated(OldKey, NewKey) ->
io:format("SPI: found deprecated ~p, use ~p instead!!!~n", [OldKey, NewKey]).

%% @private
validate_integer_entry(Key, Map) ->
validate_integer_entry(Key, Map, undefined).
Expand Down

0 comments on commit 8eb9ef9

Please sign in to comment.