github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/configuration/core/config_patch_option.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package core 21 22 import ( 23 "bytes" 24 "reflect" 25 ) 26 27 func compareWithConfig(left, right interface{}, option CfgOption) (bool, error) { 28 switch option.Type { 29 case CfgRawType: 30 if !typeMatch([]byte{}, left, right) { 31 return false, MakeError("invalid []byte data type!") 32 } 33 return bytes.Equal(left.([]byte), right.([]byte)), nil 34 case CfgLocalType: 35 if !typeMatch("", left, right) { 36 return false, MakeError("invalid string data type!") 37 } 38 return left.(string) == right.(string), nil 39 case CfgCmType, CfgTplType: 40 if !typeMatch(&ConfigResource{}, left, right) { 41 return false, MakeError("invalid data type!") 42 } 43 return left.(*ConfigResource) == right.(*ConfigResource), nil 44 default: 45 return false, MakeError("not supported config type to compare") 46 } 47 } 48 49 func withOption(option CfgOption, data interface{}) CfgOption { 50 op := option 51 switch option.Type { 52 case CfgRawType: 53 op.RawData = data.([]byte) 54 case CfgLocalType: 55 op.Path = data.(string) 56 case CfgCmType, CfgTplType: 57 op.ConfigResource = data.(*ConfigResource) 58 } 59 return op 60 } 61 62 func typeMatch(expected interface{}, values ...interface{}) bool { 63 matcher := newMatcherWithType(expected) 64 for _, v := range values { 65 if ok, err := matcher.match(v); !ok || err != nil { 66 return false 67 } 68 } 69 return true 70 } 71 72 type typeMatcher struct { 73 expected interface{} 74 } 75 76 func newMatcherWithType(expected interface{}) typeMatcher { 77 return typeMatcher{ 78 expected: expected, 79 } 80 } 81 82 func (matcher *typeMatcher) match(actual interface{}) (success bool, err error) { 83 switch { 84 case actual == nil && matcher.expected == nil: 85 return false, MakeError("type is <nil> to <nil>.") 86 case matcher.expected == nil: 87 return false, MakeError("expected type is <nil>.") 88 case actual == nil: 89 return false, nil 90 } 91 92 actualType := reflect.TypeOf(actual) 93 expectedType := reflect.TypeOf(matcher.expected) 94 return actualType.AssignableTo(expectedType), nil 95 }