-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
executable file
·79 lines (67 loc) · 2.25 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package gen
import (
"fmt"
"reflect"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/mitchellh/mapstructure"
)
type CommentResultType struct {
EntityResultType
}
type Comment struct {
ID string `json:"id" gorm:"column:id;primary_key"`
Reference string `json:"reference" gorm:"column:reference;index:fetch"`
ReferenceID string `json:"referenceID" gorm:"column:referenceID;index:fetch"`
Text *string `json:"text" gorm:"column:text;type:text"`
UpdatedAt *time.Time `json:"updatedAt" gorm:"column:updatedAt"`
CreatedAt time.Time `json:"createdAt" gorm:"column:createdAt"`
UpdatedBy *string `json:"updatedBy" gorm:"column:updatedBy"`
CreatedBy *string `json:"createdBy" gorm:"column:createdBy"`
}
func (m *Comment) Is_Entity() {}
type CommentChanges struct {
ID string
Reference string
ReferenceID string
Text *string
UpdatedAt *time.Time
CreatedAt time.Time
UpdatedBy *string
CreatedBy *string
}
// used to convert map[string]interface{} to EntityChanges struct
func ApplyChanges(changes map[string]interface{}, to interface{}) error {
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
ErrorUnused: true,
TagName: "json",
Result: to,
ZeroFields: true,
// This is needed to get mapstructure to call the gqlgen unmarshaler func for custom scalars (eg Date)
DecodeHook: func(a reflect.Type, b reflect.Type, v interface{}) (interface{}, error) {
if b == reflect.TypeOf(time.Time{}) {
switch a.Kind() {
case reflect.String:
return time.Parse(time.RFC3339, v.(string))
case reflect.Float64:
return time.Unix(0, int64(v.(float64))*int64(time.Millisecond)), nil
case reflect.Int64:
return time.Unix(0, v.(int64)*int64(time.Millisecond)), nil
default:
return v, fmt.Errorf("Unable to parse date from %v", v)
}
}
if reflect.PtrTo(b).Implements(reflect.TypeOf((*graphql.Unmarshaler)(nil)).Elem()) {
resultType := reflect.New(b)
result := resultType.MethodByName("UnmarshalGQL").Call([]reflect.Value{reflect.ValueOf(v)})
err, _ := result[0].Interface().(error)
return resultType.Elem().Interface(), err
}
return v, nil
},
})
if err != nil {
return err
}
return dec.Decode(changes)
}