{"record":{"id":"6ea0821719dbb0a7","repo":"cayleygraph/cayley","slug":"unexpected-type-for-int-field-t","errorCode":null,"errorMessage":"unexpected type for int field: %T","messagePattern":"unexpected type for int field: %T","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/nosql/quadstore.go","lineNumber":518,"sourceCode":"\tcase quad.Bool:\n\t\tdoc = nosql.Document{fldValBool: nosql.Bool(d)}\n\tcase quad.Time:\n\t\tdoc = nosql.Document{fldValTime: nosql.Time(time.Time(d).UTC())}\n\tdefault:\n\t\tencPb()\n\t}\n\treturn nosql.Document{fldValue: doc}\n}\n\nfunc asInt(v nosql.Value) (nosql.Int, error) {\n\tvar vi nosql.Int\n\tswitch v := v.(type) {\n\tcase nosql.Int:\n\t\tvi = v\n\tcase nosql.Float:\n\t\tvi = nosql.Int(v)\n\tdefault:\n\t\treturn 0, fmt.Errorf(\"unexpected type for int field: %T\", v)\n\t}\n\treturn vi, nil\n}\n\nfunc toQuadValue(opt *Traits, d nosql.Document) (quad.Value, error) {\n\tif len(d) == 0 {\n\t\treturn nil, nil\n\t}\n\tvar err error\n\t// prefer protobuf representation\n\tif v, ok := d[fldValPb]; ok {\n\t\tvar b []byte\n\t\tswitch v := v.(type) {\n\t\tcase nosql.String:\n\t\t\tb, err = base64.StdEncoding.DecodeString(string(v))\n\t\tcase nosql.Bytes:\n\t\t\tb = []byte(v)\n\t\tdefault:","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/nosql/quadstore.go#L500-L536","documentation":"asInt converts a NoSQL document field to an integer and throws this error when the stored value is neither nosql.Int nor nosql.Float. It indicates the value document contains a field of an unexpected Go type, typically meaning the stored data was written by an incompatible writer or the wrong field was read.","triggerScenarios":"checkQuadValid or toQuadValue reading a numeric field (fldValInt / size counters) that was stored as nosql.String, nosql.Bool, nosql.Time or nosql.Bytes instead of Int/Float — e.g. data written by a different Cayley version or backend encoding.","commonSituations":"Migrating a quadstore between backends or Cayley versions where value encodings changed (pre/post Number32 support); a corrupt or hand-edited document; reading a field name that collides with another value kind.","solutions":["Log %T of the offending value to see what was actually stored and where it was written from.","Re-export and re-load the quadstore data (cayley dump / load) so all values are re-encoded by the current version.","Check for version mismatch: data written by an older Cayley may need the documented migration steps.","Ensure the document actually belongs to the expected field (correct fldVal* key, not a mis-keyed read).","If writing custom tooling against the store, always encode numbers as nosql.Int or nosql.Float."],"exampleFix":"// before\ndefault:\n    return 0, fmt.Errorf(\"unexpected type for int field: %T\", v)\n// after\ncase nosql.String:\n    n, err := strconv.ParseInt(string(v), 10, 64)\n    if err != nil {\n        return 0, fmt.Errorf(\"int field not parseable: %w\", err)\n    }\n    vi = nosql.Int(n)\ndefault:\n    return 0, fmt.Errorf(\"unexpected type for int field: %T\", v)","handlingStrategy":"type-guard","validationCode":"// validate stored numeric fields before conversion\nfunc isIntLike(v nosql.Value) bool {\n    switch v.(type) {\n    case nosql.Int, nosql.Float:\n        return true\n    }\n    return false\n}","typeGuard":"func asIntSafe(v interface{}) (nosql.Int, bool) {\n    switch t := v.(type) {\n    case nosql.Int:\n        return t, true\n    case nosql.Float:\n        return nosql.Int(t), true\n    }\n    return 0, false\n}","tryCatchPattern":"n, err := asInt(doc[fldSize])\nif err != nil {\n    log.Printf(\"bad numeric field %T: %v\", doc[fldSize], err)\n    // re-import or repair the document instead of proceeding\n}","preventionTips":["Always write numbers as nosql.Int/nosql.Float in custom tooling","Re-import data after Cayley version upgrades","Never hand-edit store documents","Validate imported dumps with cayley dump/load round-trip"],"tags":["type-mismatch","nosql","data-corruption","cayley"],"backgroundTag":"type-mismatch","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}