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: default:
sv := reflect.ValueOf(item) sv := reflect.ValueOf(item)
if dv.Type().AssignableTo(sv.Type()) { 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 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 return nil

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

@ -179,9 +179,13 @@ func (o *Object) fillValue(dv reflect.Value) error {
switch t_ov := ov.(type) { switch t_ov := ov.(type) {
case *Object: case *Object:
return t_ov.fillValue(fv) if err := t_ov.fillValue(fv); err != nil {
return err
}
case List: case List:
return t_ov.fillValue(fv) if err := t_ov.fillValue(fv); err != nil {
return err
}
default: default:
if !fv.Type().AssignableTo(reflect.TypeOf(ov)) { 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()) 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