mirror of
https://github.com/78/xiaozhi-esp32.git
synced 2026-01-14 01:07:30 +08:00
Merge 631a017ae4 into be88719932
This commit is contained in:
commit
d51a9acbd4
@ -63,28 +63,50 @@ private:
|
||||
bool has_default_value_;
|
||||
std::optional<int> min_value_; // 新增:整数最小值
|
||||
std::optional<int> max_value_; // 新增:整数最大值
|
||||
// 新增:字段描述(向后兼容,默认空)
|
||||
std::string description_;
|
||||
|
||||
std::vector<std::string> enum_strings_;
|
||||
std::vector<int> enum_ints_;
|
||||
|
||||
public:
|
||||
// Required field constructor
|
||||
Property(const std::string& name, PropertyType type)
|
||||
: name_(name), type_(type), has_default_value_(false) {}
|
||||
: name_(name), type_(type), has_default_value_(false), description_("") {}
|
||||
|
||||
// Optional field constructor with default value
|
||||
template<typename T>
|
||||
Property(const std::string& name, PropertyType type, const T& default_value)
|
||||
: name_(name), type_(type), has_default_value_(true) {
|
||||
: name_(name), type_(type), has_default_value_(true), description_("") {
|
||||
value_ = default_value;
|
||||
}
|
||||
|
||||
// --- 新增:带 description 的构造重载(向后兼容)
|
||||
Property(const std::string& name, PropertyType type, const std::string& description)
|
||||
: name_(name), type_(type), has_default_value_(false), description_(description) {}
|
||||
|
||||
template<typename T>
|
||||
Property(const std::string& name, PropertyType type, const std::string& description, const T& default_value)
|
||||
: name_(name), type_(type), has_default_value_(true), description_(description) {
|
||||
value_ = default_value;
|
||||
}
|
||||
|
||||
Property(const std::string& name, PropertyType type, int min_value, int max_value)
|
||||
: name_(name), type_(type), has_default_value_(false), min_value_(min_value), max_value_(max_value) {
|
||||
: name_(name), type_(type), has_default_value_(false), min_value_(min_value), max_value_(max_value), description_("") {
|
||||
if (type != kPropertyTypeInteger) {
|
||||
throw std::invalid_argument("Range limits only apply to integer properties");
|
||||
}
|
||||
}
|
||||
|
||||
Property(const std::string& name, PropertyType type, int min_value, int max_value, const std::string& description)
|
||||
: name_(name), type_(type), has_default_value_(false), min_value_(min_value), max_value_(max_value), description_(description) {
|
||||
if (type != kPropertyTypeInteger) {
|
||||
throw std::invalid_argument("Range limits only apply to integer properties");
|
||||
}
|
||||
}
|
||||
|
||||
Property(const std::string& name, PropertyType type, int default_value, int min_value, int max_value)
|
||||
: name_(name), type_(type), has_default_value_(true), min_value_(min_value), max_value_(max_value) {
|
||||
: name_(name), type_(type), has_default_value_(true), min_value_(min_value), max_value_(max_value), description_("") {
|
||||
if (type != kPropertyTypeInteger) {
|
||||
throw std::invalid_argument("Range limits only apply to integer properties");
|
||||
}
|
||||
@ -94,7 +116,41 @@ public:
|
||||
value_ = default_value;
|
||||
}
|
||||
|
||||
Property(const std::string& name, PropertyType type, int default_value, int min_value, int max_value, const std::string& description)
|
||||
: name_(name), type_(type), has_default_value_(true), min_value_(min_value), max_value_(max_value), description_(description) {
|
||||
if (type != kPropertyTypeInteger) {
|
||||
throw std::invalid_argument("Range limits only apply to integer properties");
|
||||
}
|
||||
if (default_value < min_value || default_value > max_value) {
|
||||
throw std::invalid_argument("Default value must be within the specified range");
|
||||
}
|
||||
value_ = default_value;
|
||||
}
|
||||
|
||||
// ================= 统一的 Enum 支持 =================
|
||||
|
||||
// 为字符串类型设置 enum 列表
|
||||
Property& SetEnum(const std::vector<std::string>& enums) {
|
||||
if (type_ != kPropertyTypeString) {
|
||||
throw std::invalid_argument("SetEnum can only be applied to string properties");
|
||||
}
|
||||
enum_strings_ = enums;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// 为整数类型设置 enum 列表
|
||||
Property& SetEnum(const std::vector<int>& enums) {
|
||||
if (type_ != kPropertyTypeInteger) {
|
||||
throw std::invalid_argument("SetEnum can only be applied to integer properties");
|
||||
}
|
||||
enum_ints_ = enums;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
|
||||
inline const std::string& name() const { return name_; }
|
||||
inline const std::string& description() const { return description_; }
|
||||
inline PropertyType type() const { return type_; }
|
||||
inline bool has_default_value() const { return has_default_value_; }
|
||||
inline bool has_range() const { return min_value_.has_value() && max_value_.has_value(); }
|
||||
@ -139,11 +195,34 @@ public:
|
||||
if (max_value_.has_value()) {
|
||||
cJSON_AddNumberToObject(json, "maximum", max_value_.value());
|
||||
}
|
||||
|
||||
// 如果设置了 integer enum,则输出 enum(整型)
|
||||
if (!enum_ints_.empty()) {
|
||||
cJSON* arr = cJSON_CreateArray();
|
||||
for (int v : enum_ints_) {
|
||||
cJSON_AddItemToArray(arr, cJSON_CreateNumber(v));
|
||||
}
|
||||
cJSON_AddItemToObject(json, "enum", arr);
|
||||
}
|
||||
} else if (type_ == kPropertyTypeString) {
|
||||
cJSON_AddStringToObject(json, "type", "string");
|
||||
if (has_default_value_) {
|
||||
cJSON_AddStringToObject(json, "default", value<std::string>().c_str());
|
||||
}
|
||||
|
||||
// 如果设置了 string enum,则输出 enum(字符串)
|
||||
if (!enum_strings_.empty()) {
|
||||
cJSON* arr = cJSON_CreateArray();
|
||||
for (const auto& s : enum_strings_) {
|
||||
cJSON_AddItemToArray(arr, cJSON_CreateString(s.c_str()));
|
||||
}
|
||||
cJSON_AddItemToObject(json, "enum", arr);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有描述,则输出 description 字段(向后兼容:老工具 description_ 为空)
|
||||
if (!description_.empty()) {
|
||||
cJSON_AddStringToObject(json, "description", description_.c_str());
|
||||
}
|
||||
|
||||
char *json_str = cJSON_PrintUnformatted(json);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user