fill lists of primitives

master
Jordan Orelli 10 years ago
parent 1c82e01937
commit ca6e986b0f

@ -28,11 +28,10 @@ func (l List) fillValue(v reflect.Value) error {
}
default:
sv := reflect.ValueOf(item)
if dv.Type().AssignableTo(sv.Type()) {
return fmt.Errorf("can assign")
} else {
if !dv.Type().AssignableTo(sv.Type()) {
return fmt.Errorf("unable to assign element %d: source type %v is not assignable to destination type %v", idx, sv.Type(), dv.Type())
}
dv.Set(sv)
}
}
return nil

@ -1,6 +1,7 @@
package moon
import (
"reflect"
"testing"
)
@ -11,39 +12,32 @@ func TestFillList(t *testing.T) {
{hostname: dev.example.com; label: dev}
{hostname: prod.example.com; label: prod}
]
numbers: [1 1 2 3 5 8 13]
`)
if err != nil {
t.Error(err)
return
}
type server struct {
Hostname string `name: hostname; required: true`
Label string `name: label; required: true`
}
var config struct {
Servers []struct {
Hostname string `name: hostname; required: true`
Label string `name: label; required: true`
} `name: servers`
Servers []server `name: servers`
Numbers []int `name: numbers`
}
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])
servers := []server{
{"dev.example.com", "dev"},
{"prod.example.com", "prod"},
}
if config.Servers[1].Hostname != "prod.example.com" {
t.Errorf("wut 1 lol %v", config.Servers[1])
if !reflect.DeepEqual(config.Servers, servers) {
t.Errorf("bad servers: %v", config.Servers)
}
if config.Servers[1].Label != "prod" {
t.Errorf("wut 2 lol %v", config.Servers[1])
if !reflect.DeepEqual(config.Numbers, []int{1, 1, 2, 3, 5, 8, 13}) {
t.Errorf("bad numbers: %v", config.Numbers)
}
}

@ -179,9 +179,13 @@ func (o *Object) fillValue(dv reflect.Value) error {
switch t_ov := ov.(type) {
case *Object:
return t_ov.fillValue(fv)
if err := t_ov.fillValue(fv); err != nil {
return err
}
case List:
return t_ov.fillValue(fv)
if err := t_ov.fillValue(fv); err != nil {
return err
}
default:
if !fv.Type().AssignableTo(reflect.TypeOf(ov)) {
return fmt.Errorf("unable to assign field %s: source type %v is not assignable to destination type %v", req.name, reflect.TypeOf(ov), fv.Type())

Loading…
Cancel
Save