Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default value modification for simple type arguments #106

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/aaz_dev/command/api/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ def editor_workspace_command_argument(name, node_names, leaf_name, arg_var):
return jsonify(result)


# TODO: support to modify element of array or dict arguments
# @bp.route("/Workspaces/<name>/CommandTree/Nodes/<names_path:node_names>/Leaves/<name:leaf_name>/Arguments/<arg_var>/Element",
# methods=("GET", "PATCH"))
# def editor_workspace_command_arg_element_of_array_or_dict(name, node_names, leaf_name, arg_var):
# pass


@bp.route("/Workspaces/<name>/CommandTree/Nodes/<names_path:node_names>/Leaves/<name:leaf_name>/Arguments/<arg_var>/Flatten",
methods=("POST", ))
def editor_workspace_command_argument_flatten(name, node_names, leaf_name, arg_var):
Expand Down
18 changes: 15 additions & 3 deletions src/aaz_dev/command/controller/workspace_cfg_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import os

from command.model.configuration import CMDConfiguration, CMDHttpOperation, CMDDiffLevelEnum, \
CMDHttpRequest, CMDArgGroup, CMDObjectArg, CMDArrayArg, CMDArg, CMDClsArg, \
CMDHttpRequest, CMDArgGroup, CMDObjectArg, CMDArrayArg, CMDArg, CMDBooleanArg, CMDClsArg, \
CMDObjectArgBase, CMDArrayArgBase, CMDCondition, CMDConditionNotOperator, CMDConditionHasValueOperator, \
CMDConditionAndOperator, CMDCommandGroup, CMDArgumentHelp
CMDConditionAndOperator, CMDCommandGroup, CMDArgumentHelp, CMDArgDefault
from utils import exceptions
from utils.base64 import b64encode_str
from utils.case import to_camel_case
Expand Down Expand Up @@ -232,11 +232,14 @@ def update_arg_by_var(self, *cmd_names, arg_var, **kwargs):
return None
if isinstance(arg, CMDArg):
self._update_cmd_arg(arg, **kwargs)
if isinstance(arg, CMDBooleanArg):
self._update_boolean_arg(arg, **kwargs)
if isinstance(arg, CMDClsArg):
self._update_cls_arg(arg, **kwargs)
if isinstance(arg, CMDArrayArg):
self._update_array_arg(arg, **kwargs)
return arg

self.reformat()

def _update_cmd_arg(self, arg, **kwargs):
if 'options' in kwargs:
Expand All @@ -251,6 +254,15 @@ def _update_cmd_arg(self, arg, **kwargs):
arg.group = kwargs['group'] or None
if 'help' in kwargs:
arg.help = CMDArgumentHelp(kwargs['help'])
if 'default' in kwargs:
if kwargs['default'] is None:
arg.default = None
else:
arg.default = CMDArgDefault(kwargs['default'])

def _update_boolean_arg(self, arg, **kwargs):
if 'reverse' in kwargs:
arg.reverse = kwargs['reverse'] or False

def _update_cls_arg(self, arg, **kwargs):
if 'singularOptions' in kwargs:
Expand Down
66 changes: 61 additions & 5 deletions src/aaz_dev/command/model/configuration/_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,20 @@ def _reformat_base(self, **kwargs):
super()._reformat_base(**kwargs)
if self.enum:
self.enum.reformat(**kwargs)
if self.blank:
if not isinstance(self.blank.value, str) and not (self.nullable and self.blank.value is None):
raise exceptions.VerificationError(
f"Invalid blank value", details=f"'{self.blank.value}' is not a valid string arg value")


class CMDStringArg(CMDStringArgBase, CMDArg):
pass

def _reformat(self, **kwargs):
super()._reformat(**kwargs)
if self.default:
if not isinstance(self.default.value, str) and not (self.nullable and self.default.value is None):
raise exceptions.VerificationError(
f"Invalid default value", details=f"'{self.default.value}' is not a valid string arg value")


# byte: base64 encoded characters
Expand Down Expand Up @@ -421,7 +431,6 @@ class CMDResourceLocationArgBase(CMDStringArgBase):
TYPE_VALUE = "ResourceLocation"

no_rg_default = CMDBooleanField() # the default value will not be the location of resource group
# TODO: support global as default

@classmethod
def build_arg_base(cls, builder):
Expand Down Expand Up @@ -465,10 +474,20 @@ def _reformat_base(self, **kwargs):
super()._reformat_base(**kwargs)
if self.enum:
self.enum.reformat(**kwargs)
if self.blank:
if not isinstance(self.blank.value, int) and not (self.nullable and self.blank.value is None):
raise exceptions.VerificationError(
f"Invalid blank value", details=f"'{self.blank.value}' is not a valid int arg value")


class CMDIntegerArg(CMDIntegerArgBase, CMDArg):
pass

def _reformat(self, **kwargs):
super()._reformat(**kwargs)
if self.default:
if not isinstance(self.default.value, int) and not (self.nullable and self.default.value is None):
raise exceptions.VerificationError(
f"Invalid default value", details=f"'{self.default.value}' is not a valid int arg value")


# integer32
Expand Down Expand Up @@ -504,7 +523,13 @@ def build_arg_base(cls, builder):


class CMDBooleanArg(CMDBooleanArgBase, CMDArg):
pass

def _reformat(self, **kwargs):
super()._reformat(**kwargs)
if self.default:
if not isinstance(self.default.value, bool) and not (self.nullable and self.default.value is None):
raise exceptions.VerificationError(
f"Invalid default value", details=f"'{self.default.value}' is not a valid boolean arg value")


# float
Expand All @@ -530,10 +555,20 @@ def _reformat_base(self, **kwargs):
super()._reformat_base(**kwargs)
if self.enum:
self.enum.reformat(**kwargs)
if self.blank:
if not isinstance(self.blank.value, float) and not (self.nullable and self.blank.value is None):
raise exceptions.VerificationError(
f"Invalid blank value", details=f"'{self.blank.value}' is not a valid float arg value")


class CMDFloatArg(CMDFloatArgBase, CMDArg):
pass

def _reformat(self, **kwargs):
super()._reformat(**kwargs)
if self.default:
if not isinstance(self.default.value, float) and not (self.nullable and self.default.value is None):
raise exceptions.VerificationError(
f"Invalid default value", details=f"'{self.default.value}' is not a valid float arg value")


# float32
Expand Down Expand Up @@ -645,6 +680,11 @@ def _reformat_base(self, **kwargs):
if self.additional_props:
self.additional_props.reformat(**kwargs)

if self.blank:
if not isinstance(self.blank.value, dict) and not (self.nullable and self.blank.value is None):
raise exceptions.VerificationError(
f"Invalid blank value", details=f"'{self.blank.value}' is not a valid object arg value")


class CMDObjectArg(CMDObjectArgBase, CMDArg):

Expand All @@ -654,6 +694,13 @@ def build_arg(cls, builder):
assert isinstance(arg, CMDObjectArg)
return arg

def _reformat(self, **kwargs):
super()._reformat(**kwargs)
if self.default:
if not isinstance(self.default.value, dict) and not (self.nullable and self.default.value is None):
raise exceptions.VerificationError(
f"Invalid default value", details=f"'{self.default.value}' is not a valid object arg value")


# array
class CMDArrayArgBase(CMDArgBase):
Expand Down Expand Up @@ -693,6 +740,11 @@ def _reformat_base(self, **kwargs):
}
raise err

if self.blank:
if not isinstance(self.blank.value, list) and not (self.nullable and self.blank.value is None):
raise exceptions.VerificationError(
f"Invalid blank value", details=f"'{self.blank.value}' is not a valid array arg value")


class CMDArrayArg(CMDArrayArgBase, CMDArg):

Expand All @@ -713,3 +765,7 @@ def _reformat(self, **kwargs):
super()._reformat(**kwargs)
if self.singular_options:
self.singular_options = sorted(self.singular_options, key=lambda op: (len(op), op))
if self.default:
if not isinstance(self.default.value, list) and not (self.nullable and self.default.value is None):
raise exceptions.VerificationError(
f"Invalid default value", details=f"'{self.default.value}' is not a valid array arg value")
Loading