Skip to content

Commit

Permalink
fix offset bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fancl committed Nov 16, 2022
1 parent 3577bd1 commit 5f85343
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 12 additions & 1 deletion document.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func (document Document) Unmarshal(refType reflect.Type) (val reflect.Value, err

func toDocument(v interface{}) Document {
var (
err error
pos int
buf []byte
fieldName string
refValue reflect.Value
refType reflect.Type
Expand All @@ -69,12 +71,21 @@ func toDocument(v interface{}) Document {
if fieldName == "-" {
continue
}
if !structField.IsExported() {
continue
}
if pos = strings.IndexByte(fieldName, ','); pos > -1 {
fieldName = fieldName[:i]
fieldName = fieldName[:pos]
}
if fieldName == "" {
fieldName = structField.Name
}
if marshaler, ok := fieldValue.Interface().(json.Marshaler); ok {
if buf, err = marshaler.MarshalJSON(); err == nil {
doc[fieldName] = strings.Trim(string(buf), "\"")
continue
}
}
switch fieldValue.Kind() {
case reflect.Struct:
doc[fieldName] = toDocument(fieldValue.Interface())
Expand Down
11 changes: 7 additions & 4 deletions examples/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"fmt"
"github.com/uole/jsondb"
"os"
"time"
)

type User struct {
ID int `json:"id"`
Name string `json:"name"`
ID int `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
}

func (u *User) TableName() string {
Expand All @@ -29,8 +31,9 @@ func main() {
fmt.Println(err)
}
if err = db.Insert(context.Background(), &User{
ID: 1,
Name: "Hello",
ID: 1,
Name: "Hello",
CreatedAt: time.Now(),
}); err != nil {
fmt.Println(err)
}
Expand Down

0 comments on commit 5f85343

Please sign in to comment.