|
| 1 | +package request |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "kusionstack.io/kusion/pkg/domain/constant" |
| 7 | + "kusionstack.io/kusion/pkg/domain/entity" |
| 8 | +) |
| 9 | + |
| 10 | +// CreateVariableRequest represents the create request structure |
| 11 | +// for a variable. |
| 12 | +type CreateVariableRequest struct { |
| 13 | + // Name is the name of the variable. |
| 14 | + Name string `json:"name" binding:"required"` |
| 15 | + // Value is the value of the variable. |
| 16 | + Value string `json:"value"` |
| 17 | + // Type is the type of the variable. |
| 18 | + Type entity.VariableType `json:"type"` |
| 19 | + // VariableSet is the variable set to which the variable belongs. |
| 20 | + VariableSet string `json:"variableSet" binding:"required"` |
| 21 | +} |
| 22 | + |
| 23 | +// UpdateVariableRequest represents the update request structure |
| 24 | +// for a variable. |
| 25 | +type UpdateVariableRequest struct { |
| 26 | + // Name is the name of the variable. |
| 27 | + Name string `json:"name" binding:"required"` |
| 28 | + // Value is the value of the variable. |
| 29 | + Value string `json:"value"` |
| 30 | + // Type is the type of the variable. |
| 31 | + Type entity.VariableType `json:"type"` |
| 32 | + // VariableSet is the variable set to which the variable belongs. |
| 33 | + VariableSet string `json:"variableSet" binding:"required"` |
| 34 | +} |
| 35 | + |
| 36 | +func (payload *CreateVariableRequest) Validate() error { |
| 37 | + // Validate variable name. |
| 38 | + if validName(payload.Name) { |
| 39 | + return constant.ErrInvalidVariableName |
| 40 | + } |
| 41 | + |
| 42 | + // Validate variable set name. . |
| 43 | + if validName(payload.VariableSet) { |
| 44 | + return constant.ErrInvalidVariableSetName |
| 45 | + } |
| 46 | + |
| 47 | + // Validate variable type. |
| 48 | + if payload.Type != "" && |
| 49 | + payload.Type != entity.PlainTextType && payload.Type != entity.CipherTextType { |
| 50 | + return constant.ErrInvalidVariableType |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func (payload *UpdateVariableRequest) Validate() error { |
| 57 | + // Validate variable name. |
| 58 | + if validName(payload.Name) { |
| 59 | + return constant.ErrInvalidVariableName |
| 60 | + } |
| 61 | + |
| 62 | + // Validate variable set name. . |
| 63 | + if validName(payload.VariableSet) { |
| 64 | + return constant.ErrInvalidVariableSetName |
| 65 | + } |
| 66 | + |
| 67 | + // Validate variable type. |
| 68 | + if payload.Type != "" && |
| 69 | + payload.Type != entity.PlainTextType && payload.Type != entity.CipherTextType { |
| 70 | + return constant.ErrInvalidVariableType |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (payload *CreateVariableRequest) Decode(r *http.Request) error { |
| 77 | + return decode(r, payload) |
| 78 | +} |
| 79 | + |
| 80 | +func (payload *UpdateVariableRequest) Decode(r *http.Request) error { |
| 81 | + return decode(r, payload) |
| 82 | +} |
0 commit comments