cayleygraph/cayley · error

field

Error message

field %s: %v

What it means

When setting a struct field from a graph value, DefaultConverter.SetValue failed. The loader wraps that failure as "field %s: %v" so the developer knows which struct field could not be populated and why (typically a type conversion failure).

Solutions

  1. Check the wrapped inner error for the exact conversion failure and align the struct field type with the stored quad value type.
  2. Register a custom converter (schema.RegisterConverter) for the custom/complex field type.
  3. Validate/normalize data in the store, or make the field a string/interface{} and convert manually after load.

Example fix

// before
type P struct{ Age int `quad:"age"` } // store holds "42" as string
// after
type P struct{ Age string `quad:"age"` }
// or register a converter:
schema.RegisterConverter(0, converterFunc(func(v quad.Value, out reflect.Value) (interface{}, error) {...}))
Defensive patterns

Strategy: try-catch

Validate before calling

// check field types vs expected quad value kinds before load
t := reflect.TypeOf(Person{})
f, _ := t.FieldByName("Age")
if f.Type.Kind() != reflect.String && f.Type.Kind() != reflect.Int {
    return errors.New("field type not convertible from stored quad value")
}

Try / catch

if err := c.LoadTo(ctx, qs, &obj, id); err != nil {
    if strings.Contains(err.Error(), "field ") {
        // extract field name from "field <name>: <cause>" and align types
        return fmt.Errorf("load conversion failed: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: A quad value's runtime type does not match the destination field type — e.g. an IRI or string in the graph being converted into an int/bool/float field, or a time field fed a non-time value.

Common situations: Schema drift: the graph stores strings but the struct field was changed to int; missing custom Converter for a custom type; loading RDF data with unexpected value kinds into strongly typed structs.

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/95d91c0be90990c4. Report an issue: GitHub.

Appendix: source

Thrown at schema/loader.go:396

				sv = reflect.New(ft).Elem()
				err := l.loadIteratorToDepth(ctx, sv, depth-1, iterator.NewFixed(fv))
				if err == errRequiredFieldIsMissing {
					continue
				} else if err != nil {
					return err
				}
			} else {
				fv, err := l.qs.NameOf(fv)
				if err != nil {
					return err
				}
				if fv == nil {
					continue
				}
				sv = reflect.ValueOf(fv)
			}
			if err := DefaultConverter.SetValue(df, sv); err != nil {
				return fmt.Errorf("field %s: %v", f.Name, err)
			}
		}
	}
	return nil
}

func (l *loader) iteratorForType(ctx context.Context, root iterator.Shape, rt reflect.Type, rootOnly bool) (iterator.Shape, error) {
	p, err := l.makePathForType(rt, "", rootOnly)
	if err != nil {
		return nil, err
	}
	return l.iteratorFromPath(ctx, root, p)
}

func mergeMap(dst map[string][]graph.Ref, m map[string]graph.Ref) {
loop:
	for k, v := range m {
		sl := dst[k]

View on GitHub (pinned to 81dcd7d73e)