internal: remove xml.Name arg from Response.DecodeProp

The xml.Name is now retrieved from the interface{} argument via
reflection.
This commit is contained in:
Simon Ser 2020-01-15 11:44:27 +01:00
parent 25ab0b2076
commit 5fe39bbc13
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 43 additions and 8 deletions

View File

@ -38,7 +38,7 @@ func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
}
var prop addressbookHomeSet
if err := resp.DecodeProp(name, &prop); err != nil {
if err := resp.DecodeProp(&prop); err != nil {
return "", err
}
@ -70,7 +70,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
}
var resTypeProp internal.ResourceType
if err := resp.DecodeProp(resTypeName, &resTypeProp); err != nil {
if err := resp.DecodeProp(&resTypeProp); err != nil {
return nil, err
}
if !resTypeProp.Is(addressBookName) {
@ -78,7 +78,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
}
var descProp addressbookDescription
if err := resp.DecodeProp(descName, &descProp); err != nil {
if err := resp.DecodeProp(&descProp); err != nil {
return nil, err
}
@ -92,8 +92,6 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
}
func (c *Client) QueryAddressBook(addressBook string, query *AddressBookQuery) ([]Address, error) {
addrDataName := xml.Name{namespace, "address-data"}
var addrDataReq addressDataReq
for _, name := range query.Props {
addrDataReq.Props = append(addrDataReq.Props, prop{Name: name})
@ -126,7 +124,7 @@ func (c *Client) QueryAddressBook(addressBook string, query *AddressBookQuery) (
}
var addrData addressDataResp
if err := resp.DecodeProp(addrDataName, &addrData); err != nil {
if err := resp.DecodeProp(&addrData); err != nil {
return nil, err
}

View File

@ -29,7 +29,7 @@ func (c *Client) FindCurrentUserPrincipal() (string, error) {
}
var prop currentUserPrincipal
if err := resp.DecodeProp(name, &prop); err != nil {
if err := resp.DecodeProp(&prop); err != nil {
return "", err
}

View File

@ -73,7 +73,11 @@ func (resp *Response) Href() (string, error) {
return resp.Hrefs[0], nil
}
func (resp *Response) DecodeProp(name xml.Name, v interface{}) error {
func (resp *Response) DecodeProp(v interface{}) error {
name, err := valueXMLName(v)
if err != nil {
return err
}
if err := resp.Status.Err(); err != nil {
return err
}

View File

@ -2,7 +2,10 @@ package internal
import (
"encoding/xml"
"fmt"
"io"
"reflect"
"strings"
)
// RawXMLValue is a raw XML value. It implements xml.Unmarshaler and
@ -88,6 +91,9 @@ func (val *RawXMLValue) Decode(v interface{}) error {
// TokenReader returns a stream of tokens for the XML value.
func (val *RawXMLValue) TokenReader() xml.TokenReader {
if val.out != nil {
panic("webdav: called RawXMLValue.TokenReader on a marshal-only XML value")
}
return &rawXMLValueReader{val: val}
}
@ -133,3 +139,30 @@ func (tr *rawXMLValueReader) Token() (xml.Token, error) {
}
var _ xml.TokenReader = (*rawXMLValueReader)(nil)
func valueXMLName(v interface{}) (xml.Name, error) {
t := reflect.TypeOf(v)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return xml.Name{}, fmt.Errorf("webdav: %T is not a struct", v)
}
nameField, ok := t.FieldByName("XMLName")
if !ok {
return xml.Name{}, fmt.Errorf("webdav: %T is missing an XMLName struct field", v)
}
if nameField.Type != reflect.TypeOf(xml.Name{}) {
return xml.Name{}, fmt.Errorf("webdav: %T.XMLName isn't an xml.Name", v)
}
tag := nameField.Tag.Get("xml")
if tag == "" {
return xml.Name{}, fmt.Errorf(`webdav: %T.XMLName is missing an "xml" tag`, v)
}
name := strings.Split(tag, ",")[0]
nameParts := strings.Split(name, " ")
if len(nameParts) != 2 {
return xml.Name{}, fmt.Errorf("webdav: expected a namespace and local name in %T.XMLName's xml tag", v)
}
return xml.Name{nameParts[0], nameParts[1]}, nil
}