Documentation
¶
Overview ¶
Package objects implements a number of objects. Currently just one, though, and a dummy-one at that.
It is intended to be imported for side effects, but there's no reason these objects can't be exported like any other.
An object is a data structure that implements one of Putter, Getter, Poster and Deleter from gondulapi to do RESTful stuff.
This file demonstrates two things:
1. How to implement basic GET/PUT/POST/DELETE using gondulapi/receiver.
2. How to use the simplified gondulapi/db methods to map your own interface directly to a database element without writing SQL.
See types/ for how to implement custom-types not already supported by database/sql.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Thing ¶
Thing is a dummy structure to illustrate the core GET/PUT/POST/DELETE API.
It mimics a common pattern where an object also contains its own name.
We use a tag to inform gondulapi/db that MgmtIP is mapped to the "ip" column in the database.
Special note here: The only way for Go to distinguish between "The variable was not provided" and "the variable was provided, but set to the zero-value" is if the field is a pointer. That means that if Vlan is a nil-pointer in Put, the client didn't specify it and we should not modify it. If, however, it is a allocated, pointing to int<0>, it was provided, but set to 0. This is important: If you change Vlan to "Vlan int" instead of "Vlan *int", two things will happen:
1. If a client doesn't provide vlan on an update with PUT, we will still set it to 0.
2. If a row in the database has vlan set to NULL, we will fail to scan that since NULL can not be converted into an int.
In general, that means you want pointers.
It also implies that you should use constraints on your database to ensure that the values are legal. Set NOT NULL if it shouldn't be null. Set default values if it's OK to omit it on INSERT, and so on.
func (*Thing) Get ¶
Get is called on GET. b will be an empty thing. Fill it out, using the element to determine what we're looking for. If it fails: return an error. Simple.
func (Thing) Put ¶
Put is used to store an element with an absolute URL. In our case, the name of the element is also (potentially) present in the data it self - so we do a bit of magic. Note that this should NEVER generate a random name.
b will contain the parsed data. element will be the name of the thing.
PUT is idempotent. Calling it once with a set of parameters or a hundred times with the same parameters should yield the same result.