// Package models provides the models for the KermaGo application package models import ( "errors" "regexp" "github.com/docker/go/canonical/json" ) /* A Hello represents a struct that has a type, a version and a user agent */ type Hello struct { Type string `json:"type" binding:"required"` Version string `json:"version" binding:"required"` Agent string `json:"agent"` } /* Construct creates a new Hello object for this instance */ func (h *Hello) Construct() { h.Type = "hello" h.Version = "0.8.0" h.Agent = "BadKerma Go Client 0.8.x" } /* MarshalJson returns the canonical json of the hello object */ func (h *Hello) MarshalJson() ([]byte, error) { return json.MarshalCanonical(h) } /* UnmarshalJSON returns a new Hello request from a byte array */ func (h *Hello) UnmarshalJSON(data []byte) error { if len(data) == 0 { return nil } type tmp Hello err := json.Unmarshal(data, (*tmp)(h)) if err != nil { return err } if !h.verify() { return errors.New("hello request not valid") } return nil } func (h *Hello) verify() bool { if h.Type == "hello" && h.isValidVersion() { return true } return false } func (h *Hello) isValidVersion() bool { RegExp := regexp.MustCompile(`^0\.8\.[0-9]$`) return RegExp.MatchString(h.Version) }