From cea9fff718efe18edd20ef6da6706d2ec287d081 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 31 Oct 2025 11:10:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/http_handlers/container_handler.erl | 102 +++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/apps/iot/src/http_handlers/container_handler.erl b/apps/iot/src/http_handlers/container_handler.erl index 433315b..6293d06 100644 --- a/apps/iot/src/http_handlers/container_handler.erl +++ b/apps/iot/src/http_handlers/container_handler.erl @@ -179,4 +179,104 @@ handle_request(_, Path, _, _) -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% helper methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ No newline at end of file +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +validate_config(Config) when is_map(Config) -> + %% 必选参数 + Required = [ + {image, binary}, + {container_name, binary}, + {command, list}, + {restart, binary}, + {privileged, boolean} + ], + + %% 可选参数(附带默认值) + Optional = [ + {envs, list}, + {ports, list}, + {expose, list}, + {volumes, list}, + {networks, list}, + {labels, map}, + {user, binary}, + {working_dir, binary}, + {hostname, binary}, + {cap_add, list}, + {cap_drop, list}, + {devices, list}, + {mem_limit, binary}, + {mem_reservation, binary}, + {cpu_shares, integer}, + {cpus, number}, + {ulimits, map}, + {sysctls, map}, + {tmpfs, list}, + {extra_hosts, list}, + {healthcheck, map} + ], + + Errors1 = check_required(Config, Required), + Errors2 = check_optional(Config, Optional), + + Errors = Errors1 ++ Errors2, + case Errors of + [] -> + ok; + _ -> + {error, lists:map(fun erlang:iolist_to_binary/1, Errors)} + end; + +validate_config(_) -> + {error, [<<"Config 必须为 map() 类型">>]}. + +%%------------------------------------------------------------------------------ +%% 校验必选项 +%%------------------------------------------------------------------------------ +check_required(Config, Fields) -> + lists:foldl( + fun({Key, Type}, ErrAcc) -> + case maps:get(Key, Config, undefined) of + undefined -> + [io_lib:format("缺少必选参数: ~p", [Key]) | ErrAcc]; + Value -> + case check_type(Value, Type) of + true -> + ErrAcc; + false -> + [io_lib:format("参数 ~p 类型错误,应为 ~p", [Key, Type]) | ErrAcc] + end + end + end, + [], Fields). + +%%------------------------------------------------------------------------------ +%% 校验可选项(支持默认值填充) +%%------------------------------------------------------------------------------ +check_optional(Config, Fields) -> + lists:foldl( + fun({Key, Type}, ErrAcc) -> + case maps:get(Key, Config, undefined) of + undefined -> + ErrAcc; + Value -> + case check_type(Value, Type) of + true -> + ErrAcc; + false -> + [io_lib:format("可选参数 ~p 类型错误,应为 ~p", [Key, Type]) | ErrAcc] + end + end + end, + [], Fields). + +%%------------------------------------------------------------------------------ +%% 类型检查辅助函数(binary版) +%%------------------------------------------------------------------------------ +check_type(Value, binary) -> is_binary(Value); +check_type(Value, integer) -> is_integer(Value); +check_type(Value, number) -> is_number(Value); +check_type(Value, list) -> is_list(Value); +check_type(Value, map) -> is_map(Value); +check_type(Value, boolean) -> is_boolean(Value); +check_type(_, _) -> false. \ No newline at end of file