/
cdc_config_wraper.go
80 lines (68 loc) · 2.11 KB
/
cdc_config_wraper.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
// Copyright 2020 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 v1alpha1
import (
stdjson "encoding/json"
"github.com/pingcap/errors"
"github.com/pingcap/tidb-operator/pkg/apis/util/config"
"k8s.io/apimachinery/pkg/util/json"
)
var _ stdjson.Marshaler = &CDCConfigWraper{}
var _ stdjson.Unmarshaler = &CDCConfigWraper{}
// NewCDCConfig returns an empty config structure
func NewCDCConfig() *CDCConfigWraper {
return &CDCConfigWraper{
GenericConfig: config.New(map[string]interface{}{}),
}
}
// CDCConfigWraper simply wrapps a GenericConfig
type CDCConfigWraper struct {
*config.GenericConfig `json:",inline"`
}
// MarshalJSON implements stdjson.Marshaler interface.
func (c *CDCConfigWraper) MarshalJSON() ([]byte, error) {
toml, err := c.GenericConfig.MarshalTOML()
if err != nil {
return nil, errors.AddStack(err)
}
return json.Marshal(string(toml))
}
// UnmarshalJSON implements stdjson.Unmarshaler interface.
// If the data is a object, we must use the Deprecated TiCDCConfig to Unmarshal
// for compatibility, if we use a map[string]interface{} to Unmarshal directly,
// we can not distinct the type between integer and float for toml.
func (c *CDCConfigWraper) UnmarshalJSON(data []byte) error {
deprecated := new(TiCDCConfig)
var err error
c.GenericConfig, err = unmarshalJSON(data, deprecated)
return err
}
func (c *CDCConfigWraper) MarshalTOML() ([]byte, error) {
if c == nil {
return nil, nil
}
return c.GenericConfig.MarshalTOML()
}
func (c *CDCConfigWraper) OnlyOldItems() bool {
for k := range c.GenericConfig.Inner() {
switch k {
case "tz":
case "gc-ttl":
case "log-level":
case "log-file":
default:
return false
}
}
return true
}