-
Notifications
You must be signed in to change notification settings - Fork 269
/
executor.go
98 lines (86 loc) · 2.72 KB
/
executor.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
// Copyright 2022 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"database/sql/driver"
"encoding/json"
"reflect"
engineModel "github.com/pingcap/tiflow/engine/model"
"github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/label"
)
// LabelSet is a type alias for label.Set.
// It adds some extra methods for gorm to scan and convert values.
type LabelSet label.Set
// Value implements the driver.Valuer interface.
func (s LabelSet) Value() (driver.Value, error) {
data, err := json.Marshal(s)
if err != nil {
return nil, errors.Errorf("failed to marshal LabelSet: %v", err)
}
return string(data), nil
}
// ToMap converts a LabelSet to a plain map.
func (s LabelSet) ToMap() map[string]string {
return label.Set(s).ToMap()
}
// Scan implements the sql.Scanner interface.
func (s *LabelSet) Scan(rawInput interface{}) error {
*s = make(LabelSet)
if rawInput == nil {
return nil
}
// As different SQL drivers might treat the JSON value differently,
// we need to handle two cases where the JSON value is passed as a string
// and a byte slice respectively.
var bytes []byte
switch input := rawInput.(type) {
case string:
// SQLite is this case.
if len(input) == 0 {
return nil
}
bytes = []byte(input)
case []byte:
// MySQL is this case.
if len(input) == 0 {
return nil
}
bytes = input
default:
return errors.Errorf("failed to scan LabelSet. "+
"Expected string or []byte, got %s", reflect.TypeOf(rawInput))
}
if err := json.Unmarshal(bytes, s); err != nil {
return errors.Annotate(err, "failed to unmarshal LabelSet")
}
return nil
}
// Executor records the information of an executor.
type Executor struct {
Model
ID engineModel.ExecutorID `json:"id" gorm:"column:id;type:varchar(256) not null;uniqueIndex:uni_id"`
Name string `json:"name" gorm:"column:name;type:varchar(256) not null"`
Address string `json:"address" gorm:"column:address;type:varchar(256) not null"`
// Labels store the label set for each executor.
Labels LabelSet `json:"labels" gorm:"column:labels;type:json"`
}
// Map is used in gorm update.
func (e *Executor) Map() map[string]interface{} {
return map[string]interface{}{
"id": e.ID,
"name": e.Name,
"address": e.Address,
"labels": e.Labels,
}
}