variable length hex
This commit is contained in:
		
							
								
								
									
										27
									
								
								datatype.go
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								datatype.go
									
									
									
									
									
								
							@@ -1,10 +1,11 @@
 | 
				
			|||||||
package envconf
 | 
					package envconf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type DataType int
 | 
					type DataType uint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	TypeNone      DataType = iota
 | 
						TypeNone      DataType = iota
 | 
				
			||||||
@@ -15,12 +16,16 @@ const (
 | 
				
			|||||||
	TypeDirectory DataType = iota
 | 
						TypeDirectory DataType = iota
 | 
				
			||||||
	TypeBool      DataType = iota
 | 
						TypeBool      DataType = iota
 | 
				
			||||||
	TypeHex       DataType = iota
 | 
						TypeHex       DataType = iota
 | 
				
			||||||
	TypeHex16     DataType = iota
 | 
					 | 
				
			||||||
	TypeHex32     DataType = iota
 | 
					 | 
				
			||||||
	TypeHex64     DataType = iota
 | 
					 | 
				
			||||||
	TypeHex128    DataType = iota
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func FixedHex(size uint) DataType {
 | 
				
			||||||
 | 
						return (DataType)(size<<16) | TypeHex
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (dtype DataType) typeAndSize() (DataType, uint) {
 | 
				
			||||||
 | 
						return (dtype & 0xffff), uint(dtype >> 16)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type cValue struct {
 | 
					type cValue struct {
 | 
				
			||||||
	dtype   DataType
 | 
						dtype   DataType
 | 
				
			||||||
	intval  int64
 | 
						intval  int64
 | 
				
			||||||
@@ -32,18 +37,24 @@ type cValue struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (dtype DataType) parse(key string, str string) (ret cValue) {
 | 
					func (dtype DataType) parse(key string, str string) (ret cValue) {
 | 
				
			||||||
	info, ok := tInfo[dtype]
 | 
						rdtype, size := dtype.typeAndSize()
 | 
				
			||||||
 | 
						info, ok := tInfo[rdtype]
 | 
				
			||||||
	if ok {
 | 
						if ok {
 | 
				
			||||||
		ret = info.parser(key, str)
 | 
							ret = info.parser(key, str, size)
 | 
				
			||||||
		if len(ret.binval) == 0 {
 | 
							if len(ret.binval) == 0 {
 | 
				
			||||||
			ret.binval = make([]byte, 0, 0)
 | 
								ret.binval = make([]byte, 0, 0)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (dtype DataType) String() string {
 | 
					func (dtype DataType) String() string {
 | 
				
			||||||
	info, ok := tInfo[dtype]
 | 
						rdtype, size := dtype.typeAndSize()
 | 
				
			||||||
 | 
						info, ok := tInfo[rdtype]
 | 
				
			||||||
	if ok {
 | 
						if ok {
 | 
				
			||||||
 | 
							if size > 0 {
 | 
				
			||||||
 | 
								return fmt.Sprintf("%s%d", info.name, size)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		return info.name
 | 
							return info.name
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return "invalid"
 | 
						return "invalid"
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										68
									
								
								envconf.go
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								envconf.go
									
									
									
									
									
								
							@@ -324,36 +324,8 @@ func (c *Config) GetInt(key string) int64 {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Returns the value of an environment variable.
 | 
					// Returns the value of an environment variable.
 | 
				
			||||||
// If the variable is not defined as envconf.TypeHex the function will return []byte{}.
 | 
					// If the variable is not defined as envconf.TypeHex the function will return []byte{}.
 | 
				
			||||||
func (c *Config) GetHex(key string) []byte {
 | 
					func (c *Config) GetHex(key string, size int) []byte {
 | 
				
			||||||
	val := c.getRaw(key, TypeHex)
 | 
						val := c.getRaw(key, FixedHex(uint(size)))
 | 
				
			||||||
	return val.binval
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Returns the value of an environment variable.
 | 
					 | 
				
			||||||
// If the variable is not defined as envconf.TypeHex16 the function will return []byte{}.
 | 
					 | 
				
			||||||
func (c *Config) GetHex16(key string) []byte {
 | 
					 | 
				
			||||||
	val := c.getRaw(key, TypeHex16)
 | 
					 | 
				
			||||||
	return val.binval
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Returns the value of an environment variable.
 | 
					 | 
				
			||||||
// If the variable is not defined as envconf.TypeHex32 the function will return []byte{}.
 | 
					 | 
				
			||||||
func (c *Config) GetHex32(key string) []byte {
 | 
					 | 
				
			||||||
	val := c.getRaw(key, TypeHex32)
 | 
					 | 
				
			||||||
	return val.binval
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Returns the value of an environment variable.
 | 
					 | 
				
			||||||
// If the variable is not defined as envconf.TypeHex64 the function will return []byte{}.
 | 
					 | 
				
			||||||
func (c *Config) GetHex64(key string) []byte {
 | 
					 | 
				
			||||||
	val := c.getRaw(key, TypeHex64)
 | 
					 | 
				
			||||||
	return val.binval
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Returns the value of an environment variable.
 | 
					 | 
				
			||||||
// If the variable is not defined as envconf.TypeHex128 the function will return []byte{}.
 | 
					 | 
				
			||||||
func (c *Config) GetHex128(key string) []byte {
 | 
					 | 
				
			||||||
	val := c.getRaw(key, TypeHex128)
 | 
					 | 
				
			||||||
	return val.binval
 | 
						return val.binval
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -428,41 +400,9 @@ func (c *Config) GetMapBool(key string) (retval map[string]bool) {
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Config) GetMapHex(key string) (retval map[string][]byte) {
 | 
					func (c *Config) GetMapHex(key string, size int) (retval map[string][]byte) {
 | 
				
			||||||
	retval = make(map[string][]byte)
 | 
						retval = make(map[string][]byte)
 | 
				
			||||||
	for k, v := range c.getRawMap(key, TypeHex) {
 | 
						for k, v := range c.getRawMap(key, FixedHex(uint(size))) {
 | 
				
			||||||
		retval[k] = v.binval
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *Config) GetMapHex16(key string) (retval map[string][]byte) {
 | 
					 | 
				
			||||||
	retval = make(map[string][]byte)
 | 
					 | 
				
			||||||
	for k, v := range c.getRawMap(key, TypeHex16) {
 | 
					 | 
				
			||||||
		retval[k] = v.binval
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *Config) GetMapHex32(key string) (retval map[string][]byte) {
 | 
					 | 
				
			||||||
	retval = make(map[string][]byte)
 | 
					 | 
				
			||||||
	for k, v := range c.getRawMap(key, TypeHex32) {
 | 
					 | 
				
			||||||
		retval[k] = v.binval
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *Config) GetMapHex64(key string) (retval map[string][]byte) {
 | 
					 | 
				
			||||||
	retval = make(map[string][]byte)
 | 
					 | 
				
			||||||
	for k, v := range c.getRawMap(key, TypeHex64) {
 | 
					 | 
				
			||||||
		retval[k] = v.binval
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *Config) GetMapHex128(key string) (retval map[string][]byte) {
 | 
					 | 
				
			||||||
	retval = make(map[string][]byte)
 | 
					 | 
				
			||||||
	for k, v := range c.getRawMap(key, TypeHex128) {
 | 
					 | 
				
			||||||
		retval[k] = v.binval
 | 
							retval[k] = v.binval
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										58
									
								
								parsers.go
									
									
									
									
									
								
							
							
						
						
									
										58
									
								
								parsers.go
									
									
									
									
									
								
							@@ -11,7 +11,7 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseInt(key string, str string) (ret cValue) {
 | 
					func parseInt(key string, str string, _ uint) (ret cValue) {
 | 
				
			||||||
	val, err := strconv.ParseInt(str, 10, 64)
 | 
						val, err := strconv.ParseInt(str, 10, 64)
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
		ret.intval = val
 | 
							ret.intval = val
 | 
				
			||||||
@@ -21,7 +21,7 @@ func parseInt(key string, str string) (ret cValue) {
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseMetric(key string, str string) (ret cValue) {
 | 
					func parseMetric(key string, str string, _ uint) (ret cValue) {
 | 
				
			||||||
	mod := int64(1)
 | 
						mod := int64(1)
 | 
				
			||||||
	str = strings.ToUpper(str)
 | 
						str = strings.ToUpper(str)
 | 
				
			||||||
	if strings.HasSuffix(str, "K") {
 | 
						if strings.HasSuffix(str, "K") {
 | 
				
			||||||
@@ -46,7 +46,7 @@ func parseMetric(key string, str string) (ret cValue) {
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseDuration(key string, str string) (ret cValue) {
 | 
					func parseDuration(key string, str string, _ uint) (ret cValue) {
 | 
				
			||||||
	val, err := time.ParseDuration(str)
 | 
						val, err := time.ParseDuration(str)
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
		ret.durval = val
 | 
							ret.durval = val
 | 
				
			||||||
@@ -56,7 +56,7 @@ func parseDuration(key string, str string) (ret cValue) {
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseBool(key string, str string) (ret cValue) {
 | 
					func parseBool(key string, str string, _ uint) (ret cValue) {
 | 
				
			||||||
	val, err := strconv.ParseBool(str)
 | 
						val, err := strconv.ParseBool(str)
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
		ret.boolval = val
 | 
							ret.boolval = val
 | 
				
			||||||
@@ -66,57 +66,21 @@ func parseBool(key string, str string) (ret cValue) {
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseHex(key string, str string) (ret cValue) {
 | 
					func parseHex(key string, str string, size uint) (ret cValue) {
 | 
				
			||||||
	val, err := hex.DecodeString(str)
 | 
						val, err := hex.DecodeString(str)
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil && (size == 0 || size == uint(len(val))) {
 | 
				
			||||||
		ret.binval = val
 | 
							ret.binval = val
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
 | 
							if size == 0 {
 | 
				
			||||||
			ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
 | 
								ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func parseHex16(key string, str string) (ret cValue) {
 | 
					 | 
				
			||||||
	val, err := hex.DecodeString(str)
 | 
					 | 
				
			||||||
	if err == nil && len(val) == 16 {
 | 
					 | 
				
			||||||
		ret.binval = val
 | 
					 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
		ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex16.`, key))
 | 
								ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex%d.`, key, size))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseHex32(key string, str string) (ret cValue) {
 | 
					func parseDirectory(_ string, str string, _ uint) (ret cValue) {
 | 
				
			||||||
	val, err := hex.DecodeString(str)
 | 
					 | 
				
			||||||
	if err == nil && len(val) == 32 {
 | 
					 | 
				
			||||||
		ret.binval = val
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex32.`, key))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func parseHex64(key string, str string) (ret cValue) {
 | 
					 | 
				
			||||||
	val, err := hex.DecodeString(str)
 | 
					 | 
				
			||||||
	if err == nil && len(val) == 64 {
 | 
					 | 
				
			||||||
		ret.binval = val
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex64.`, key))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func parseHex128(key string, str string) (ret cValue) {
 | 
					 | 
				
			||||||
	val, err := hex.DecodeString(str)
 | 
					 | 
				
			||||||
	if err == nil && len(val) == 128 {
 | 
					 | 
				
			||||||
		ret.binval = val
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex128.`, key))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func parseDirectory(_ string, str string) (ret cValue) {
 | 
					 | 
				
			||||||
	wd, err := os.Getwd()
 | 
						wd, err := os.Getwd()
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
		if path.IsAbs(str) {
 | 
							if path.IsAbs(str) {
 | 
				
			||||||
@@ -130,7 +94,7 @@ func parseDirectory(_ string, str string) (ret cValue) {
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseString(_ string, str string) (ret cValue) {
 | 
					func parseString(_ string, str string, _ uint) (ret cValue) {
 | 
				
			||||||
	ret.strval = str
 | 
						ret.strval = str
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										18
									
								
								typeinfo.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								typeinfo.go
									
									
									
									
									
								
							@@ -1,7 +1,7 @@
 | 
				
			|||||||
package envconf
 | 
					package envconf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type dataTypeInfo struct {
 | 
					type dataTypeInfo struct {
 | 
				
			||||||
	parser func(string, string) cValue
 | 
						parser func(string, string, uint) cValue
 | 
				
			||||||
	name   string
 | 
						name   string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -16,11 +16,6 @@ func init() {
 | 
				
			|||||||
	var dirInfo dataTypeInfo
 | 
						var dirInfo dataTypeInfo
 | 
				
			||||||
	var hexInfo dataTypeInfo
 | 
						var hexInfo dataTypeInfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var hexInfo16 dataTypeInfo
 | 
					 | 
				
			||||||
	var hexInfo32 dataTypeInfo
 | 
					 | 
				
			||||||
	var hexInfo64 dataTypeInfo
 | 
					 | 
				
			||||||
	var hexInfo128 dataTypeInfo
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var boolInfo dataTypeInfo
 | 
						var boolInfo dataTypeInfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	intInfo.name = "int"
 | 
						intInfo.name = "int"
 | 
				
			||||||
@@ -38,11 +33,6 @@ func init() {
 | 
				
			|||||||
	strInfo.parser = parseString
 | 
						strInfo.parser = parseString
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hexInfo.parser = parseHex
 | 
						hexInfo.parser = parseHex
 | 
				
			||||||
	hexInfo16.parser = parseHex16
 | 
					 | 
				
			||||||
	hexInfo32.parser = parseHex32
 | 
					 | 
				
			||||||
	hexInfo64.parser = parseHex64
 | 
					 | 
				
			||||||
	hexInfo128.parser = parseHex128
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	boolInfo.parser = parseBool
 | 
						boolInfo.parser = parseBool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tInfo[TypeInt] = intInfo
 | 
						tInfo[TypeInt] = intInfo
 | 
				
			||||||
@@ -51,11 +41,5 @@ func init() {
 | 
				
			|||||||
	tInfo[TypeString] = strInfo
 | 
						tInfo[TypeString] = strInfo
 | 
				
			||||||
	tInfo[TypeDirectory] = dirInfo
 | 
						tInfo[TypeDirectory] = dirInfo
 | 
				
			||||||
	tInfo[TypeHex] = hexInfo
 | 
						tInfo[TypeHex] = hexInfo
 | 
				
			||||||
 | 
					 | 
				
			||||||
	tInfo[TypeHex16] = hexInfo16
 | 
					 | 
				
			||||||
	tInfo[TypeHex32] = hexInfo32
 | 
					 | 
				
			||||||
	tInfo[TypeHex64] = hexInfo64
 | 
					 | 
				
			||||||
	tInfo[TypeHex128] = hexInfo128
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	tInfo[TypeBool] = boolInfo
 | 
						tInfo[TypeBool] = boolInfo
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user