metadata

package module
v0.0.0-...-494a34a Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2017 License: MIT Imports: 6 Imported by: 2

README

Metadata

Metadata can inspect types and return JSON response for OPTIONS requests. All api is chainable, but warning it changes data inplace.

Example:

    type Product struct {
        Name string `json:"name"`
    }

    metadata := New("Product detail").Description("Basic information about product")
    metadata.Action(ACTION_CREATE).From(ProductNew{})
    metadata.Action(ACTION_DELETE)
    metadata.Action(ACTION_RETRIEVE).Field("result").From(Product{})

This will yield to this when json marshalled

    {
        "name": "test endpoint",
        "description": "description",
        "actions": {
            "POST": {
                "type": "struct",
                "fields": {
                    "name": {
                        "type": "string",
                    }
                }
            }
        }
    }

You can even describe more complicated structures

    type User struct {
        Username string `json:"username"`
    }

    md := New("Product detail").Description("Basic information about product")
    md.Action(ACTION_RETRIEVE).Field("result", "user").From(Product{})

Which then accepts structure like this:

{
  "result": {
    "user": {
      "username": "phonkee"
    }
  }
}

Fields also support choices so you can do this

    md := New()
    md.Action(ACTION_CREATE).Field("status").Choices().Add(1, "new").Add(2, "active").Add(3, "closed")

TODO:

add support for custom validators such as min max value and other.

Contribute:

Your contribution is welcome, feel free to send PR.

Documentation

Overview

@TODO: add more documentation to this code, since it is more advanced code and can bring misunderstandnis.

Source provides way how to specify choices rest endpoint for given field

This is proposal:

md := New()
userlist := md.Action(ACTION_POST).Field("result").From([]User{})
statusfield := userlist.Field("status")
statusfield.
    Source("/api/user/status").
    Action(action).
    Mapping("result", "value", "display")

Index

Constants

View Source
const (
	ACTION_CREATE   = "POST"
	ACTION_UPDATE   = "POST"
	ACTION_RETRIEVE = "GET"
	ACTION_DELETE   = "DELETE"
)
View Source
const (
	SOURCE_DEFAULT_VALUE_FIELD   = "value"
	SOURCE_DEFAULT_DISPLAY_FIELD = "display"
)
View Source
const (
	FIELD_INVALID          = "invalid"
	FIELD_ARRAY            = "array"
	FIELD_STRUCT           = "struct"
	FIELD_MAP              = "map"
	FIELD_INTEGER          = "integer"
	FIELD_UNSIGNED_INGETER = "unsigned"
	FIELD_STRING           = "string"
	FIELD_BOOLEAN          = "boolean"
	FIELD_FLOAT            = "float"
	FIELD_DATETIME         = "datetime"
)

Variables

Functions

func Debug

func Debug()

Debug enables global debug for go-metadata

func ParseQuery

func ParseQuery(query string, action Action) (err error)

ParseQuery parses query and udpates action query has following format

var1=type&var2=type

Where type is type of Field

Example:

q=string&page=integer

func ParseTag

func ParseTag(tag string) (string, tagOptions)

parseTag splits a struct field's json tag into its name and comma-separated options.

func RegisterKind

func RegisterKind(f FieldTypeFunc, kind ...reflect.Kind)

register kinds

func RegisterType

func RegisterType(f FieldTypeFunc, values ...interface{}) (err error)

RegisterType provides register for custom types (e.g. time.Time)

Types

type Action

type Action interface {

	// Description sets description for action
	Description(string) Action

	// GetDescription returns description
	GetDescription() string

	// Field adds or retrieves fields (recursively)
	Field(...string) Field

	// HasField returns whether field is set
	HasField(names ...string) bool

	// GetFieldNames returns names of all fields
	GetFieldNames() []string

	// From inspects given value and makes appropriate steps
	From(v interface{}) Action

	// GetData returns dynamic data (for json etc..)
	GetData() map[string]interface{}

	// MarshalJSON satisfies json marshaller
	MarshalJSON() ([]byte, error)

	// Debug enables debugging
	Debug() Action

	// ParseQueryParam parses
	ParseQueryParam(query string) Action

	// QueryParam returns or adds new query param
	QueryParam(name string) Field

	// RemoveQueryParam removes query param
	RemoveQueryParam(name string) Action

	// GetQueryParamNames returns names of all available query params
	GetQueryParamNames() []string
	// contains filtered or unexported methods
}

Action interface that describes single action (or http method)

func NewAction

func NewAction() Action

NewAction creates fresh new action

type Choice

type Choice struct {
	Value   interface{} `json:"value"`
	Display string      `json:"display"`
}

choice type

type Choices

type Choices interface {
	// Add adds new choice
	Add(value interface{}, display string) Choices

	// returns choices count
	Count() int

	// Support for marshalling
	MarshalJSON() ([]byte, error)
}

Choices interface

type Field

type Field interface {

	// Field method returns field by names (recursively)
	Field(...string) Field

	// returns whether Field has field with given name
	HasField(...string) bool

	// return whether we have fields
	NumFields() int

	// GetFieldNames returns list of fields
	GetFieldNames() []string

	// Description sets help text for given field
	Description(string) Field

	// GetDescription is getter for HelpText
	GetDescription() string

	// Label sets label of field
	Label(string) Field

	// GetLabel is getter for Label
	GetLabel() string

	// RemoveField removes field from fields
	RemoveField(string) (self Field)

	// set required flag to field
	Required(bool) (self Field)

	// returns whether field is required
	IsRequired() bool

	// type is type of field
	Type(string) Field

	// GetType returns field type
	GetType() string

	// From reads target and sets data to field
	From(interface{}) Field

	// Data returns data representation in dynamic fashion (interface{})
	GetData() map[string]interface{}

	// MarshalJSON satisfies json marshaller
	MarshalJSON() ([]byte, error)

	// Choices returns choices
	Choices() Choices

	// Source
	Source(path string) Source

	// Debug enables debugging for metadata
	Debug() Field
	// contains filtered or unexported methods
}

Field interface

type FieldTypeFunc

type FieldTypeFunc func(reflect.Type) Field

field type func returns Field by reflect value

type Metadata

type Metadata interface {
	// action returns either existing or newly created action
	Action(method string) Action

	// RemoveAction removes action
	RemoveAction(method string) Metadata

	// Name sets name to metadata
	Name(string) Metadata

	// GetName returns name of metadata
	GetName() string

	// Description sets description of metadata
	Description(string) Metadata

	// GetDescription returns description
	GetDescription() string

	// GetData returns dynamic data (for json etc..)
	GetData() map[string]interface{}

	// MarshalJSON satisfies json marshaller
	MarshalJSON() ([]byte, error)

	// Debug enables debugging for metadata
	Debug() Metadata
	// contains filtered or unexported methods
}

MetaData interface that describes heep endpoint methods

func New

func New(label ...string) (result Metadata)

New returns new metadata instance

type Source

type Source interface {

	// Debug sets debug to source
	Debug() Source

	// Action sets action
	Action(action Action) Source

	// GetAction Returns action
	GetAction() Action

	// ResultField points to resultField
	Result(field ...string) Source

	// IsValid returns whether source is setup correct
	IsValid() bool

	// Value sets value field name
	Value(value string) Source

	// GetValue returns value field name
	GetValue() string

	// Display sets display field name
	Display(display string) Source

	// GetDisplay returns display field name
	GetDisplay() string

	// Path sets path to source
	Path(string) Source

	// GetPath returns path set for this source
	GetPath() string

	// Return data
	GetData() map[string]interface{}

	// MarshalJSON satisfies json marshal interface
	MarshalJSON() ([]byte, error)
	// contains filtered or unexported methods
}

Source is source for field value. This describes rest endpoint together with (optional) Metadata information.

Jump to

Keyboard shortcuts

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