Dragonfly2
141 строка · 3.1 Кб
1/*
2* Copyright 2020 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package models
18
19import (
20"database/sql/driver"
21"encoding/json"
22"errors"
23"fmt"
24"time"
25
26"gorm.io/gorm"
27"gorm.io/gorm/schema"
28"gorm.io/plugin/soft_delete"
29)
30
31type BaseModel struct {
32ID uint `gorm:"primarykey;comment:id" json:"id"`
33CreatedAt time.Time `gorm:"column:created_at;type:timestamp;default:current_timestamp" json:"created_at"`
34UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;default:current_timestamp" json:"updated_at"`
35IsDel soft_delete.DeletedAt `gorm:"softDelete:flag;comment:soft delete flag" json:"is_del"`
36}
37
38func Paginate(page, perPage int) func(db *gorm.DB) *gorm.DB {
39return func(db *gorm.DB) *gorm.DB {
40offset := (page - 1) * perPage
41return db.Offset(offset).Limit(perPage)
42}
43}
44
45type JSONMap map[string]any
46
47func (m JSONMap) Value() (driver.Value, error) {
48if m == nil {
49return nil, nil
50}
51ba, err := m.MarshalJSON()
52return string(ba), err
53}
54
55func (m *JSONMap) Scan(val any) error {
56var ba []byte
57switch v := val.(type) {
58case []byte:
59ba = v
60case string:
61ba = []byte(v)
62default:
63return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", val))
64}
65t := map[string]any{}
66err := json.Unmarshal(ba, &t)
67*m = JSONMap(t)
68return err
69}
70
71func (m JSONMap) MarshalJSON() ([]byte, error) {
72if m == nil {
73return []byte("null"), nil
74}
75t := (map[string]any)(m)
76return json.Marshal(t)
77}
78
79func (m *JSONMap) UnmarshalJSON(b []byte) error {
80t := map[string]any{}
81err := json.Unmarshal(b, &t)
82*m = JSONMap(t)
83return err
84}
85
86func (m JSONMap) GormDataType() string {
87return "jsonmap"
88}
89
90func (JSONMap) GormDBDataType(db *gorm.DB, field *schema.Field) string {
91return "text"
92}
93
94type Array []string
95
96func (a Array) Value() (driver.Value, error) {
97if a == nil {
98return nil, nil
99}
100ba, err := a.MarshalJSON()
101return string(ba), err
102}
103
104func (a *Array) Scan(val any) error {
105var ba []byte
106switch v := val.(type) {
107case []byte:
108ba = v
109case string:
110ba = []byte(v)
111default:
112return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", val))
113}
114t := []string{}
115err := json.Unmarshal(ba, &t)
116*a = Array(t)
117return err
118}
119
120func (a Array) MarshalJSON() ([]byte, error) {
121if a == nil {
122return []byte("null"), nil
123}
124t := ([]string)(a)
125return json.Marshal(t)
126}
127
128func (a *Array) UnmarshalJSON(b []byte) error {
129t := []string{}
130err := json.Unmarshal(b, &t)
131*a = Array(t)
132return err
133}
134
135func (Array) GormDataType() string {
136return "array"
137}
138
139func (Array) GormDBDataType(db *gorm.DB, field *schema.Field) string {
140return "text"
141}
142