fix one problem in the CLIs that try to use enums

This commit is contained in:
OMGeeky
2024-05-18 14:45:59 +02:00
parent f1a5835563
commit ef00af9d13
2 changed files with 45 additions and 4 deletions

View File

@@ -679,6 +679,25 @@ def is_required_property(p):
return p.get('required', False) or p.get('priority', 0) > 0
def has_default_value(p):
from .enum_utils import is_property_enum, get_enum_default
if is_property_enum(p):
return get_enum_default(p) is not None
return p.get('default', None) is not None
def get_default_value(p):
if not has_default_value(p):
return None
from .enum_utils import is_property_enum, get_enum_default
if is_property_enum(p):
return get_enum_default(p)
return p.default
def is_repeated_property(p):
return p.get('repeated', False)
@@ -1249,3 +1268,8 @@ def unique(
if __name__ == '__main__':
raise AssertionError('For import only')

View File

@@ -2,7 +2,7 @@
<%!
from generator.lib.util import (hub_type, mangle_ident, indent_all_but_first_by, activity_rust_type, setter_fn_name, ADD_PARAM_FN,
upload_action_fn, is_schema_with_optionals, schema_markers, indent_by, method_default_scope,
ADD_SCOPE_FN, TREF, enclose_in)
ADD_SCOPE_FN, TREF, enclose_in, has_default_value, get_default_value)
from generator.lib.cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, OUTPUT_FLAG, VALUE_ARG,
CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG,
call_method_ident, POD_TYPES, opt_value, ident,
@@ -10,6 +10,7 @@
application_secret_path, CONFIG_DIR_FLAG, req_value, MODE_ARG,
opt_values, SCOPE_ARG, CONFIG_DIR_ARG, DEFAULT_MIME, field_vec, comma_sep_fields, JSON_TYPE_TO_ENUM_MAP,
CTYPE_TO_ENUM_MAP)
from generator.lib.enum_utils import is_property_enum
from generator.lib.types import JSON_TO_RUST_DEFAULT
v_arg = '<%s>' % VALUE_ARG
SOPT = 'self.opt'
@@ -35,6 +36,7 @@ use client::{InvalidOptionsError, CLIError, arg_from_str, writer_from_opts, pars
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, CallType, UploadProtocol,
calltype_from_str, remove_json_null_values, ComplexType, JsonType, JsonTypeInfo};
use std::convert::TryInto;
use std::default::Default;
use std::error::Error as StdError;
use std::str::FromStr;
@@ -227,10 +229,24 @@ for parg in ${opt_values(VALUE_ARG)} {
% for p in optional_props:
<%
ptype = actual_json_type(p.name, p.get("format", p.type))
default_value = JSON_TO_RUST_DEFAULT[ptype]
value_unwrap = f"value.unwrap_or({default_value})"
no_default_enum = False
if is_property_enum(p):
default_value = 'Default::default()'
value_unwrap = f"&value.and_then(|v|v.try_into().ok()).unwrap_or({default_value})"
no_default_enum = True
else:
default_value = JSON_TO_RUST_DEFAULT[ptype]
value_unwrap = f"value.unwrap_or({default_value})"
%>\
"${mangle_subcommand(p.name)}" => {
% if no_default_enum:
if let Some(value) = value.and_then(|v|v.try_into().ok()) {
call = call.${mangle_ident(setter_fn_name(p))}(&value);
} else {
/*value was empty but has no default*/
todo!("I don't know what would be best in this situation.")
}
% else:
% if p.name == 'alt':
if ${value_unwrap} == "media" {
download_mode = true;
@@ -243,6 +259,7 @@ for parg in ${opt_values(VALUE_ARG)} {
${value_unwrap}\
% endif # handle conversion
);
% endif # enum without default or not
},
% endfor # each property
_ => {
@@ -258,7 +275,7 @@ ${value_unwrap}\
}
% endif
found = true;
call = call.${ADD_PARAM_FN}(self.gpm.iter().find(|t| t.0 == key).unwrap_or(&("", key)).1, ${value_unwrap});
call = call.${ADD_PARAM_FN}(self.gpm.iter().find(|t| t.0 == key).unwrap_or(&("", key)).1, ${value_unwrap}.into());
break;
}
}