@@ -148,7 +148,6 @@ type structEncoder struct {
148148
149149func (se * structEncoder ) encode (v reflect.Value ) interface {} {
150150 m := make (map [string ]interface {})
151-
152151 for i , f := range se .fields {
153152 fv := fieldByIndex (v , f .index )
154153 if ! fv .IsValid () || f .omitEmpty && se .isEmptyValue (fv ) {
@@ -159,27 +158,8 @@ func (se *structEncoder) encode(v reflect.Value) interface{} {
159158
160159 // If this field is a referenced field then attempt to extract the value.
161160 if f .reference {
162- refName := f .name
163- if f .refName != "" {
164- refName = f .refName
165- }
166-
167- // referenced fields can only handle maps so return an error if the
168- // encoded field is of a different type
169- m , ok := encField .(map [string ]interface {})
170- if ! ok {
171- err := fmt .Errorf ("Error referencing field %s in %s, expected object but got %t" , refName , f .name , encField )
172- panic (& MarshalerError {v .Type (), err })
173- }
174-
175- refVal , ok := m [refName ]
176- if ! ok {
177- err := fmt .Errorf ("Error referencing field %s in %s, could not find referenced field" , refName , f .name )
178- panic (& MarshalerError {v .Type (), err })
179- }
180-
181161 // Override the encoded field with the referenced field
182- encField = refVal
162+ encField = getReferenceField ( f , v , encField )
183163 }
184164
185165 m [f .name ] = encField
@@ -188,6 +168,40 @@ func (se *structEncoder) encode(v reflect.Value) interface{} {
188168 return m
189169}
190170
171+ func getReferenceField (f field , v reflect.Value , encField interface {}) interface {} {
172+ refName := f .name
173+ if f .refName != "" {
174+ refName = f .refName
175+ }
176+
177+ encFields , isArray := encField .([]interface {})
178+ if isArray {
179+ refVals := make ([]interface {}, len (encFields ))
180+ for i , e := range encFields {
181+ refVals [i ] = extractValue (e , v , f .name , refName )
182+ }
183+ return refVals
184+ }
185+ refVal := extractValue (encField , v , f .name , refName )
186+ return refVal
187+ }
188+
189+ func extractValue (encField interface {}, v reflect.Value , name string , refName string ) interface {} {
190+ // referenced fields can only handle maps so return an error if the
191+ // encoded field is of a different type
192+ m , ok := encField .(map [string ]interface {})
193+ if ! ok {
194+ err := fmt .Errorf ("Error refing field %s in %s, expected object but got %t" , refName , name , encField )
195+ panic (& MarshalerError {v .Type (), err })
196+ }
197+ refVal , ok := m [refName ]
198+ if ! ok {
199+ err := fmt .Errorf ("Error refing field %s in %s, could not find referenced field" , refName , name )
200+ panic (& MarshalerError {v .Type (), err })
201+ }
202+ return refVal
203+ }
204+
191205func (se * structEncoder ) isEmptyValue (v reflect.Value ) bool {
192206 if v .Type () == timeType {
193207 return v .Interface ().(time.Time ) == time.Time {}
0 commit comments