Skip to content

Commit

Permalink
mapper type
Browse files Browse the repository at this point in the history
  • Loading branch information
shu-bc committed Jul 2, 2021
1 parent 5aab080 commit ef6f13d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
33 changes: 32 additions & 1 deletion cypherx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cypherx

import (
"fmt"
"reflect"

"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)

Expand Down Expand Up @@ -39,6 +42,34 @@ func (db *DB) SendQuery(cypher string, params map[string]interface{}) (neo4j.Res
return res, nil
}

func (db *DB) Get(dest interface{}, cypher string, params map[string]interface{}) {
func (db *DB) Get(
dest interface{},
cypher string,
params map[string]interface{},
) error {
rv := reflect.ValueOf(dest)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("dest must be a non-null pointer\n")
}

session := db.driver.NewSession(neo4j.SessionConfig{})
defer session.Close()

res, err := session.Run(cypher, params)
if err != nil {
return fmt.Errorf("cypher execution failure: %w\n", err)
}

var record *neo4j.Record
record, err = res.Single()
if err != nil {
return fmt.Errorf("result should contain at least one record: %w\n", err)
}

_, ok := record.GetByIndex(0).(neo4j.Node)
if !ok {
return fmt.Errorf("type neo4j.Node assertion failure, unexpected result type\n")
}

return nil
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/shu-bc/cypherx

go 1.16

require github.com/neo4j/neo4j-go-driver/v4 v4.3.1
require (
github.com/ettle/strcase v0.1.1
github.com/neo4j/neo4j-go-driver/v4 v4.3.1
github.com/stretchr/testify v1.5.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ettle/strcase v0.1.1 h1:htFueZyVeE1XNnMEfbqp5r67qAN/4r6ya1ysq8Q+Zcw=
github.com/ettle/strcase v0.1.1/go.mod h1:hzDLsPC7/lwKyBOywSHEP89nt2pDgdy+No1NBA9o9VY=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
Expand Down Expand Up @@ -28,8 +31,10 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je41yGY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -81,4 +86,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
50 changes: 50 additions & 0 deletions mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cypherx

import (
"reflect"
"strings"

"github.com/ettle/strcase"
)

type Mapper struct {
}

func (m Mapper) Map(dest interface{}, props map[string]interface{}) error {
rt := reflect.TypeOf(dest).Elem()

for i := 0; i < rt.NumField(); i++ {
tf := rt.Field(i)
tag := tf.Tag.Get("neo4j")
propName := strings.Split(tag, ",")[0]
if propName == "" {
propName = strcase.ToSnake(tf.Name)
}

pv, ok := props[propName]
if !ok {
continue
}

vf := reflect.ValueOf(dest).Elem().Field(i)
if err := m.fillField(vf, pv); err != nil {
return err
}
}

return nil
}

func (m Mapper) fillField(vf reflect.Value, pv interface{}) error {
switch vf.Kind() {
case reflect.String:
if s, ok := pv.(string); ok {
vf.SetString(s)
}
case reflect.Int:
if reflect.ValueOf(pv).Kind() == reflect.Int {
vf.SetInt(reflect.ValueOf(pv).Int())
}
}
return nil
}
27 changes: 27 additions & 0 deletions mapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cypherx_test

import (
"testing"

"github.com/shu-bc/cypherx"
"github.com/stretchr/testify/assert"
)

type Person struct {
Name string `neo4j:"name"`
Age int
}

func TestMap(t *testing.T) {
m := cypherx.Mapper{}
props := map[string]interface{}{
"name": "test",
"age": 3,
}
p := &Person{}
err := m.Map(p, props)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, &Person{Name: "test", Age: 3}, p)
}

0 comments on commit ef6f13d

Please sign in to comment.