objects

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2020 License: GPL-2.0 Imports: 7 Imported by: 0

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 three 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.

3. Implementing custom-types that are supported implicitly by gondulapi/db.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Thing

type Thing struct {
	Sysname   string
	MgmtIP    *types.IP `column:"ip"`
	Placement *ThingPlacement
}

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.

func (Thing) Delete

func (b Thing) Delete(element string) error

Delete is called to delete an element.

func (*Thing) Get

func (b *Thing) Get(element string) error

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) Post

func (b Thing) Post() error

Post stores the provided object. Unlike PUT, it will always use INSERT.

func (Thing) Put

func (b Thing) Put(element string) error

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.

type ThingPlacement

type ThingPlacement struct {
	X1 int
	X2 int
	Y1 int
	Y2 int
}

ThingPlacement illustrates nested data structures which are implicitly handled, and, also mimicks the placement logic of switches in Gondul, which use an X1/X2 Y1/Y2 coordinate system to determine left-most and right-most X and top-most and bottom-most Y.

Please note that Postgresql will always place the upper right corner first - so X1/Y1 might be X2/Y2 when you read it back out - but the semantics are correct.

func (*ThingPlacement) Scan added in v0.0.4

func (tp *ThingPlacement) Scan(src interface{}) error

Scan implements sql.Scan to enable gondulapi/db to read the SQL directly into our custom type.

func (ThingPlacement) Value added in v0.0.4

func (tp ThingPlacement) Value() (driver.Value, error)

Value implements sql/driver's Value interface to provide implicit support for writing the value.

In other words: Scan() enables SELECT and Value() allows INSERT/UPDATE.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL