cayleygraph/cayley · error

unexpected token: %T

Error message

unexpected token: %T

What it means

NameOf resolves a stored node token (NodeHash, refs.ValueHash, or a quad.Value wrapper) back to its quad.Value. If the argument's concrete type isn't one of the recognized token types, this error is returned. It usually indicates a hash-vs-value mismatch in iterator plumbing.

Source

Thrown at graph/sql/quadstore.go:632

	return nt.Time, nil
}

func (qs *QuadStore) NameOf(v graph.Ref) (quad.Value, error) {
	if v == nil {
		return nil, nil
	} else if v, ok := v.(refs.PreFetchedValue); ok {
		return v.NameOf(), nil
	}
	var hash NodeHash
	switch h := v.(type) {
	case refs.PreFetchedValue:
		return h.NameOf(), nil
	case NodeHash:
		hash = h
	case refs.ValueHash:
		hash = NodeHash{h}
	default:
		return nil, fmt.Errorf("unexpected token: %T", v)
	}
	if !hash.Valid() {
		return nil, nil
	}
	if val, ok := qs.ids.Get(hash.String()); ok {
		return val.(quad.Value), nil
	}
	query := `SELECT
		value,
		value_string,
		datatype,
		language,
		iri,
		bnode,
		value_int,
		value_bool,
		value_float,
		value_time

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Ensure only NodeHash or refs.ValueHash values are passed to NameOf
  2. If you hold a quad.Value, use the appropriate hash conversion (e.g. qs.ValueOf) before lookup
  3. Check the type of the token with %T before calling NameOf
  4. Update custom iterator code to match the current QuadStore token API

Example fix

// before
val, _ := qs.NameOf(quad.IRI("http://example.com"))
// after
h := qs.ValueOf(quad.IRI("http://example.com"))
val, _ := qs.NameOf(h)
Defensive patterns

Strategy: type-guard

Validate before calling

func isNodeToken(v interface{}) bool {
    switch v.(type) {
    case graph.NodeHash, refs.ValueHash, quad.Value:
        return true
    }
    return false
}

Type guard

switch t := v.(type) {
case NodeHash, refs.ValueHash:
    // safe to call NameOf
default:
    return fmt.Errorf("NameOf requires NodeHash/refs.ValueHash, got %T", t)
}

Try / catch

val, err := qs.NameOf(tok)
if err != nil && strings.Contains(err.Error(), "unexpected token") {
    tok = qs.ValueOf(v) // convert then retry
    val, err = qs.NameOf(tok)
}

Prevention

When it happens

Trigger: Passing a value of an unexpected type (e.g. a plain string, quad.IRI, or a different hash type) to qs.NameOf instead of NodeHash/refs.ValueHash/wrapped values; custom iterators feeding unsupported token types.

Common situations: Writing custom iterators or resolvers over the SQL store that emit raw quad values where hashes are expected; API drift between quadstore versions changing the token type contract.

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/3d1aad70d81dd43f. Report an issue: GitHub.