all: use variables for xml.Name values

This commit is contained in:
Simon Ser 2020-01-17 17:09:23 +01:00
parent 557972089c
commit 34b2ebf940
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
5 changed files with 20 additions and 13 deletions

View File

@ -6,15 +6,11 @@ import (
"github.com/emersion/go-vcard" "github.com/emersion/go-vcard"
) )
const namespace = "urn:ietf:params:xml:ns:carddav"
type AddressBook struct { type AddressBook struct {
Href string Href string
Description string Description string
} }
var addressBookName = xml.Name{namespace, "addressbook"}
type AddressBookQuery struct { type AddressBookQuery struct {
Props []string Props []string
} }

View File

@ -6,6 +6,10 @@ import (
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
) )
const namespace = "urn:ietf:params:xml:ns:carddav"
var addressBookName = xml.Name{namespace, "addressbook"}
type addressbookHomeSet struct { type addressbookHomeSet struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-home-set"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-home-set"`
Href string `xml:"href"` Href string `xml:"href"`

View File

@ -111,7 +111,7 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i
func (b *backend) propfindAddressBook(propfind *internal.Propfind) (*internal.Response, error) { func (b *backend) propfindAddressBook(propfind *internal.Propfind) (*internal.Response, error) {
props := map[xml.Name]internal.PropfindFunc{ props := map[xml.Name]internal.PropfindFunc{
{"DAV:", "resourcetype"}: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) {
return internal.NewResourceType(internal.CollectionName, addressBookName), nil return internal.NewResourceType(internal.CollectionName, addressBookName), nil
}, },
} }
@ -121,10 +121,7 @@ func (b *backend) propfindAddressBook(propfind *internal.Propfind) (*internal.Re
func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *AddressObject) (*internal.Response, error) { func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *AddressObject) (*internal.Response, error) {
props := map[xml.Name]internal.PropfindFunc{ props := map[xml.Name]internal.PropfindFunc{
{"DAV:", "resourcetype"}: func(*internal.RawXMLValue) (interface{}, error) { internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) {
return internal.NewResourceType(), nil
},
{"DAV:", "getcontenttype"}: func(*internal.RawXMLValue) (interface{}, error) {
return &internal.GetContentType{Type: vcard.MIMEType}, nil return &internal.GetContentType{Type: vcard.MIMEType}, nil
}, },
// TODO getlastmodified, getetag // TODO getlastmodified, getetag

View File

@ -9,6 +9,8 @@ import (
"time" "time"
) )
const Namespace = "DAV:"
type Status struct { type Status struct {
Code int Code int
Text string Text string
@ -265,6 +267,14 @@ type GetLastModified struct {
LastModified Time `xml:",chardata"` LastModified Time `xml:",chardata"`
} }
var (
ResourceTypeName = xml.Name{"DAV:", "resourcetype"}
GetContentLengthName = xml.Name{"DAV:", "getcontentlength"}
GetContentTypeName = xml.Name{"DAV:", "getcontenttype"}
GetLastModifiedName = xml.Name{"DAV:", "getlastmodified"}
GetETagName = xml.Name{"DAV:", "getetag"}
)
// https://tools.ietf.org/html/rfc4918#section-14.5 // https://tools.ietf.org/html/rfc4918#section-14.5
type Error struct { type Error struct {
XMLName xml.Name `xml:"DAV: error"` XMLName xml.Name `xml:"DAV: error"`

View File

@ -137,7 +137,7 @@ func (b *backend) propfind(propfind *internal.Propfind, name string, fi os.FileI
func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.FileInfo) (*internal.Response, error) { func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.FileInfo) (*internal.Response, error) {
props := make(map[xml.Name]internal.PropfindFunc) props := make(map[xml.Name]internal.PropfindFunc)
props[xml.Name{"DAV:", "resourcetype"}] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.ResourceTypeName] = func(*internal.RawXMLValue) (interface{}, error) {
var types []xml.Name var types []xml.Name
if fi.IsDir() { if fi.IsDir() {
types = append(types, internal.CollectionName) types = append(types, internal.CollectionName)
@ -146,10 +146,10 @@ func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.F
} }
if !fi.IsDir() { if !fi.IsDir() {
props[xml.Name{"DAV:", "getcontentlength"}] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) {
return &internal.GetContentLength{Length: fi.Size()}, nil return &internal.GetContentLength{Length: fi.Size()}, nil
} }
props[xml.Name{"DAV:", "getcontenttype"}] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentTypeName] = func(*internal.RawXMLValue) (interface{}, error) {
t := mime.TypeByExtension(path.Ext(name)) t := mime.TypeByExtension(path.Ext(name))
if t == "" { if t == "" {
// TODO: use http.DetectContentType // TODO: use http.DetectContentType
@ -157,7 +157,7 @@ func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.F
} }
return &internal.GetContentType{Type: t}, nil return &internal.GetContentType{Type: t}, nil
} }
props[xml.Name{"DAV:", "getlastmodified"}] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) {
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime())}, nil return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime())}, nil
} }
// TODO: getetag // TODO: getetag