{"record":{"id":"3ee9126ebaf3bf15","repo":"larksuite/cli","slug":"value-is-incompatible-with-s-w","errorCode":null,"errorMessage":"value is incompatible with %s: %w","messagePattern":"value is incompatible with (.+?): %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/common/typed_compile_args.go","lineNumber":641,"sourceCode":"\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":623,"sourceCodeEnd":645,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/common/typed_compile_args.go#L623-L645","documentation":"valueAssignableTo round-trips the default value through JSON (Marshal then Unmarshal into a reflect.New(target)) to prove it is compatible with the declared target type. If Unmarshal fails, this error wraps the JSON error with the target type name, indicating the default's shape does not fit the field's Go type.","triggerScenarios":"Assigning a default like a string to an int field, an object to a scalar, or a value that cannot unmarshal into a named type with a custom UnmarshalJSON rejecting it.","commonSituations":"Type drift after refactoring a field's type without updating its default; defaults written as untyped JSON strings/numbers that do not fit the declared Go type.","solutions":["Align the default value with the field's Go type (e.g. numeric default for int fields, array for slices)","Check the wrapped json error (%w cause) for the exact offset/type mismatch","If the field uses a named type, ensure its JSON unmarshal accepts the default"],"exampleFix":"// before\ntype in struct{ Count int `cli:\"default=many\"` }\n// after\ntype in struct{ Count int `cli:\"default=3\"` }","handlingStrategy":"type-guard","validationCode":"func defaultAssignable(value any, target reflect.Type) error {\n\tencoded, err := json.Marshal(value)\n\tif err != nil { return err }\n\treturn json.Unmarshal(encoded, reflect.New(target).Interface())\n}","typeGuard":"func canUnmarshalAs(value any, target reflect.Type) bool {\n\tencoded, err := json.Marshal(value)\n\tif err != nil { return false }\n\treturn json.Unmarshal(encoded, reflect.New(target).Interface()) == nil\n}","tryCatchPattern":"if err := valueAssignableTo(def, fieldType); err != nil {\n\tvar jsonErr *json.UnmarshalTypeError\n\tif errors.As(err, &jsonErr) { log.Fatalf(\"default %v does not fit %s at %s\", def, jsonErr.Type, jsonErr.Field) }\n\treturn err\n}","preventionTips":["Update defaults whenever a field's type changes","Prefer typed Go values over raw JSON strings for defaults","Add a round-trip marshal/unmarshal test per default value"],"tags":["go","json","type-mismatch","default-value"],"backgroundTag":"type-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"}