github.com/oam-dev/kubevela@v1.9.11/pkg/utils/schema/ui_schema.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package schema
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/kubevela/pkg/util/slices"
    23  )
    24  
    25  // UISchema ui schema
    26  type UISchema []*UIParameter
    27  
    28  // Validate check the ui schema
    29  func (u UISchema) Validate() error {
    30  	for _, p := range u {
    31  		// check the conditions
    32  		for _, c := range p.Conditions {
    33  			if err := c.Validate(); err != nil {
    34  				return err
    35  			}
    36  		}
    37  		//TODO: other fields check
    38  	}
    39  	return nil
    40  }
    41  
    42  // UIParameter Structured import table simple UI model
    43  type UIParameter struct {
    44  	Sort        uint      `json:"sort"`
    45  	Label       string    `json:"label"`
    46  	Description string    `json:"description"`
    47  	Validate    *Validate `json:"validate,omitempty"`
    48  	JSONKey     string    `json:"jsonKey"`
    49  	UIType      string    `json:"uiType"`
    50  	Style       *Style    `json:"style,omitempty"`
    51  	// means disable parameter in ui
    52  	Disable *bool `json:"disable,omitempty"`
    53  	// Conditions: control whether fields are enabled or disabled by certain conditions.
    54  	// Rules:
    55  	// if all conditions are not matching, the parameter will be disabled
    56  	// if there are no conditions, and disable==false the parameter will be enabled.
    57  	// if one disable action condition is matched, the parameter will be disabled.
    58  	// if all enable actions conditions are matched, the parameter will be enabled.
    59  	// +optional
    60  	Conditions              []Condition    `json:"conditions,omitempty"`
    61  	SubParameterGroupOption []GroupOption  `json:"subParameterGroupOption,omitempty"`
    62  	SubParameters           []*UIParameter `json:"subParameters,omitempty"`
    63  	AdditionalParameter     *UIParameter   `json:"additionalParameter,omitempty"`
    64  	Additional              *bool          `json:"additional,omitempty"`
    65  }
    66  
    67  // Condition control whether fields are enabled or disabled by certain conditions.
    68  type Condition struct {
    69  	// JSONKey specifies the path of the field, support the peer and subordinate fields.
    70  	JSONKey string `json:"jsonKey"`
    71  	// Op options includes `==` 、`!=` and `in`, default is `==`
    72  	// +optional
    73  	Op string `json:"op,omitempty"`
    74  	// Value specifies the prospective value.
    75  	Value interface{} `json:"value"`
    76  	// Action options includes `enable` or `disable`, default is `enable`
    77  	// +optional
    78  	Action string `json:"action,omitempty"`
    79  }
    80  
    81  // Validate check the validity of condition
    82  func (c Condition) Validate() error {
    83  	if c.JSONKey == "" {
    84  		return fmt.Errorf("the json key of the condition can not be empty")
    85  	}
    86  	if c.Action != "enable" && c.Action != "disable" && c.Action != "" {
    87  		return fmt.Errorf("the action of the condition only supports enable, disable or leave it empty")
    88  	}
    89  	if c.Op != "" && !slices.Contains([]string{"==", "!=", "in"}, c.Op) {
    90  		return fmt.Errorf("the op of the condition must be `==` 、`!=` and `in`")
    91  	}
    92  	return nil
    93  }
    94  
    95  // Style ui style
    96  type Style struct {
    97  	// ColSpan the width of a responsive layout
    98  	ColSpan int `json:"colSpan"`
    99  }
   100  
   101  // GroupOption define multiple data structure composition options.
   102  type GroupOption struct {
   103  	Label string   `json:"label"`
   104  	Keys  []string `json:"keys"`
   105  }
   106  
   107  // Validate parameter validate rule
   108  type Validate struct {
   109  	Required     bool        `json:"required,omitempty"`
   110  	Max          *float64    `json:"max,omitempty"`
   111  	MaxLength    *uint64     `json:"maxLength,omitempty"`
   112  	Min          *float64    `json:"min,omitempty"`
   113  	MinLength    uint64      `json:"minLength,omitempty"`
   114  	Pattern      string      `json:"pattern,omitempty"`
   115  	Options      []Option    `json:"options,omitempty"`
   116  	DefaultValue interface{} `json:"defaultValue,omitempty"`
   117  	// the parameter cannot be changed twice.
   118  	Immutable bool `json:"immutable"`
   119  }
   120  
   121  // Option select option
   122  type Option struct {
   123  	Label string      `json:"label"`
   124  	Value interface{} `json:"value"`
   125  }
   126  
   127  // GetDefaultUIType Set the default mapping for API Schema Type
   128  func GetDefaultUIType(apiType string, haveOptions bool, subType string, haveSub bool) string {
   129  	switch apiType {
   130  	case "string":
   131  		if haveOptions {
   132  			return "Select"
   133  		}
   134  		return "Input"
   135  	case "number", "integer":
   136  		return "Number"
   137  	case "boolean":
   138  		return "Switch"
   139  	case "array":
   140  		if subType == "string" {
   141  			return "Strings"
   142  		}
   143  		if subType == "number" || subType == "integer" {
   144  			return "Numbers"
   145  		}
   146  		return "Structs"
   147  	case "object":
   148  		if haveSub {
   149  			return "Group"
   150  		}
   151  		return "KV"
   152  	default:
   153  		return "Input"
   154  	}
   155  }