{"record":{"id":"ddd1127837bef1fe","repo":"larksuite/cli","slug":"array-default-for-s-requires-exactly-d-items-go","errorCode":null,"errorMessage":"array default for %s requires exactly %d items, got %d","messagePattern":"array default for (.+?) requires exactly (.+?) items, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/common/typed_compile_args.go","lineNumber":632,"sourceCode":"\tcase typedObjectShape:\n\t\treturn base.Kind() == reflect.Struct || base.Kind() == reflect.Map || base.Kind() == reflect.Interface\n\tdefault:\n\t\treturn false\n\t}\n}\n\nfunc valueAssignableTo(value any, target reflect.Type) error {\n\tbase := indirectType(target)\n\tif base.Kind() == reflect.Array && value != nil {\n\t\tsource := reflect.ValueOf(value)\n\t\tfor source.Kind() == reflect.Pointer || source.Kind() == reflect.Interface {\n\t\t\tif source.IsNil() {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tsource = source.Elem()\n\t\t}\n\t\tif (source.Kind() == reflect.Array || source.Kind() == reflect.Slice) && source.Len() != base.Len() {\n\t\t\treturn fmt.Errorf(\"array default for %s requires exactly %d items, got %d\", target, base.Len(), source.Len())\n\t\t}\n\t}\n\tencoded, err := json.Marshal(value)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdecoded := reflect.New(target)\n\tif err := json.Unmarshal(encoded, decoded.Interface()); err != nil {\n\t\treturn fmt.Errorf(\"value is incompatible with %s: %w\", target, err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":614,"sourceCodeEnd":645,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/common/typed_compile_args.go#L614-L645","documentation":"valueAssignableTo checks that a default value can be assigned to the target Go type, and for fixed-size reflect.Array targets it requires the JSON representation of the default to have exactly the same length. This error reports the required length versus the actual length so declaration mistakes are caught at compile/validation time.","triggerScenarios":"Declaring an [N]T (fixed array) field with a default value whose marshaled JSON array has fewer or more than N elements, e.g. default of 2 items for a [3]string field.","commonSituations":"Changing the array size in the struct but not the default literal; copy-pasting a default between fields with different array lengths.","solutions":["Make the default JSON array contain exactly N items matching the declared array length","If the count is variable, change the field to a slice []T instead of a fixed array [N]T","Re-run the input validation test after resizing either side"],"exampleFix":"// before\ntype in struct{ Names [3]string `cli:\"default=[a,b]\"` }\n// after\ntype in struct{ Names [3]string `cli:\"default=[a,b,c]\"` }","handlingStrategy":"validation","validationCode":"func defaultFitsArray(def any, n int) error {\n\titems, ok := def.([]any)\n\tif !ok { return fmt.Errorf(\"default is not a list\") }\n\tif len(items) != n { return fmt.Errorf(\"need exactly %d items, got %d\", n, len(items)) }\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep array field sizes and default literals in sync; prefer slices when length varies","Comment the fixed length next to the default","Add a valueAssignableTo-style round-trip test for each default"],"tags":["go","arrays","json","default-value"],"backgroundTag":"array-length-mismatch","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}