-
Notifications
You must be signed in to change notification settings - Fork 267
/
models.go
142 lines (124 loc) · 3.09 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/plugin/soft_delete"
)
type Model struct {
ID uint `gorm:"primarykey;comment:id" json:"id"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;default:current_timestamp" json:"updated_at"`
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag;comment:soft delete flag" json:"-"`
}
func Paginate(page, perPage int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
offset := (page - 1) * perPage
return db.Offset(offset).Limit(perPage)
}
}
type (
JSONMap map[string]any
Array []string
)
func (m JSONMap) Value() (driver.Value, error) {
if m == nil {
return nil, nil
}
ba, err := m.MarshalJSON()
return string(ba), err
}
func (a Array) Value() (driver.Value, error) {
if a == nil {
return nil, nil
}
ba, err := a.MarshalJSON()
return string(ba), err
}
func (m *JSONMap) Scan(val any) error {
var ba []byte
switch v := val.(type) {
case []byte:
ba = v
case string:
ba = []byte(v)
default:
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", val))
}
t := map[string]any{}
err := json.Unmarshal(ba, &t)
*m = JSONMap(t)
return err
}
func (a *Array) Scan(val any) error {
var ba []byte
switch v := val.(type) {
case []byte:
ba = v
case string:
ba = []byte(v)
default:
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", val))
}
t := []string{}
err := json.Unmarshal(ba, &t)
*a = Array(t)
return err
}
func (m JSONMap) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
t := (map[string]any)(m)
return json.Marshal(t)
}
func (a Array) MarshalJSON() ([]byte, error) {
if a == nil {
return []byte("null"), nil
}
t := ([]string)(a)
return json.Marshal(t)
}
func (m *JSONMap) UnmarshalJSON(b []byte) error {
t := map[string]any{}
err := json.Unmarshal(b, &t)
*m = JSONMap(t)
return err
}
func (a *Array) UnmarshalJSON(b []byte) error {
t := []string{}
err := json.Unmarshal(b, &t)
*a = Array(t)
return err
}
func (m JSONMap) GormDataType() string {
return "jsonmap"
}
func (JSONMap) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return "text"
}
func (Array) GormDataType() string {
return "array"
}
func (Array) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return "text"
}