Skip to content

Commit

Permalink
Fix bug: Directives will be error on deserialize rules and field with…
Browse files Browse the repository at this point in the history
… None type
  • Loading branch information
Jimmy01240397 committed Oct 27, 2024
1 parent 0b2fbb1 commit 66eb67b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
30 changes: 16 additions & 14 deletions hyperglass/models/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .fields import Action

StringOrArray = t.Union[str, t.List[str]]
Condition = t.Union[IPvAnyNetwork, str]
Condition = t.Union[str, None]
RuleValidation = t.Union[t.Literal["ipv4", "ipv6", "pattern"], None]
PassedValidation = t.Union[bool, None]
IPFamily = t.Literal["ipv4", "ipv6"]
Expand Down Expand Up @@ -264,7 +264,7 @@ class Directive(HyperglassUniqueModel, unique_by=("id", "table_output")):
id: str
name: str
rules: t.List[RuleType] = [RuleWithoutValidation()]
field: t.Union[Text, Select]
field: t.Union[Text, Select, None]
info: t.Optional[FilePath] = None
plugins: t.List[str] = []
table_output: t.Optional[str] = None
Expand All @@ -282,15 +282,16 @@ def validate_rules(cls, rules: t.List[t.Dict[str, t.Any]]):
condition = rule.get("condition")
if condition is None:
out_rules.append(RuleWithoutValidation(**rule))
try:
condition_net = ip_network(condition)
if condition_net.version == 4:
out_rules.append(RuleWithIPv4(**rule))
if condition_net.version == 6:
out_rules.append(RuleWithIPv6(**rule))
except ValueError:
out_rules.append(RuleWithPattern(**rule))
if isinstance(rule, Rule):
else:
try:
condition_net = ip_network(condition)
if condition_net.version == 4:
out_rules.append(RuleWithIPv4(**rule))
if condition_net.version == 6:
out_rules.append(RuleWithIPv6(**rule))
except ValueError:
out_rules.append(RuleWithPattern(**rule))
elif isinstance(rule, Rule):
out_rules.append(rule)
return out_rules

Expand All @@ -306,7 +307,8 @@ def validate_target(self, target: str) -> bool:
@property
def field_type(self) -> t.Literal["text", "select", None]:
"""Get the linked field type."""

if self.field is None:
return None
if self.field.is_select:
return "select"
if self.field.is_text or self.field.is_ip:
Expand Down Expand Up @@ -337,15 +339,15 @@ def frontend(self: "Directive") -> t.Dict[str, t.Any]:
"name": self.name,
"field_type": self.field_type,
"groups": self.groups,
"description": self.field.description,
"description": self.field.description if self.field is not None else '',
"info": None,
}

if self.info is not None:
with self.info.open() as md:
value["info"] = md.read()

if self.field.is_select:
if self.field is not None and self.field.is_select:
value["options"] = [o.export_dict() for o in self.field.options if o is not None]

return value
Expand Down
2 changes: 1 addition & 1 deletion hyperglass/models/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UIDirective(HyperglassModel):

id: str
name: str
field_type: str
field_type: t.Union[str, None]
groups: t.List[str]
description: str
info: t.Optional[str] = None
Expand Down
11 changes: 9 additions & 2 deletions hyperglass/ui/components/looking-glass-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ export const LookingGlassForm = (): JSX.Element => {
);

const directive = useMemo<Directive | null>(
() => getDirective(),
() => {
const tmp = getDirective();
if (tmp !== null && tmp.fieldType === null) {
setFormValue('queryTarget', ['null']);
setValue('queryTarget', ['null']);
}
return tmp;
},
[form.queryType, form.queryLocation, getDirective],
);

Expand Down Expand Up @@ -200,7 +207,7 @@ export const LookingGlassForm = (): JSX.Element => {
<QueryType onChange={handleChange} label={web.text.queryType} />
</FormField>
</SlideFade>
<SlideFade offsetX={100} in={directive !== null} unmountOnExit>
<SlideFade offsetX={100} in={directive !== null && directive.fieldType !== null} unmountOnExit>
{directive !== null && (
<FormField name="queryTarget" label={web.text.queryTarget}>
<QueryTarget
Expand Down

0 comments on commit 66eb67b

Please sign in to comment.