Добавлена поддержка bool
Этот коммит содержится в:
родитель
c546c50bd6
коммит
ecbda47823
2 изменённых файлов: 60 добавлений и 13 удалений
55
goconf.go
55
goconf.go
|
@ -76,15 +76,17 @@ func (c *ConfigParser) digConfigElement(e ast.Expr) error {
|
||||||
return errors.New("Key not found")
|
return errors.New("Key not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
value, ok := keyvalue.Value.(*ast.BasicLit)
|
switch keyvalue.Value.(type) {
|
||||||
if !ok {
|
case *ast.BasicLit, *ast.Ident:
|
||||||
|
return c.setConfigElement(key.Name, keyvalue.Value)
|
||||||
|
default:
|
||||||
return errors.New("Value not found")
|
return errors.New("Value not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.setConfigElement(key.Name, value)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigParser) setConfigElement(key string, value *ast.BasicLit) error {
|
func (c *ConfigParser) setConfigElement(key string, value ast.Expr) error {
|
||||||
config := reflect.ValueOf(c.config)
|
config := reflect.ValueOf(c.config)
|
||||||
|
|
||||||
kind := config.Kind()
|
kind := config.Kind()
|
||||||
|
@ -106,7 +108,7 @@ func (c *ConfigParser) setConfigElement(key string, value *ast.BasicLit) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (c *ConfigParser) setConfigElementField(key string, value *ast.BasicLit, config reflect.Value, i int, vt reflect.Type) error {
|
func (c *ConfigParser) setConfigElementField(key string, value ast.Expr, config reflect.Value, i int, vt reflect.Type) error {
|
||||||
if vt.Field(i).Name != key {
|
if vt.Field(i).Name != key {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -120,6 +122,8 @@ func (c *ConfigParser) setConfigElementField(key string, value *ast.BasicLit, co
|
||||||
c.setConfigFieldToString(uv, value)
|
c.setConfigFieldToString(uv, value)
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
c.setConfigFieldToInt(uv, value)
|
c.setConfigFieldToInt(uv, value)
|
||||||
|
case reflect.Bool:
|
||||||
|
c.setConfigFieldToBool(uv, value)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -131,7 +135,15 @@ func unpackValue(v reflect.Value) reflect.Value {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigParser) setConfigFieldToString(uv reflect.Value, value *ast.BasicLit) error {
|
func (c *ConfigParser) setConfigFieldToString(uv reflect.Value, v ast.Expr) error {
|
||||||
|
switch v.(type) {
|
||||||
|
case *ast.BasicLit:
|
||||||
|
default:
|
||||||
|
return errors.New("Not string")
|
||||||
|
}
|
||||||
|
|
||||||
|
value := v.(*ast.BasicLit)
|
||||||
|
|
||||||
if value.Kind != token.STRING {
|
if value.Kind != token.STRING {
|
||||||
return errors.New("Not string")
|
return errors.New("Not string")
|
||||||
}
|
}
|
||||||
|
@ -143,7 +155,15 @@ func (c *ConfigParser) setConfigFieldToString(uv reflect.Value, value *ast.Basic
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigParser) setConfigFieldToInt(uv reflect.Value, value *ast.BasicLit) error {
|
func (c *ConfigParser) setConfigFieldToInt(uv reflect.Value, v ast.Expr) error {
|
||||||
|
switch v.(type) {
|
||||||
|
case *ast.BasicLit:
|
||||||
|
default:
|
||||||
|
return errors.New("Not int")
|
||||||
|
}
|
||||||
|
|
||||||
|
value := v.(*ast.BasicLit)
|
||||||
|
|
||||||
if value.Kind != token.INT {
|
if value.Kind != token.INT {
|
||||||
return errors.New("Not int")
|
return errors.New("Not int")
|
||||||
}
|
}
|
||||||
|
@ -154,3 +174,24 @@ func (c *ConfigParser) setConfigFieldToInt(uv reflect.Value, value *ast.BasicLit
|
||||||
uv.SetInt(int64(num))
|
uv.SetInt(int64(num))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ConfigParser) setConfigFieldToBool(uv reflect.Value, v ast.Expr) error {
|
||||||
|
switch v.(type) {
|
||||||
|
case *ast.Ident:
|
||||||
|
default:
|
||||||
|
return errors.New("Not bool")
|
||||||
|
}
|
||||||
|
|
||||||
|
value := v.(*ast.Ident)
|
||||||
|
|
||||||
|
switch value.Name {
|
||||||
|
case "true":
|
||||||
|
uv.SetBool(true)
|
||||||
|
case "false":
|
||||||
|
uv.SetBool(false)
|
||||||
|
default:
|
||||||
|
return errors.New("Not bool")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -17,18 +17,22 @@ func TestUtils(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
Params string
|
Params string
|
||||||
IntParam int
|
IntParam int
|
||||||
|
BoolParamTrue bool
|
||||||
|
BoolParamFalse bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = Config{
|
var config = Config{
|
||||||
Host: "testhost",
|
Host: "testhost",
|
||||||
Port: 19,
|
Port: 19,
|
||||||
|
|
||||||
Params: "some params",
|
Params: "some params",
|
||||||
IntParam: 199,
|
IntParam: 199,
|
||||||
|
BoolParamTrue: true,
|
||||||
|
BoolParamFalse: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = Describe("Lib", func() {
|
var _ = Describe("Lib", func() {
|
||||||
|
@ -42,6 +46,8 @@ var _ = Describe("Lib", func() {
|
||||||
Ω(c.Port).To(Be(19))
|
Ω(c.Port).To(Be(19))
|
||||||
Ω(c.Params).To(Be("some params"))
|
Ω(c.Params).To(Be("some params"))
|
||||||
Ω(c.IntParam).To(Be(199))
|
Ω(c.IntParam).To(Be(199))
|
||||||
|
Ω(c.BoolParamTrue).To(BeTrue())
|
||||||
|
Ω(c.BoolParamFalse).To(BeFalse())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче