-
Notifications
You must be signed in to change notification settings - Fork 78
/
project.go
85 lines (77 loc) · 3.26 KB
/
project.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package scaffold
import (
"fmt"
"strconv"
)
// ProjectTemplate is a Kusion project template manifest.
type ProjectTemplate struct {
// ProjectName is a required fully qualified name.
ProjectName string `json:"projectName" yaml:"projectName"`
// Description is an optional description of the template.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Quickstart contains optional text to be displayed after template creation.
Quickstart string `json:"quickstart,omitempty" yaml:"quickstart,omitempty"`
// ProjectFields contains configuration in project level
ProjectFields []*FieldTemplate `json:"projectFields,omitempty" yaml:"projectFields,omitempty"`
// StackTemplates contains configuration in stack level
StackTemplates []*StackTemplate `json:"stacks,omitempty" yaml:"stacks,omitempty"`
}
// StackTemplates contains configuration in stack level
type StackTemplate struct {
// Name is stack name
Name string `json:"name" yaml:"name"`
// Fields contains all stack fields definition
Fields []*FieldTemplate `json:"fields,omitempty" yaml:"fields,omitempty"`
}
// FieldTemplate can describe all kinds of type, including primitive and composite.
type FieldTemplate struct {
// Name represents the field name, required
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Description represents the field description, optional
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Type can be string/int/bool/float/array/map/struct/any, required
Type FieldType `json:"type,omitempty" yaml:"type,omitempty"`
// Default represents default value for all FieldType
Default interface{} `json:"default,omitempty" yaml:"default,omitempty"`
// Elem is effective only when type is ArrayField
Elem *FieldTemplate `json:"elem,omitempty" yaml:"elem,omitempty"`
// Key is effective only when type is MapField
Key *FieldTemplate `json:"key,omitempty" yaml:"key,omitempty"`
// Value is effective only when type is MapField
Value *FieldTemplate `json:"value,omitempty" yaml:"value,omitempty"`
// Fields is effective only when type is StructField
Fields []*FieldTemplate `json:"fields,omitempty" yaml:"fields,omitempty"`
}
// FieldType includes field type that can be unmarshalled directly
type FieldType string
const (
StringField FieldType = "string"
IntField FieldType = "int"
BoolField FieldType = "bool"
FloatField FieldType = "float"
ArrayField FieldType = "array"
MapField FieldType = "map"
StructField FieldType = "struct"
AnyField FieldType = "any" // AnyField equal to interface{}
)
// IsPrimitive indicate the give field is one of StringField, IntField, FloatField, BoolField or not
func (f FieldType) IsPrimitive() bool {
return f == StringField || f == IntField || f == FloatField || f == BoolField
}
// RestoreActualValue help to transfer input to actual value according to its type
func (f *FieldTemplate) RestoreActualValue(input string) (actual interface{}, err error) {
if f.Type == "" {
return nil, fmt.Errorf("field %s miss type definition", f.Name)
}
switch f.Type {
case IntField:
actual, err = strconv.Atoi(input)
case BoolField:
actual, err = strconv.ParseBool(input)
case FloatField:
actual, err = strconv.ParseFloat(input, 64)
case StringField:
return input, nil
}
return actual, err
}