github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/parser/model/str.go (about) 1 // Copyright 2022 zGraph Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package model 16 17 import ( 18 "encoding/json" 19 "strings" 20 21 "github.com/pingcap/errors" 22 ) 23 24 // CIStr is case-insensitive string. 25 type CIStr struct { 26 O string `json:"O"` // Original string. 27 L string `json:"L"` // Lower case string. 28 } 29 30 // String implements fmt.Stringer interface. 31 func (cis CIStr) String() string { 32 return cis.O 33 } 34 35 // Equal reports whether `cis` is equal to `other` in a case-insensitive way. 36 func (cis CIStr) Equal(other CIStr) bool { 37 return cis.L == other.L 38 } 39 40 // IsEmpty reports whether the CIStr is an empty string 41 func (cis CIStr) IsEmpty() bool { 42 return cis.O == "" 43 } 44 45 // NewCIStr creates a new CIStr. 46 func NewCIStr(s string) (cs CIStr) { 47 cs.O = s 48 cs.L = strings.ToLower(s) 49 return 50 } 51 52 // UnmarshalJSON implements the user defined unmarshal method. 53 // CIStr can be unmarshaled from a single string, so PartitionDefinition.Name 54 // in this change https://github.com/pingcap/tidb/pull/6460/files would be 55 // compatible during TiDB upgrading. 56 func (cis *CIStr) UnmarshalJSON(b []byte) error { 57 type T CIStr 58 if err := json.Unmarshal(b, (*T)(cis)); err == nil { 59 return nil 60 } 61 62 // Unmarshal CIStr from a single string. 63 err := json.Unmarshal(b, &cis.O) 64 if err != nil { 65 return errors.Trace(err) 66 } 67 cis.L = strings.ToLower(cis.O) 68 return nil 69 }