|
| 1 | +package metadata |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "configcenter/src/common" |
| 8 | + "configcenter/src/common/blog" |
| 9 | + "configcenter/src/common/mapstr" |
| 10 | + "configcenter/src/common/util" |
| 11 | + "configcenter/src/common/valid/attribute/manager/register" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + // Register the arrayBool attribute type |
| 16 | + register.Register(arrayBool{}) |
| 17 | +} |
| 18 | + |
| 19 | +type arrayBool struct { |
| 20 | +} |
| 21 | + |
| 22 | +// Name returns the name of the arrayBool attribute. |
| 23 | +func (a arrayBool) Name() string { |
| 24 | + return "array_bool" |
| 25 | +} |
| 26 | + |
| 27 | +// DisplayName returns the display name for user. |
| 28 | +func (a arrayBool) DisplayName() string { |
| 29 | + return "布尔数组" |
| 30 | +} |
| 31 | + |
| 32 | +// RealType returns the db type of the arrayBool attribute. |
| 33 | +// Flattened array uses LongChar as storage type |
| 34 | +func (a arrayBool) RealType() string { |
| 35 | + return common.FieldTypeLongChar |
| 36 | +} |
| 37 | + |
| 38 | +// Info returns the tips for user. |
| 39 | +func (a arrayBool) Info() string { |
| 40 | + return "布尔值的扁平化数组字段,存储多个true/false值" |
| 41 | +} |
| 42 | + |
| 43 | +// Validate validates the arrayBool attribute value |
| 44 | +func (a arrayBool) Validate(ctx context.Context, objID string, propertyType string, required bool, option interface{}, value interface{}) error { |
| 45 | + |
| 46 | + rid := util.ExtractRequestIDFromContext(ctx) |
| 47 | + |
| 48 | + if value == nil { |
| 49 | + if required { |
| 50 | + blog.Errorf("arrayBool attribute %s.%s value is required but got nil, rid: %s", objID, propertyType, rid) |
| 51 | + return fmt.Errorf("arrayBool attribute %s.%s value is required but got nil", objID, propertyType) |
| 52 | + } |
| 53 | + return nil |
| 54 | + } |
| 55 | + opts, err := ParseArrayOption[any](option) |
| 56 | + if err != nil { |
| 57 | + blog.Errorf("array_bool parse option failed: %v, rid: %s", err, rid) |
| 58 | + return fmt.Errorf("array_bool invalid option: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Validate that value is a slice of any |
| 62 | + boolArray, ok := value.([]interface{}) |
| 63 | + if !ok { |
| 64 | + blog.Errorf("arrayBool attribute %s.%s value must be []interface{}, got %T, rid: %s", objID, propertyType, value, rid) |
| 65 | + return fmt.Errorf("arrayBool attribute %s.%s value must be []interface{}, got %T", objID, propertyType, value) |
| 66 | + } |
| 67 | + if opts.Cap > len(boolArray) { |
| 68 | + return fmt.Errorf("array_bool invalid cap %d, rid: %s", opts.Cap, rid) |
| 69 | + } |
| 70 | + // Validate each item in the array is a boolean |
| 71 | + for i, item := range boolArray { |
| 72 | + if _, ok := item.(bool); !ok { |
| 73 | + blog.Errorf("arrayBool attribute %s.%s array item [%d] type %T is not bool, rid: %s", objID, propertyType, i, item, rid) |
| 74 | + return fmt.Errorf("arrayBool attribute %s.%s array item [%v] type %T is not bool", objID, propertyType, item, item) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// FillLostValue fills the lost value with default value |
| 82 | +func (a arrayBool) FillLostValue(ctx context.Context, valData mapstr.MapStr, propertyId string, defaultValue, option interface{}) error { |
| 83 | + |
| 84 | + rid := util.ExtractRequestIDFromContext(ctx) |
| 85 | + |
| 86 | + valData[propertyId] = nil |
| 87 | + if defaultValue == nil { |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + // Validate default value |
| 92 | + defaultArray, ok := defaultValue.([]interface{}) |
| 93 | + if !ok { |
| 94 | + blog.Errorf("arrayBool default value must be []interface{}, got %T, rid: %s", defaultValue, rid) |
| 95 | + return fmt.Errorf("arrayBool default value must be []interface{}, got %T", defaultValue) |
| 96 | + } |
| 97 | + |
| 98 | + // Validate each item in default array |
| 99 | + for i, item := range defaultArray { |
| 100 | + if _, ok := item.(bool); !ok { |
| 101 | + blog.Errorf("arrayBool default value array item [%d] type %T is not bool, rid: %s", i, item, rid) |
| 102 | + return fmt.Errorf("arrayBool default value array item [%d] type %T is not bool", i, item) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + valData[propertyId] = defaultArray |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// ValidateOption validates the option field |
| 111 | +func (a arrayBool) ValidateOption(ctx context.Context, option interface{}, defaultVal interface{}) error { |
| 112 | + |
| 113 | + rid := util.ExtractRequestIDFromContext(ctx) |
| 114 | + |
| 115 | + _, err := ParseArrayOption[any](option) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + if defaultVal == nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + // Validate default value |
| 124 | + defaultArray, ok := defaultVal.([]interface{}) |
| 125 | + if !ok { |
| 126 | + blog.Errorf("arrayBool default value must be []interface{}, got %T, rid: %s", defaultVal, rid) |
| 127 | + return fmt.Errorf("arrayBool default value must be []interface{}, got %T", defaultVal) |
| 128 | + } |
| 129 | + |
| 130 | + // Validate each item in default array |
| 131 | + for i, item := range defaultArray { |
| 132 | + if _, ok := item.(bool); !ok { |
| 133 | + blog.Errorf("arrayBool default value array item [%d] type %T is not bool, rid: %s", i, item, rid) |
| 134 | + return fmt.Errorf("arrayBool default value array item [%d] type %T is not bool", i, item) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +var _ register.AttributeTypeI = &arrayBool{} |
0 commit comments