cayleygraph/cayley · error

anonymous fields of type %v are not supported

Error message

anonymous fields of type %v are not supported

What it means

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.

Source

Thrown at schema/schema.go:287

func anonFieldType(fld reflect.StructField) (reflect.Type, bool) {
	ft := fld.Type
	if ft.Kind() == reflect.Ptr {
		ft = ft.Elem()
	}
	if ft.Kind() == reflect.Struct {
		return ft, true
	}
	return ft, false
}

func (c *Config) rulesForStructTo(out fieldRules, pref string, rt reflect.Type) error {
	for i := 0; i < rt.NumField(); i++ {
		f := rt.Field(i)
		name := f.Name
		if f.Anonymous {
			if ft, ok := anonFieldType(f); !ok {
				return fmt.Errorf("anonymous fields of type %v are not supported", ft)
			} else if err := c.rulesForStructTo(out, pref+name+".", ft); err != nil {
				return err
			}
			continue
		}
		rules, err := c.fieldRule(f)
		if err != nil {
			return err
		}
		if rules != nil {
			out[pref+name] = rules
		}
	}
	return nil
}

// rulesFor
//

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Replace the embedded non-struct type with a named field
  2. If it is an embedded struct pointer, keep it as *Struct (that is supported)
  3. Ignore the field from schema mapping via the ignore tag

Example fix

// before
type Doc struct {
    string
}
// after
type Doc struct {
    Value string
}
Defensive patterns

Strategy: validation

Validate before calling

func hasBadEmbedded(v interface{}) bool {
    t := reflect.TypeOf(v)
    if t.Kind() == reflect.Ptr { t = t.Elem() }
    for i := 0; i < t.NumField(); i++ {
        f := t.Field(i)
        if !f.Anonymous { continue }
        ft := f.Type
        for ft.Kind() == reflect.Ptr { ft = ft.Elem() }
        if ft.Kind() != reflect.Struct { return true }
    }
    return false
}

Try / catch

if err := schema.Generate(c, Doc{}); err != nil {
    if strings.Contains(err.Error(), "anonymous fields") {
        // replace embedded non-struct with a named field
    }
    return err
}

Prevention

When it happens

Trigger: Calling Generate on a struct with an embedded non-struct type, e.g. embedding a string, time.Time via interface, or an array directly.

Common situations: Embedding primitive types for promotion (common Go idiom), embedding interfaces, or embedding protobuf/message types that are not plain structs.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/aa25a77c1acce5b4. Report an issue: GitHub.