From 5a4bfff9e55486d36d911a6c5747aaebb9dbbbb6 Mon Sep 17 00:00:00 2001 From: zmstone Date: Wed, 17 Apr 2024 15:10:26 +0200 Subject: [PATCH] refactor: add template_str type --- apps/emqx/src/emqx_schema.erl | 11 +++++++++-- apps/emqx_conf/src/emqx_conf_schema_types.erl | 6 ++++++ apps/emqx_s3/src/emqx_s3_client.erl | 3 +-- apps/emqx_s3/src/emqx_s3_schema.erl | 4 ++-- apps/emqx_s3/test/emqx_s3_schema_SUITE.erl | 4 ++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/emqx/src/emqx_schema.erl b/apps/emqx/src/emqx_schema.erl index 57ad00e99..02e31387e 100644 --- a/apps/emqx/src/emqx_schema.erl +++ b/apps/emqx/src/emqx_schema.erl @@ -62,6 +62,7 @@ -type url() :: binary(). -type json_binary() :: binary(). -type template() :: binary(). +-type template_str() :: string(). -typerefl_from_string({duration/0, emqx_schema, to_duration}). -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({json_binary/0, emqx_schema, to_json_binary}). -typerefl_from_string({template/0, emqx_schema, to_template}). +-typerefl_from_string({template_str/0, emqx_schema, to_template_str}). -type parsed_server() :: #{ hostname := string(), @@ -123,7 +125,8 @@ to_comma_separated_atoms/1, to_url/1, to_json_binary/1, - to_template/1 + to_template/1, + to_template_str/1 ]). -export([ @@ -164,7 +167,8 @@ url/0, json_binary/0, port_number/0, - template/0 + template/0, + template_str/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) -> {ok, iolist_to_binary(Str)}. +to_template_str(Str) -> + {ok, unicode:characters_to_list(Str, utf8)}. + %% @doc support the following format: %% - 127.0.0.1:1883 %% - ::1:1883 diff --git a/apps/emqx_conf/src/emqx_conf_schema_types.erl b/apps/emqx_conf/src/emqx_conf_schema_types.erl index dbfbe74bc..bcc9c1469 100644 --- a/apps/emqx_conf/src/emqx_conf_schema_types.erl +++ b/apps/emqx_conf/src/emqx_conf_schema_types.erl @@ -76,6 +76,12 @@ readable("template()") -> dashboard => #{type => string, is_template => true}, 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()") -> #{ swagger => #{type => string}, diff --git a/apps/emqx_s3/src/emqx_s3_client.erl b/apps/emqx_s3/src/emqx_s3_client.erl index b7bd85833..a415cf8d4 100644 --- a/apps/emqx_s3/src/emqx_s3_client.erl +++ b/apps/emqx_s3/src/emqx_s3_client.erl @@ -103,7 +103,7 @@ put_object(Client, Key, Value) -> -spec put_object(client(), key(), upload_options(), iodata()) -> ok_or_error(term()). put_object( - #{bucket := Bucket0, headers := BaseHeaders, aws_config := AwsConfig = #aws_config{}}, + #{bucket := Bucket, headers := BaseHeaders, aws_config := AwsConfig = #aws_config{}}, Key, UploadOpts, Content @@ -111,7 +111,6 @@ put_object( ECKey = erlcloud_key(Key), ECOpts = erlcloud_upload_options(UploadOpts), 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 Props when is_list(Props) -> ok diff --git a/apps/emqx_s3/src/emqx_s3_schema.erl b/apps/emqx_s3/src/emqx_s3_schema.erl index de5e4f53e..1199948d0 100644 --- a/apps/emqx_s3/src/emqx_s3_schema.erl +++ b/apps/emqx_s3/src/emqx_s3_schema.erl @@ -74,7 +74,7 @@ fields(s3_upload) -> [ {bucket, mk( - emqx_schema:template(), + emqx_schema:template_str(), #{ desc => ?DESC("bucket"), required => true @@ -82,7 +82,7 @@ fields(s3_upload) -> )}, {key, mk( - emqx_schema:template(), + emqx_schema:template_str(), #{ desc => ?DESC("key"), required => true diff --git a/apps/emqx_s3/test/emqx_s3_schema_SUITE.erl b/apps/emqx_s3/test/emqx_s3_schema_SUITE.erl index 170f8a065..ec02aeac4 100644 --- a/apps/emqx_s3/test/emqx_s3_schema_SUITE.erl +++ b/apps/emqx_s3/test/emqx_s3_schema_SUITE.erl @@ -20,7 +20,7 @@ all() -> t_minimal_config(_Config) -> ?assertMatch( #{ - bucket := <<"bucket">>, + bucket := "bucket", host := "s3.us-east-1.endpoint.com", port := 443, min_part_size := 5242880, @@ -45,7 +45,7 @@ t_full_config(_Config) -> #{ access_key_id := "access_key_id", acl := public_read, - bucket := <<"bucket">>, + bucket := "bucket", host := "s3.us-east-1.endpoint.com", min_part_size := 10485760, port := 443,