cayleygraph/cayley · error

load anonymous field %s failed: %v

Error message

load anonymous field %s failed: %v

What it means

When a struct embeds an anonymous field, the loader recursively loads it into the embedded value. If that recursive loadToValue fails, the error is wrapped as "load anonymous field %s failed: %v", preserving the embedded field name and the underlying cause.

Source

Thrown at schema/loader.go:337

				}
			}
		}
	}
	for i := 0; i < rt.NumField(); i++ {
		select {
		case <-ctx.Done():
			return ctx.Err()
		default:
		}
		f := rt.Field(i)
		name := f.Name
		if err := checkFieldType(f.Type); err != nil {
			return err
		}
		df := dst.Field(i)
		if f.Anonymous {
			if err := l.loadToValue(ctx, df, depth, m, tagPref+name+"."); err != nil {
				return fmt.Errorf("load anonymous field %s failed: %v", f.Name, err)
			}
			continue
		}
		rules := fields[tagPref+name]
		if rules == nil {
			continue
		}
		arr, ok := m[tagPref+name]
		if !ok || len(arr) == 0 {
			continue
		}
		ft := f.Type
		native := isNative(ft)
		ptr := ft.Kind() == reflect.Ptr
		for ft.Kind() == reflect.Ptr || ft.Kind() == reflect.Slice {
			ft = ft.Elem()
			native = native || isNative(ft)
			switch ft.Kind() {

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Read the wrapped %v cause first — it names the real failure (struct expected, conversion failed, etc.).
  2. Ensure the anonymous field's type is a struct (or pointer to struct).
  3. Fix the embedded struct's field tags/types to match the stored quad values.

Example fix

// before
type Person struct {
	Name string // not embedded issue
	Meta // embedded interface -> fails
}
// after
type Person struct {
	Name string
	Meta MetaInfo // embedded struct type: type MetaInfo struct{ ... }
}
Defensive patterns

Strategy: try-catch

Validate before calling

for i := 0; i < reflect.TypeOf(dst).Elem().NumField(); i++ {
    f := reflect.TypeOf(dst).Elem().Field(i)
    if f.Anonymous && f.Type.Kind() != reflect.Struct && f.Type.Kind() != reflect.Ptr {
        return fmt.Errorf("embedded field %s must be struct-based", f.Name)
    }
}

Try / catch

if err := c.LoadTo(ctx, qs, &obj, id); err != nil {
    var wrapped string = err.Error()
    if strings.Contains(wrapped, "load anonymous field") {
        cause := strings.SplitN(wrapped, "failed: ", 2)
        // inspect cause[1] for the inner failure and fix the embedded struct
    }
    return err
}

Prevention

When it happens

Trigger: Loading into a struct with an embedded (anonymous) struct field whose own load fails — e.g. the embedded type is not a struct after deref, or its fields fail conversion.

Common situations: Embedding non-struct types (e.g. embedded interfaces or primitives) expected to be schema-mapped; the embedded struct's fields don't match stored quad values; renamed/removed schema tags after refactor.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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