can now fill lists
parent
981d7978f5
commit
1c82e01937
@ -0,0 +1,39 @@
|
||||
package moon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type List []interface{}
|
||||
|
||||
func (l List) fillValue(v reflect.Value) error {
|
||||
if v.Kind() != reflect.Slice {
|
||||
return fmt.Errorf("moon List can only fillValue to a slice, saw %v (%v)", v.Type(), v.Kind())
|
||||
}
|
||||
if v.IsNil() {
|
||||
v.Set(reflect.MakeSlice(v.Type(), len(l), cap(l)))
|
||||
}
|
||||
for idx, item := range l {
|
||||
dv := v.Index(idx)
|
||||
|
||||
switch t_sv := item.(type) {
|
||||
case *Object:
|
||||
if err := t_sv.fillValue(dv); err != nil {
|
||||
return err
|
||||
}
|
||||
case List:
|
||||
if err := t_sv.fillValue(dv); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
sv := reflect.ValueOf(item)
|
||||
if dv.Type().AssignableTo(sv.Type()) {
|
||||
return fmt.Errorf("can assign")
|
||||
} else {
|
||||
return fmt.Errorf("unable to assign element %d: source type %v is not assignable to destination type %v", idx, sv.Type(), dv.Type())
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package moon
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFillList(t *testing.T) {
|
||||
doc, err := ReadString(`
|
||||
# yay for lists
|
||||
servers: [
|
||||
{hostname: dev.example.com; label: dev}
|
||||
{hostname: prod.example.com; label: prod}
|
||||
]
|
||||
`)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
var config struct {
|
||||
Servers []struct {
|
||||
Hostname string `name: hostname; required: true`
|
||||
Label string `name: label; required: true`
|
||||
} `name: servers`
|
||||
}
|
||||
if err := doc.Fill(&config); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if config.Servers == nil {
|
||||
t.Error("servers is nil for some reason")
|
||||
return
|
||||
}
|
||||
if len(config.Servers) != 2 {
|
||||
t.Errorf("expected 2 servers, saw %d", len(config.Servers))
|
||||
return
|
||||
}
|
||||
if config.Servers[0].Hostname != "dev.example.com" {
|
||||
t.Errorf("wut lol %v", config.Servers[0])
|
||||
}
|
||||
if config.Servers[0].Label != "dev" {
|
||||
t.Errorf("wut lol %v", config.Servers[0])
|
||||
}
|
||||
if config.Servers[1].Hostname != "prod.example.com" {
|
||||
t.Errorf("wut 1 lol %v", config.Servers[1])
|
||||
}
|
||||
if config.Servers[1].Label != "prod" {
|
||||
t.Errorf("wut 2 lol %v", config.Servers[1])
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue