Compare commits
3 Commits
v0.1.52
...
73278439b0
Author | SHA1 | Date | |
---|---|---|---|
73278439b0
|
|||
0a589a2565
|
|||
179666c8f9
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
tests/buildenv/*
|
||||||
|
tests/bin/*
|
@ -1,3 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
go fmt *.go
|
go fmt *.go
|
||||||
git add *.go
|
go fmt tests/*.go
|
||||||
|
|
||||||
|
git add *.go tests/*.go
|
||||||
|
git add tests/*.go
|
||||||
|
33
datatype.go
33
datatype.go
@ -5,29 +5,34 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DataType uint
|
type DataType uint32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TypeNone DataType = iota
|
sizeBitmask DataType = 0xffff
|
||||||
TypeInt DataType = iota
|
typeBitmask DataType = sizeBitmask << 16
|
||||||
TypeMetric DataType = iota
|
|
||||||
TypeDuration DataType = iota
|
|
||||||
TypeString DataType = iota
|
|
||||||
TypeDirectory DataType = iota
|
|
||||||
TypeBool DataType = iota
|
|
||||||
TypeHex DataType = iota
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func FixedHex(size uint) DataType {
|
const (
|
||||||
return (DataType)(size<<16) | TypeHex
|
TypeNone DataType = iota << 16
|
||||||
|
TypeInt DataType = iota << 16
|
||||||
|
TypeMetric DataType = iota << 16
|
||||||
|
TypeDuration DataType = iota << 16
|
||||||
|
TypeString DataType = iota << 16
|
||||||
|
TypeDirectory DataType = iota << 16
|
||||||
|
TypeBool DataType = iota << 16
|
||||||
|
TypeHex DataType = iota << 16
|
||||||
|
)
|
||||||
|
|
||||||
|
func FixedHex(size uint16) DataType {
|
||||||
|
return TypeHex | DataType(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dtype DataType) baseType() DataType {
|
func (dtype DataType) baseType() DataType {
|
||||||
return dtype & 0xffff
|
return dtype & typeBitmask
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dtype DataType) typeAndSize() (DataType, uint) {
|
func (dtype DataType) typeAndSize() (DataType, int) {
|
||||||
return (dtype & 0xffff), uint(dtype >> 16)
|
return (dtype & typeBitmask), int(dtype & sizeBitmask)
|
||||||
}
|
}
|
||||||
|
|
||||||
type cValue struct {
|
type cValue struct {
|
||||||
|
16
parsers.go
16
parsers.go
@ -11,7 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseInt(key string, str string, _ uint) (ret cValue) {
|
func parseInt(key string, str string, _ int) (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, _ uint) (ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseMetric(key string, str string, _ uint) (ret cValue) {
|
func parseMetric(key string, str string, _ int) (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, _ uint) (ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDuration(key string, str string, _ uint) (ret cValue) {
|
func parseDuration(key string, str string, _ int) (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, _ uint) (ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseBool(key string, str string, _ uint) (ret cValue) {
|
func parseBool(key string, str string, _ int) (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,9 +66,9 @@ func parseBool(key string, str string, _ uint) (ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseHex(key string, str string, size uint) (ret cValue) {
|
func parseHex(key string, str string, size int) (ret cValue) {
|
||||||
val, err := hex.DecodeString(str)
|
val, err := hex.DecodeString(str)
|
||||||
if err == nil && (size == 0 || size == uint(len(val))) {
|
if err == nil && (size == 0 || size == len(val)) {
|
||||||
ret.binval = val
|
ret.binval = val
|
||||||
} else {
|
} else {
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
@ -80,7 +80,7 @@ func parseHex(key string, str string, size uint) (ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDirectory(_ string, str string, _ uint) (ret cValue) {
|
func parseDirectory(_ string, str string, _ int) (ret cValue) {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if path.IsAbs(str) {
|
if path.IsAbs(str) {
|
||||||
@ -94,7 +94,7 @@ func parseDirectory(_ string, str string, _ uint) (ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseString(_ string, str string, _ uint) (ret cValue) {
|
func parseString(_ string, str string, _ int) (ret cValue) {
|
||||||
ret.strval = str
|
ret.strval = str
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
22
tests/build.sh
Executable file
22
tests/build.sh
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
dir=$(dirname $(realpath $0))
|
||||||
|
export GO111MODULE=on
|
||||||
|
export GOPROXY=direct
|
||||||
|
export GOPATH=${dir}/buildenv/deps
|
||||||
|
|
||||||
|
mkdir -p ${dir}/buildenv
|
||||||
|
chmod -R 755 ${dir}/buildenv
|
||||||
|
rm -rf ${dir}/buildenv
|
||||||
|
|
||||||
|
mkdir -p "${dir}/buildenv/src"
|
||||||
|
mkdir -p ${GOPATH}/src
|
||||||
|
mkdir -p ${dir}/bin
|
||||||
|
|
||||||
|
cp -r "${dir}/main.go" "${dir}/buildenv/src/main.go"
|
||||||
|
|
||||||
|
cd ${dir}/buildenv
|
||||||
|
|
||||||
|
go mod init src
|
||||||
|
go mod tidy
|
||||||
|
|
||||||
|
go build -o "${dir}/bin/test" "src/main.go"
|
15
tests/main.go
Normal file
15
tests/main.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.purser.it/roypur/envconf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conf := envconf.NewConfig()
|
||||||
|
conf.Define("this_is_a_map", envconf.FixedHex(22))
|
||||||
|
conf.Parse()
|
||||||
|
conf.Status()
|
||||||
|
|
||||||
|
fmt.Println(conf.GetHex("this_is_a_map"))
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package envconf
|
package envconf
|
||||||
|
|
||||||
type dataTypeInfo struct {
|
type dataTypeInfo struct {
|
||||||
parser func(string, string, uint) cValue
|
parser func(string, string, int) cValue
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user