refactor: add template_str type

This commit is contained in:
zmstone 2024-04-17 15:10:26 +02:00
parent 5c014f4c29
commit 5a4bfff9e5
5 changed files with 20 additions and 8 deletions

View File

@ -62,6 +62,7 @@
-type url() :: binary(). -type url() :: binary().
-type json_binary() :: binary(). -type json_binary() :: binary().
-type template() :: binary(). -type template() :: binary().
-type template_str() :: string().
-typerefl_from_string({duration/0, emqx_schema, to_duration}). -typerefl_from_string({duration/0, emqx_schema, to_duration}).
-typerefl_from_string({duration_s/0, emqx_schema, to_duration_s}). -typerefl_from_string({duration_s/0, emqx_schema, to_duration_s}).
@ -80,6 +81,7 @@
-typerefl_from_string({url/0, emqx_schema, to_url}). -typerefl_from_string({url/0, emqx_schema, to_url}).
-typerefl_from_string({json_binary/0, emqx_schema, to_json_binary}). -typerefl_from_string({json_binary/0, emqx_schema, to_json_binary}).
-typerefl_from_string({template/0, emqx_schema, to_template}). -typerefl_from_string({template/0, emqx_schema, to_template}).
-typerefl_from_string({template_str/0, emqx_schema, to_template_str}).
-type parsed_server() :: #{ -type parsed_server() :: #{
hostname := string(), hostname := string(),
@ -123,7 +125,8 @@
to_comma_separated_atoms/1, to_comma_separated_atoms/1,
to_url/1, to_url/1,
to_json_binary/1, to_json_binary/1,
to_template/1 to_template/1,
to_template_str/1
]). ]).
-export([ -export([
@ -164,7 +167,8 @@
url/0, url/0,
json_binary/0, json_binary/0,
port_number/0, port_number/0,
template/0 template/0,
template_str/0
]). ]).
-export([namespace/0, roots/0, roots/1, fields/1, desc/1, tags/0]). -export([namespace/0, roots/0, roots/1, fields/1, desc/1, tags/0]).
@ -2601,6 +2605,9 @@ to_json_binary(Str) ->
to_template(Str) -> to_template(Str) ->
{ok, iolist_to_binary(Str)}. {ok, iolist_to_binary(Str)}.
to_template_str(Str) ->
{ok, unicode:characters_to_list(Str, utf8)}.
%% @doc support the following format: %% @doc support the following format:
%% - 127.0.0.1:1883 %% - 127.0.0.1:1883
%% - ::1:1883 %% - ::1:1883

View File

@ -76,6 +76,12 @@ readable("template()") ->
dashboard => #{type => string, is_template => true}, dashboard => #{type => string, is_template => true},
docgen => #{type => "String", desc => ?DESC(template)} docgen => #{type => "String", desc => ?DESC(template)}
}; };
readable("template_str()") ->
#{
swagger => #{type => string},
dashboard => #{type => string, is_template => true},
docgen => #{type => "String", desc => ?DESC(template)}
};
readable("binary()") -> readable("binary()") ->
#{ #{
swagger => #{type => string}, swagger => #{type => string},

View File

@ -103,7 +103,7 @@ put_object(Client, Key, Value) ->
-spec put_object(client(), key(), upload_options(), iodata()) -> ok_or_error(term()). -spec put_object(client(), key(), upload_options(), iodata()) -> ok_or_error(term()).
put_object( put_object(
#{bucket := Bucket0, headers := BaseHeaders, aws_config := AwsConfig = #aws_config{}}, #{bucket := Bucket, headers := BaseHeaders, aws_config := AwsConfig = #aws_config{}},
Key, Key,
UploadOpts, UploadOpts,
Content Content
@ -111,7 +111,6 @@ put_object(
ECKey = erlcloud_key(Key), ECKey = erlcloud_key(Key),
ECOpts = erlcloud_upload_options(UploadOpts), ECOpts = erlcloud_upload_options(UploadOpts),
Headers = join_headers(BaseHeaders, maps:get(headers, UploadOpts, undefined)), Headers = join_headers(BaseHeaders, maps:get(headers, UploadOpts, undefined)),
Bucket = to_list_string(Bucket0),
try erlcloud_s3:put_object(Bucket, ECKey, Content, ECOpts, Headers, AwsConfig) of try erlcloud_s3:put_object(Bucket, ECKey, Content, ECOpts, Headers, AwsConfig) of
Props when is_list(Props) -> Props when is_list(Props) ->
ok ok

View File

@ -74,7 +74,7 @@ fields(s3_upload) ->
[ [
{bucket, {bucket,
mk( mk(
emqx_schema:template(), emqx_schema:template_str(),
#{ #{
desc => ?DESC("bucket"), desc => ?DESC("bucket"),
required => true required => true
@ -82,7 +82,7 @@ fields(s3_upload) ->
)}, )},
{key, {key,
mk( mk(
emqx_schema:template(), emqx_schema:template_str(),
#{ #{
desc => ?DESC("key"), desc => ?DESC("key"),
required => true required => true

View File

@ -20,7 +20,7 @@ all() ->
t_minimal_config(_Config) -> t_minimal_config(_Config) ->
?assertMatch( ?assertMatch(
#{ #{
bucket := <<"bucket">>, bucket := "bucket",
host := "s3.us-east-1.endpoint.com", host := "s3.us-east-1.endpoint.com",
port := 443, port := 443,
min_part_size := 5242880, min_part_size := 5242880,
@ -45,7 +45,7 @@ t_full_config(_Config) ->
#{ #{
access_key_id := "access_key_id", access_key_id := "access_key_id",
acl := public_read, acl := public_read,
bucket := <<"bucket">>, bucket := "bucket",
host := "s3.us-east-1.endpoint.com", host := "s3.us-east-1.endpoint.com",
min_part_size := 10485760, min_part_size := 10485760,
port := 443, port := 443,