diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2018-02-05 22:06:18 +0100 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2018-02-05 22:06:18 +0100 |
| commit | 735c7c4465a77d2953e9cb1328284e754f797db7 (patch) | |
| tree | 0df31162a4b0b769f2ffe641bd7ae4072b7224cf /headers.go | |
| parent | 3a24741929cd12ffee5e54d0e9a9afb83c5069b3 (diff) | |
| download | go-schwift-735c7c4465a77d2953e9cb1328284e754f797db7.tar.gz | |
apparently having a private member is not enough
Diffstat (limited to 'headers.go')
| -rw-r--r-- | headers.go | 27 |
1 files changed, 25 insertions, 2 deletions
@@ -19,6 +19,7 @@ package schwift import ( + "fmt" "reflect" "github.com/majewsky/schwift/headers" @@ -29,6 +30,9 @@ import ( //request on the account, and will be sent by a PUT or POST request. The other //attributes allow type-safe access to well-known headers, as noted in the tags //next to each field. +// +//Follow the link on the Headers attribute for the documentation of the Get(), +//Set(), Del(), Clear() methods on this type. type AccountHeaders struct { headers.Headers BytesUsed headers.Uint64Readonly `schwift:"X-Account-Bytes-Used"` @@ -39,14 +43,18 @@ type AccountHeaders struct { TempURLKey headers.String `schwift:"X-Account-Meta-Temp-URL-Key"` TempURLKey2 headers.String `schwift:"X-Account-Meta-Temp-URL-Key-2"` //forbid initialization as struct literal (must use NewAccountHeaders) - private struct{} + initialized bool } //NewAccountHeaders prepares a new AccountHeaders instance. +// +//WARNING: Always use this function to construct AccountHeaders instances. +//Failure to do so will result in uncontrolled crashes! func NewAccountHeaders() AccountHeaders { var ah AccountHeaders ah.Headers = make(headers.Headers) initializeByReflection(&ah) + ah.initialized = true return ah } @@ -64,19 +72,26 @@ func (ah AccountHeaders) Validate() error { //request on the container, and will be sent by a PUT or POST request. The //other attributes allow type-safe access to well-known headers, as noted in //the tags next to each field. +// +//Follow the link on the Headers attribute for the documentation of the Get(), +//Set(), Del(), Clear() methods on this type. type ContainerHeaders struct { headers.Headers Metadata headers.Metadata `schwift:"X-Container-Meta-"` //TODO map well-known headers //forbid initialization as struct literal (must use NewContainerHeaders) - private struct{} + initialized bool } //NewContainerHeaders prepares a new ContainerHeaders instance. +// +//WARNING: Always use this function to construct ContainerHeaders instances. +//Failure to do so will result in uncontrolled crashes! func NewContainerHeaders() ContainerHeaders { var ch ContainerHeaders ch.Headers = make(headers.Headers) initializeByReflection(&ch) + ch.initialized = true return ch } @@ -122,6 +137,14 @@ func validateByReflection(value interface{}) error { }) } +func ensureInitializedByReflection(value interface{}) { + initialized := reflect.ValueOf(value).FieldByName("initialized").Bool() + if !initialized { + msg := "values of type %T MUST be initialized with the corresponding New...() function" + panic(fmt.Sprintf(msg, value, value)) + } +} + func foreachTaggedField(value interface{}, callback func(fieldPtr interface{}, info fieldInfo) error) error { rv := reflect.ValueOf(value).Elem() |
