github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/devicetwin/dtcommon/util.go (about) 1 package dtcommon 2 3 import ( 4 "errors" 5 "regexp" 6 "strconv" 7 "strings" 8 ) 9 10 const ( 11 //MAXTWINNUM max twin key 12 MAXTWINNUM = 64 13 ) 14 15 //ValidateValue validate value type 16 func ValidateValue(valueType string, value string) error { 17 switch valueType { 18 case "": 19 valueType = "string" 20 return nil 21 case "string": 22 return nil 23 case "int": 24 _, err := strconv.ParseInt(value, 10, 64) 25 if err != nil { 26 return errors.New("the value is not int") 27 } 28 return nil 29 case "float": 30 _, err := strconv.ParseFloat(value, 64) 31 if err != nil { 32 return errors.New("the value is not float") 33 } 34 return nil 35 case "boolean": 36 if strings.Compare(value, "true") != 0 && strings.Compare(value, "false") != 0 { 37 return errors.New("the bool value must be true or false") 38 } 39 return nil 40 case "deleted": 41 return nil 42 default: 43 return errors.New("the value type is not allowed") 44 } 45 } 46 47 //ValidateTwinKey validate twin key 48 func ValidateTwinKey(key string) bool { 49 pattern := "^[a-zA-Z0-9-_.,:/@#]{1,128}$" 50 match, _ := regexp.MatchString(pattern, key) 51 return match 52 } 53 54 //ValidateTwinValue validate twin value 55 func ValidateTwinValue(value string) bool { 56 pattern := "^[a-zA-Z0-9-_.,:/@#]{1,512}$" 57 match, _ := regexp.MatchString(pattern, value) 58 return match 59 }