-
Notifications
You must be signed in to change notification settings - Fork 143
/
model_wrapper.go
154 lines (140 loc) · 3.6 KB
/
model_wrapper.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
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2023 The PipeCD 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 mysql
import (
"encoding/json"
"fmt"
"github.com/pipe-cd/pipecd/pkg/model"
)
// wrapModel attaches an extra field named `_extra` to JSON data.
// Currently, on MySQL v8, functional indexes are not hit on `LIKE` queries,
// this added `_extra` field will be shadowed by table column `extra` so that
// we could create indexes (FULLTEXT or normal) on that column for search features.
// ref: https://github.com/pipe-cd/pipecd/blob/master/docs/rfcs/0003-sql-datastore.md#text-search-operations-on-specific-json-field
func wrapModel(entity interface{}) (interface{}, error) {
switch e := entity.(type) {
case *model.Project:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &project{
Project: *e,
Extra: e.Id,
}, nil
case *model.Application:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &application{
Application: *e,
Extra: e.Name,
}, nil
case *model.Command:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &command{
Command: *e,
Extra: e.Id,
}, nil
case *model.Deployment:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &deployment{
Deployment: *e,
Extra: e.ApplicationName,
}, nil
case *model.Piped:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &piped{
Piped: *e,
Extra: e.Name,
}, nil
case *model.APIKey:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &apiKey{
APIKey: *e,
Extra: e.Name,
}, nil
case *model.Event:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &event{
Event: *e,
Extra: e.Name,
}, nil
case *model.DeploymentChain:
if e == nil {
return nil, fmt.Errorf("nil entity given")
}
return &deploymentChain{
DeploymentChain: *e,
Extra: e.Id,
}, nil
default:
return nil, fmt.Errorf("%T is not supported", e)
}
}
func encodeJSONValue(entity interface{}) (string, error) {
wrapper, err := wrapModel(entity)
if err != nil {
return "", err
}
encodedEntity, err := json.Marshal(wrapper)
if err != nil {
return "", err
}
return string(encodedEntity), nil
}
func decodeJSONValue(val string, target interface{}) error {
return json.Unmarshal([]byte(val), target)
}
type project struct {
model.Project `json:",inline"`
Extra string `json:"_extra"`
}
type application struct {
model.Application `json:",inline"`
Extra string `json:"_extra"`
}
type command struct {
model.Command `json:",inline"`
Extra string `json:"_extra"`
}
type deployment struct {
model.Deployment `json:",inline"`
Extra string `json:"_extra"`
}
type piped struct {
model.Piped `json:",inline"`
Extra string `json:"_extra"`
}
type apiKey struct {
model.APIKey `json:",inline"`
Extra string `json:"_extra"`
}
type event struct {
model.Event `json:",inline"`
Extra string `json:"_extra"`
}
type deploymentChain struct {
model.DeploymentChain `json:",inline"`
Extra string `json:"_extra"`
}