Skip to content
Open
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
45 changes: 45 additions & 0 deletions cyclops-ctrl/internal/mapper/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func HelmSchemaToFields(name string, schema helm.Property, defs map[string]helm.
fields = append(fields, HelmSchemaToFields(propertyName, property, defs, nil))
}

fields = append(fields, conditionedFields(schema, defs)...)

fields = sortFields(fields, schema.Order)

for _, dependency := range dependencies {
Expand Down Expand Up @@ -238,3 +240,46 @@ func resolvePropertyComposition(schema helm.Property) helm.Property {

return schema.AnyOf[0]
}

func conditionedFields(schema helm.Property, defs map[string]helm.Property) []models.Field {
if schema.If == nil {
return []models.Field{}
}

conditionalFields := make([]models.Field, 0)

if schema.Then != nil {
for propertyName, property := range schema.Then.Properties {
field := HelmSchemaToFields(propertyName, property, defs, nil)
field.Condition = mapConditions(schema.If, models.Equal)

conditionalFields = append(conditionalFields, field)
}
}

if schema.Else != nil {
for propertyName, property := range schema.Else.Properties {
field := HelmSchemaToFields(propertyName, property, defs, nil)
field.Condition = mapConditions(schema.If, models.NotEqual)

conditionalFields = append(conditionalFields, field)
}
}

return conditionalFields
}

func mapConditions(r *helm.Property, operation models.ConditionOperation) []models.Condition {
conditions := make([]models.Condition, 0)

for propertyName, property := range r.Properties {
conditions = append(conditions, models.Condition{
Operation: operation,
Property: propertyName,
Const: property.Const,
Enum: property.Enum,
})
}

return conditions
}
6 changes: 6 additions & 0 deletions cyclops-ctrl/internal/models/helm/helmschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ type Property struct {

// schema compositions
AnyOf []Property `json:"anyOf"`
AllOf []Property `json:"allOf"`

If *Property `json:"if,omitempty"`
Then *Property `json:"then,omitempty"`
Else *Property `json:"else,omitempty"`
Const *string `json:"const"`
}

type PropertyType string
Expand Down
16 changes: 16 additions & 0 deletions cyclops-ctrl/internal/models/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,20 @@ type Field struct {
MinLength *int `json:"minLength"`
MaxLength *int `json:"maxLength"`
Pattern *string `json:"pattern"`

Condition []Condition `json:"condition"`
}

type Condition struct {
Operation ConditionOperation `json:"operation"`
Property string `json:"property"`
Const interface{} `json:"const"`
Enum []interface{} `json:"enum"`
}

type ConditionOperation string

const (
Equal ConditionOperation = "eq"
NotEqual ConditionOperation = "neq"
)
30 changes: 24 additions & 6 deletions cyclops-ui/src/components/form/fields/string/StringField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import React, { useState } from "react";
import { Form, Input } from "antd";
import { stringInputValidators } from "./validators";
import { resolveConditions } from "../../../../utils/conditionalFields";

interface Props {
field: any;
Expand All @@ -17,16 +18,32 @@ const StringField = ({
isRequired,
isModuleEdit,
}: Props) => {
let stringValidationRules = stringInputValidators(field, isRequired);
const [display, setDisplay] = useState(!field.condition);

const stringValidationRules = stringInputValidators(field, isRequired);

const shouldUpdate = (prevValues, curValues) => {
if (!field.condition || field.condition.length === 0) {
return false;
}

let shouldDisplay = resolveConditions(field.condition, curValues);
if (shouldDisplay !== display) {
setDisplay(shouldDisplay);
return true;
}

return false;
};

return (
<Form.Item
{...arrayField}
name={formItemName}
style={{
paddingTop: "8px",
marginBottom: "12px",
}}
shouldUpdate={
!field.condition || field.condition.length === 0 ? false : shouldUpdate
}
style={{ paddingTop: "8px", marginBottom: "12px" }}
label={
<div>
{field.display_name}
Expand All @@ -38,6 +55,7 @@ const StringField = ({
hasFeedback={stringValidationRules.length > 0}
validateDebounce={1000}
rules={stringValidationRules}
hidden={!display}
>
<Input disabled={field.immutable && isModuleEdit} />
</Form.Item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ export const CreateModuleComponent = ({

values = findMaps(config.root.properties, values, initialValuesRaw);

setLoadingSubmitCreate(false);
// return

submitModule(
moduleName,
moduleNamespace,
Expand Down
29 changes: 29 additions & 0 deletions cyclops-ui/src/utils/conditionalFields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function resolveConditions(conditions: any[], values: any): boolean {
for (let condition of conditions) {
if (!resolveCondition(condition, values)) {
return false;
}
}

return true;
}

export function resolveCondition(condition: any, values: any): boolean {
const value = values[condition.property];

if (condition.operation === "eq") {
console.log(
"should update",
value,
condition.const,
value === condition.const,
);
return value === condition.const;
}

if (condition.operation === "neq") {
return value !== condition.const;
}

return true;
}