{"record":{"id":"aa25a77c1acce5b4","repo":"cayleygraph/cayley","slug":"anonymous-fields-of-type-v-are-not-supported","errorCode":null,"errorMessage":"anonymous fields of type %v are not supported","messagePattern":"anonymous fields of type (.+?) are not supported","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"schema/schema.go","lineNumber":287,"sourceCode":"\nfunc anonFieldType(fld reflect.StructField) (reflect.Type, bool) {\n\tft := fld.Type\n\tif ft.Kind() == reflect.Ptr {\n\t\tft = ft.Elem()\n\t}\n\tif ft.Kind() == reflect.Struct {\n\t\treturn ft, true\n\t}\n\treturn ft, false\n}\n\nfunc (c *Config) rulesForStructTo(out fieldRules, pref string, rt reflect.Type) error {\n\tfor i := 0; i < rt.NumField(); i++ {\n\t\tf := rt.Field(i)\n\t\tname := f.Name\n\t\tif f.Anonymous {\n\t\t\tif ft, ok := anonFieldType(f); !ok {\n\t\t\t\treturn fmt.Errorf(\"anonymous fields of type %v are not supported\", ft)\n\t\t\t} else if err := c.rulesForStructTo(out, pref+name+\".\", ft); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t\trules, err := c.fieldRule(f)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif rules != nil {\n\t\t\tout[pref+name] = rules\n\t\t}\n\t}\n\treturn nil\n}\n\n// rulesFor\n//","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/schema/schema.go#L269-L305","documentation":"rulesForStructTo encounters an anonymous (embedded) field whose type it cannot handle. anonFieldType only accepts embedded pointers-to-struct and struct types; any other embedded type (interfaces, basic types, arrays) fails and generation aborts, reporting the unsupported type.","triggerScenarios":"Calling Generate on a struct with an embedded non-struct type, e.g. embedding a string, time.Time via interface, or an array directly.","commonSituations":"Embedding primitive types for promotion (common Go idiom), embedding interfaces, or embedding protobuf/message types that are not plain structs.","solutions":["Replace the embedded non-struct type with a named field","If it is an embedded struct pointer, keep it as *Struct (that is supported)","Ignore the field from schema mapping via the ignore tag"],"exampleFix":"// before\ntype Doc struct {\n    string\n}\n// after\ntype Doc struct {\n    Value string\n}","handlingStrategy":"validation","validationCode":"func hasBadEmbedded(v interface{}) bool {\n    t := reflect.TypeOf(v)\n    if t.Kind() == reflect.Ptr { t = t.Elem() }\n    for i := 0; i < t.NumField(); i++ {\n        f := t.Field(i)\n        if !f.Anonymous { continue }\n        ft := f.Type\n        for ft.Kind() == reflect.Ptr { ft = ft.Elem() }\n        if ft.Kind() != reflect.Struct { return true }\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":"if err := schema.Generate(c, Doc{}); err != nil {\n    if strings.Contains(err.Error(), \"anonymous fields\") {\n        // replace embedded non-struct with a named field\n    }\n    return err\n}","preventionTips":["Only embed structs or pointers to structs","Convert embedded primitives/interfaces to named fields","Generate the schema in a unit test covering every mapped type"],"tags":["schema","reflection","embedded-fields"],"backgroundTag":"unsupported-operation","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}