1515-export ([code_change /3 , handle_call /3 , handle_cast /2 , handle_info /2 , terminate /2 ]).
1616
1717-type state () :: #{
18- file_paths := [string ()],
18+ file_paths := [string () | binary () ],
1919 file_auto_update := boolean (),
2020 file_poll_interval := pos_integer (),
2121 file_allow_duplicate_keys := boolean (),
@@ -155,7 +155,7 @@ upsert_valid_flag_data(true, Tag, ParsedFlags, ParsedSegments) ->
155155upsert_valid_flag_data (false , _Tag , _ParsedFlags , _ParsedSegments ) ->
156156 error .
157157
158- -spec read_file (FilePath :: string (), Extension :: string ()) -> {ok | error , map ()}.
158+ -spec read_file (FilePath :: string () | binary () , Extension :: string ()) -> {ok | error , map ()}.
159159read_file (FilePath , " .yaml" ) ->
160160 Result = yamerl_constr :file (FilePath , [{detailed_constr , true }]),
161161 [Head | _ ] = ldclient_yaml_mapper :to_map_docs (Result , []),
@@ -167,7 +167,7 @@ read_file(FilePath, _Extension) ->
167167 error_logger :warning_msg (" File had unrecognized file extension. Valid extensions are .yaml and .json. File: ~p " , [FilePath ]),
168168 {error , #{}}.
169169
170- -spec try_read_file (FilePath :: string (), Extension :: string ()) -> {ok | error , map ()}.
170+ -spec try_read_file (FilePath :: string () | binary () , Extension :: string ()) -> {ok | error , map ()}.
171171try_read_file (FilePath , Extension ) ->
172172 try
173173 read_file (FilePath , Extension )
@@ -176,7 +176,7 @@ try_read_file(FilePath, Extension) ->
176176 {error , #{}}
177177 end .
178178
179- -spec load_file (FilePath :: string (), Extension :: string (), State :: state (), LoadedFlags :: map (), LoadedSegments :: map (), FlagCount :: non_neg_integer ()) ->
179+ -spec load_file (FilePath :: string () | binary () , Extension :: string (), State :: state (), LoadedFlags :: map (), LoadedSegments :: map (), FlagCount :: non_neg_integer ()) ->
180180 {ok | error , NewState :: state (), LoadedFlags :: map (), LoadedSegments :: map (), FlagCount :: non_neg_integer ()}.
181181load_file (FilePath , Extension , #{file_info := CurFileInfo } = State , LoadedFlags , LoadedSegments , FlagCount ) ->
182182 ModifiedTime = filelib :last_modified (FilePath ),
@@ -196,7 +196,7 @@ process_decoded_file(ok, Decoded, State, LoadedFlags, LoadedSegments, FlagCount)
196196process_decoded_file (error , _Decoded , State , LoadedFlags , LoadedSegments , FlagCount ) ->
197197 {error , State , LoadedFlags , LoadedSegments , FlagCount }.
198198
199- -spec check_modified (FilesToCheck :: [string ()], Modified :: boolean (), State :: state ()) ->
199+ -spec check_modified (FilesToCheck :: [string () | binary () ], Modified :: boolean (), State :: state ()) ->
200200 {Modified :: boolean (), State :: state ()}.
201201check_modified ([], Modified , State ) ->
202202 {Modified , State };
@@ -207,29 +207,29 @@ check_modified([FileToCheck | RestOfFiles], Modified, State) ->
207207 NewModified = Modified or (ExistingModifiedTime =/= ModifiedTime ),
208208 check_modified (RestOfFiles , NewModified , State ).
209209
210- -spec load_files_if_modified (Files :: [string ()], State :: state ()) -> {ok | error , State :: state ()}.
210+ -spec load_files_if_modified (Files :: [string () | binary () ], State :: state ()) -> {ok | error , State :: state ()}.
211211load_files_if_modified (Files , State ) ->
212212 case check_modified (Files , false , State ) of
213213 {true , UpdatedState } -> load_files (Files , UpdatedState );
214214 {false , UpdatedState } -> {ok , UpdatedState }
215215 end .
216216
217- -spec load_regular_file (FilePath :: string (), IsRegularFile :: boolean (), State :: state (), LoadedFlags :: map (), LoadedSegments :: map (), FlagCount :: non_neg_integer ()) ->
217+ -spec load_regular_file (FilePath :: string () | binary () , IsRegularFile :: boolean (), State :: state (), LoadedFlags :: map (), LoadedSegments :: map (), FlagCount :: non_neg_integer ()) ->
218218 {ok | error , NewState :: state (), LoadedFlags :: map (), LoadedSegments :: map (), FlagCount :: non_neg_integer ()}.
219219load_regular_file (FileToLoad , true , State , LoadedFlags , LoadedSegments , FlagCount ) ->
220220 load_file (FileToLoad , filename :extension (FileToLoad ), State , LoadedFlags , LoadedSegments , FlagCount );
221221load_regular_file (_FileToLoad , false , State , LoadedFlags , LoadedSegments , FlagCount ) ->
222222 {error , State , LoadedFlags , LoadedSegments , FlagCount }.
223223
224- -spec load_files (Files :: [string ()], State :: state ()) -> {ok | error , State :: state ()}.
224+ -spec load_files (Files :: [string () | binary () ], State :: state ()) -> {ok | error , State :: state ()}.
225225load_files (Files , State ) ->
226226 load_files (Files , State , #{}, #{}, 0 , ok ).
227227
228- -spec load_files (Files :: [string ()], State :: state (), CombinedFlags :: map (), CombinedSegments :: map (), FlagCount :: non_neg_integer (), Status :: ok | error ) ->
228+ -spec load_files (Files :: [string () | binary () ], State :: state (), CombinedFlags :: map (), CombinedSegments :: map (), FlagCount :: non_neg_integer (), Status :: ok | error ) ->
229229 {ok | error , State :: state ()}.
230230load_files ([FileToLoad | RestOfFiles ], State , LoadedFlags , LoadedSegments , FlagCount , ok ) ->
231231 {NewStatus , NewState , CombinedFlags , CombinedSegments , UpdatedCount } =
232- load_regular_file (FileToLoad , filelib :is_regular (FileToLoad ), State , LoadedFlags , LoadedSegments , FlagCount ),
232+ load_regular_file (handle_file_path_type ( FileToLoad ) , filelib :is_regular (handle_file_path_type ( FileToLoad ) ), State , LoadedFlags , LoadedSegments , FlagCount ),
233233 load_files (RestOfFiles , NewState , CombinedFlags , CombinedSegments , UpdatedCount , NewStatus );
234234load_files ([], #{file_allow_duplicate_keys := AllowDuplicateKeys , storage_tag := Tag } = State , LoadedFlags , LoadedSegments , FlagCount , ok ) ->
235235 Valid = (FlagCount == length (maps :keys (LoadedFlags ))) or AllowDuplicateKeys ,
@@ -238,3 +238,9 @@ load_files([], #{file_allow_duplicate_keys := AllowDuplicateKeys, storage_tag :=
238238load_files (_Files , State , _LoadedFlags , _LoadedSegments , _FlagCount , error ) ->
239239 % % If there is an error, then do not process any more files.
240240 {error , State }.
241+
242+ -spec handle_file_path_type (FilePath :: string () | binary ()) -> string ().
243+ handle_file_path_type (FilePath ) when is_binary (FilePath ) ->
244+ binary_to_list (FilePath );
245+ handle_file_path_type (FilePath ) ->
246+ FilePath .
0 commit comments