unmarshalAttr unmarshals a single XML attribute into val.
(val reflect.Value, attr Attr)
| 249 | |
| 250 | // unmarshalAttr unmarshals a single XML attribute into val. |
| 251 | func (d *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error { |
| 252 | if val.Kind() == reflect.Pointer { |
| 253 | if val.IsNil() { |
| 254 | val.Set(reflect.New(val.Type().Elem())) |
| 255 | } |
| 256 | val = val.Elem() |
| 257 | } |
| 258 | if val.CanInterface() && val.Type().Implements(unmarshalerAttrType) { |
| 259 | // This is an unmarshaler with a non-pointer receiver, |
| 260 | // so it's likely to be incorrect, but we do what we're told. |
| 261 | return val.Interface().(UnmarshalerAttr).UnmarshalXMLAttr(attr) |
| 262 | } |
| 263 | if val.CanAddr() { |
| 264 | pv := val.Addr() |
| 265 | if pv.CanInterface() && pv.Type().Implements(unmarshalerAttrType) { |
| 266 | return pv.Interface().(UnmarshalerAttr).UnmarshalXMLAttr(attr) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Not an UnmarshalerAttr; try encoding.TextUnmarshaler. |
| 271 | if val.CanInterface() && val.Type().Implements(textUnmarshalerType) { |
| 272 | // This is an unmarshaler with a non-pointer receiver, |
| 273 | // so it's likely to be incorrect, but we do what we're told. |
| 274 | return val.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(attr.Value)) |
| 275 | } |
| 276 | if val.CanAddr() { |
| 277 | pv := val.Addr() |
| 278 | if pv.CanInterface() && pv.Type().Implements(textUnmarshalerType) { |
| 279 | return pv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(attr.Value)) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if val.Type().Kind() == reflect.Slice && val.Type().Elem().Kind() != reflect.Uint8 { |
| 284 | // Slice of element values. |
| 285 | // Grow slice. |
| 286 | n := val.Len() |
| 287 | val.Set(reflect.Append(val, reflect.Zero(val.Type().Elem()))) |
| 288 | |
| 289 | // Recur to read element into slice. |
| 290 | if err := d.unmarshalAttr(val.Index(n), attr); err != nil { |
| 291 | val.SetLen(n) |
| 292 | return err |
| 293 | } |
| 294 | return nil |
| 295 | } |
| 296 | |
| 297 | if val.Type() == attrType { |
| 298 | val.Set(reflect.ValueOf(attr)) |
| 299 | return nil |
| 300 | } |
| 301 | |
| 302 | return copyValue(val, []byte(attr.Value)) |
| 303 | } |
| 304 | |
| 305 | var ( |
| 306 | attrType = reflect.TypeFor[Attr]() |
no test coverage detected