forked from shu-bc/cypherx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shu-bc
committed
Jul 2, 2021
1 parent
5aab080
commit ef6f13d
Showing
5 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |