neo4j / neo4j-go-driver

Neo4j Bolt Driver for Go
Apache License 2.0
488 stars 68 forks source link

Type error when retrieving a datetime value from database #486

Closed knotseaborg closed 1 year ago

knotseaborg commented 1 year ago

The following error was found while retrieving a datetime value from the database panic: expected value to have type dbtype.Time but found type time.Time

Neo4j Version: 5.7.0 Community Neo4j Mode: Single instance Driver version: Go driver v5.8.1 Go version: go1.20.4 linux/amd64 Operating System: Linux Mint 21.1 Vera 64-bit

Steps to reproduce

  1. Start Neo4j locally
  2. Create a node with datetime value create (n:Node{dateTimeLabel: datetime("2016-11-22T18:59:00.000+0900")});
  3. Attempting to retrieve node property triggers an error
    result, err := neo4j.ExecuteQuery(ctx, driver, "MATCH (n:Node) RETURN n", map[string]any{}, neo4j.EagerResultTransformer)
    if err != nil {
    panic(err)
    }
    node, _, err := neo4j.GetRecordValue[neo4j.Node](result.Records[0], "n")
    if err != nil {
    panic(err)
    }
    dateTimeVal, err := neo4j.GetProperty[neo4j.Time](node, "dateTimeLabel")
    if err != nil { //Non-nil error
    panic(err)
    }

    As an alternative, when neo4j.Time is replaced with time.Time, it does not match the type specification and an error is returned.

    dateTimeVal, err := neo4j.GetProperty[time.Time](node, "dateTimeLabel")
    if err != nil { //Non-nil error
    panic(err)
    }

    Expected behavior

    Perhaps the node property dateTimeLabel should be of type dbtype.Time to avoid returning the error?

    Actual behavior

    Node property dateTimeLabel is of type time.Time

    Stacktrace

    For

    dateTimeVal, err := neo4j.GetProperty[neo4j.Time](node, "dateTimeLabel")
    if err != nil { //Non-nil error
    panic(err)
    }
    
    panic: expected value to have type dbtype.Time but found type time.Time

goroutine 1 [running]: main.main() /home/--------- exit status 2

For

dateTimeVal, err := neo4j.GetPropertytime.Time if err != nil { //Non-nil error panic(err) }

./neo4j.go:131:38: time.Time does not satisfy neo4j.PropertyValue (time.Time missing in bool | int64 | float64 | string | github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype.Point2D | github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype.Point3D | github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype.Date | github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype.LocalTime | github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype.LocalDateTime | github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype.Time | github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype.Duration | []byte | []any


### Quick dirty fix
added `time.Time` to `type PropertyValue interface` in neo4j/graph.go
fbiville commented 1 year ago

This is definitely a bug in the type constraint definition, thanks a lot for the report, a fix is on its way!

knotseaborg commented 1 year ago

No problem! Glad I could help.

fbiville commented 1 year ago

The fix is going to be released towards the end of the month, with v5.9.0 FYI.

fbiville commented 1 year ago

@knotseaborg 5.9.0 is now out with this fix: https://github.com/neo4j/neo4j-go-driver/wiki/5.x-changelog#v590

knotseaborg commented 1 year ago

Thanks for the update!