github.com/matrixorigin/matrixone@v1.2.0/pkg/container/types/enum.go (about) 1 // Copyright 2021 Matrix Origin 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 types 16 17 import ( 18 "strconv" 19 "strings" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 ) 23 24 func ParseIntToEnum(input int64) (Enum, error) { 25 if input < 1 || input > MaxEnumLen { 26 return Enum(0), moerr.NewInvalidInputNoCtx("invalid enum value %d", input) 27 } 28 return Enum(input), nil 29 } 30 31 // ParseEnum return item index with item name or value. 32 func ParseEnum(enumStr string, name string) (uint16, error) { 33 if len(enumStr) == 0 { 34 return 0, moerr.NewInternalErrorNoCtx("convert to MySQL enum failed: enum define is empty %v", enumStr) 35 } 36 elems := strings.Split(enumStr, ",") 37 enumName, err := parseEnumName(elems, name) 38 if err == nil { 39 return enumName, nil 40 } 41 // if name doesn't exist, it's maybe an integer 42 num, err := strconv.ParseUint(name, 0, 64) 43 if err == nil { 44 return parseEnumValue(elems, uint16(num)) 45 } 46 47 return 0, moerr.NewInternalErrorNoCtx("convert to MySQL enum failed: item %s is not in enum %v", name, elems) 48 } 49 50 // ParseEnumName return item index with item name. 51 func ParseEnumName(enumStr string, name string) (uint16, error) { 52 if len(enumStr) == 0 { 53 return 0, moerr.NewInternalErrorNoCtx("convert to MySQL enum failed: enum define is empty %v", enumStr) 54 } 55 elems := strings.Split(enumStr, ",") 56 return parseEnumName(elems, name) 57 } 58 func parseEnumName(elems []string, name string) (uint16, error) { 59 for i, n := range elems { 60 if strings.EqualFold(n, name) { 61 return uint16(i) + 1, nil 62 } 63 } 64 return 0, moerr.NewInternalErrorNoCtx("convert to MySQL enum failed: item %s is not in enum %v", name, elems) 65 } 66 67 // ParseEnumValue return item index with special number. 68 func ParseEnumValue(enumStr string, number uint16) (uint16, error) { 69 if len(enumStr) == 0 { 70 return 0, moerr.NewInternalErrorNoCtx("convert to MySQL enum failed: enum define is empty %v", enumStr) 71 } 72 elems := strings.Split(enumStr, ",") 73 return parseEnumValue(elems, number) 74 } 75 func parseEnumValue(elems []string, number uint16) (uint16, error) { 76 if number == 0 || number > uint16(len(elems)) { 77 return 0, moerr.NewInternalErrorNoCtx("convert to MySQL enum failed: number %d overflow enum boundary [1, %d]", number, len(elems)) 78 } 79 80 return number, nil 81 } 82 83 // ParseEnumIndex return item value with index. 84 func ParseEnumIndex(enumStr string, index uint16) (string, error) { 85 if len(enumStr) == 0 { 86 return "", moerr.NewInternalErrorNoCtx("parse MySQL enum failed: enum type length err %d", len(enumStr)) 87 } 88 elems := strings.Split(enumStr, ",") 89 if index == 0 || index > uint16(len(elems)) { 90 return "", moerr.NewInternalErrorNoCtx("parse MySQL enum failed: index %d overflow enum boundary [1, %d]", index, len(elems)) 91 } 92 return elems[index-1], nil 93 }