gitlab.com/evatix-go/core@v1.3.55/converters/StringToByte.go (about) 1 package converters 2 3 import ( 4 "strconv" 5 6 "gitlab.com/evatix-go/core/constants" 7 "gitlab.com/evatix-go/core/errcore" 8 ) 9 10 func StringToByte(input string) (byte, error) { 11 if input == "" { 12 return 0, errcore.FailedToConvertType. 13 Error(errcore.CannotConvertStringToByte, input) 14 } 15 16 if input == "0" { 17 return 0, nil 18 } 19 20 if input == "1" { 21 return 1, nil 22 } 23 24 vInt, err := strconv.Atoi(input) 25 26 if err != nil { 27 return 0, err 28 } 29 30 if vInt < 0 { 31 return 0, errcore.FailedToConvertType. 32 Error(errcore.CannotConvertStringToByteForLessThanZero, input) 33 } 34 35 if vInt > constants.MaxUnit8AsInt { 36 return 0, errcore.FailedToConvertType. 37 Error(errcore.CannotConvertStringToByteForMoreThan255, input) 38 } 39 40 return byte(vInt), nil 41 }