attributes

package
v1.4.5-alpha1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2019 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package attributes represents an implementation of InterfaceCustomAttributes declared in "github.com/ottemo/commerce/app/models" package.

In order to use it you should just embed ModelCustomAttributes in your actor, you can found sample usage in "github.com/app/actors/product" package.

Index

Examples

Constants

View Source
const (
	ConstCollectionNameCustomAttributes = "custom_attributes"

	ConstErrorModule = "attributes"
	ConstErrorLevel  = env.ConstErrorLevelHelper
)

Package global constants

Variables

This section is empty.

Functions

This section is empty.

Types

type ModelCustomAttributes

type ModelCustomAttributes struct {
	// contains filtered or unexported fields
}

ModelCustomAttributes type represents a set of attributes which could be modified (edited/added/removed) dynamically. The implementation relays on InterfaceCollection which is used to store values and have ability to add/remove columns on a fly.

func CustomAttributes

func CustomAttributes(model string, collection string) (*ModelCustomAttributes, error)

CustomAttributes initializes helper instance before usage

func (*ModelCustomAttributes) AddNewAttribute

func (it *ModelCustomAttributes) AddNewAttribute(newAttribute models.StructAttributeInfo) error

AddNewAttribute extends collection with new custom attribute

func (*ModelCustomAttributes) EditAttribute

func (it *ModelCustomAttributes) EditAttribute(attributeName string, attributeValues models.StructAttributeInfo) error

EditAttribute modifies custom attribute for collection

func (*ModelCustomAttributes) FromHashMap

func (it *ModelCustomAttributes) FromHashMap(input map[string]interface{}) error

FromHashMap represents object as map[string]interface{}

func (*ModelCustomAttributes) Get

func (it *ModelCustomAttributes) Get(attribute string) interface{}

Get returns object attribute value or nil

func (*ModelCustomAttributes) GetAttributesInfo

func (it *ModelCustomAttributes) GetAttributesInfo() []models.StructAttributeInfo

GetAttributesInfo represents object as map[string]interface{}

func (*ModelCustomAttributes) GetCustomAttributeCollectionName

func (it *ModelCustomAttributes) GetCustomAttributeCollectionName() string

GetCustomAttributeCollectionName returns collection name you can use to fill ModelCustomAttributes struct

func (*ModelCustomAttributes) GetInstance

func (it *ModelCustomAttributes) GetInstance() interface{}

GetInstance returns current instance delegate attached to

func (*ModelCustomAttributes) RemoveAttribute

func (it *ModelCustomAttributes) RemoveAttribute(attributeName string) error

RemoveAttribute removes custom attribute from collection

func (*ModelCustomAttributes) Set

func (it *ModelCustomAttributes) Set(attribute string, value interface{}) error

Set sets attribute value to object or returns error

func (*ModelCustomAttributes) ToHashMap

func (it *ModelCustomAttributes) ToHashMap() map[string]interface{}

ToHashMap fills object attributes from map[string]interface{}

type ModelExternalAttributes

type ModelExternalAttributes struct {
	// contains filtered or unexported fields
}

ModelExternalAttributes type represents a set of attributes managed by "external" package (outside of model package) which supposing at least InterfaceObject methods delegation, but also could delegate InterfaceStorable if the methods are implemented in delegate instance.

Workflow diagram:

               helper makes proxy for Object/Storable interface methods:
                            Get(), Set(), ListAttributes(),
      +---------------+       FromHashMap(), ToHashMap(),       +------------------+
      | Model Package |    GetId(), Load(), Save(), Delete()    | External package |
      |               |                                         |                  |
      |   +-------+   |                                         |   +----------+   |
+---------> Model <---------------------+ +-------------------------> Delegate |   |
|     |   +---+---+   |   model-helper  | |   helper-delegate   |   +----^-----+   |
|     |       |       |    proxy call   | |      proxy call     |        |         |
|     +-------|-------+                 | |                     +--------|---+-----+
|             |                         | |                              |   |
|             |   instance    +---------v-v--------------+               |   |
|             +---------------> *ModelExternalAttributes ----------------+   |
|   Model.New() instantiates  +-- -+---------------------+   Delegate.New()  |  Registering delegate
|   ExternalAttributes helper      |                         instantiates    |  for a specific model
|   which transfers instance       |                           delegate      |
+----------------------------------+-GetInstance()                           |
   helper have reference to model  |                                         |
                                   +-AddExternalAttributes() <---------------+
                                   +-RemoveExternalAttributes()
                                   |
                                   +-ListExternalAttributes()

func ExternalAttributes

func ExternalAttributes(instance interface{}) (*ModelExternalAttributes, error)

ExternalAttributes initializes helper for model instance - should be called in model.New()

  • "instance" is a reference to object which using helper
Example

ExampleExternalAttributes creates 2 instances of SampleModel

package main

import (
	"errors"
	"fmt"
	"github.com/ottemo/commerce/app/models"
	"github.com/ottemo/commerce/utils"
)

// -----------------------
// SampleModel declaration
// -----------------------

// SampleModel type implements InterfaceModel only interface, so it does not
// have any attribute at the beginning, it even don't have InterfaceObject and
// InterfaceStorable methods implementation, however they are available via
// embed ModelExternalAttributes type
type SampleModel struct {
	i string
	l string
	d bool
	s bool

	*ModelExternalAttributes
}

// GetModelName returns model name
func (it *SampleModel) GetModelName() string {
	return "Example"
}

// GetImplementationName returns model implementation name
func (it *SampleModel) GetImplementationName() string {
	return "ExampleObject"
}

// New constructor for a new instance
func (it *SampleModel) New() (models.InterfaceModel, error) {
	var err error

	newInstance := new(SampleModel)
	newInstance.ModelExternalAttributes, err = ExternalAttributes(newInstance)

	return newInstance, err
}

// --------------------
// Delegate declaration
// --------------------

// SampleDelegate type implements InterfaceAttributesDelegate and have handles
// on InterfaceStorable methods which should have call-back on model method call
// in order to test it we are pushing the callback status to model instance
type SampleDelegate struct {
	instance interface{}
	a        string
	b        float64
}

// New instantiates delegate
func (it *SampleDelegate) New(instance interface{}) (models.InterfaceAttributesDelegate, error) {
	return &SampleDelegate{instance: instance}, nil
}

// Get is a getter for external attributes
func (it *SampleDelegate) Get(attribute string) interface{} {
	switch attribute {
	case "a":
		return it.a
	case "b":
		return it.b
	}
	return nil
}

// Set is a setter for external attributes
func (it *SampleDelegate) Set(attribute string, value interface{}) error {
	switch attribute {
	case "a":
		it.a = utils.InterfaceToString(value)
	case "b":
		it.b = utils.InterfaceToFloat64(value)
	}
	return nil
}

// Load is a modelInstance.Load() method handler for external attributes
func (it *SampleDelegate) Load(id string) error {
	it.instance.(*SampleModel).l = id
	return nil
}

// Delete is a modelInstance.Delete() method handler for external attributes
func (it *SampleDelegate) Delete() error {
	it.instance.(*SampleModel).d = true
	return nil
}

// Save is a modelInstance.Save() method handler for external attributes
func (it *SampleDelegate) Save() error {
	it.instance.(*SampleModel).s = true
	return nil
}

// SetID is a modelInstance.SetID() method handler for external attributes
func (it *SampleDelegate) SetID(newID string) error {
	it.instance.(*SampleModel).i = newID
	return nil
}

// GetAttributesInfo is a specification of external attributes
func (it *SampleDelegate) GetAttributesInfo() []models.StructAttributeInfo {
	return []models.StructAttributeInfo{
		models.StructAttributeInfo{
			Model:      "",
			Collection: "",
			Attribute:  "a",
			Type:       utils.ConstDataTypeText,
			Label:      "A",
			IsRequired: false,
			IsStatic:   false,
			Group:      "Sample",
			Editors:    "text",
		},
		models.StructAttributeInfo{
			Model:      "Example",
			Collection: "",
			Attribute:  "b",
			Type:       utils.ConstDataTypeFloat,
			Label:      "B",
			IsRequired: false,
			IsStatic:   false,
			Group:      "Sample",
			Editors:    "text",
		},
	}
}

// ExampleExternalAttributes creates 2 instances of SampleModel
func main() {
	// registering SampleDelegate for SampleModel on attributes "a" and "b"
	modelInstance, err := new(SampleModel).New()
	if err != nil {
		panic(err)
	}

	modelEA, ok := modelInstance.(models.InterfaceExternalAttributes)
	if !ok {
		panic(errors.New("InterfaceExternalAttributes not impelemented"))
	}

	delegate := new(SampleDelegate)
	if err := modelEA.AddExternalAttributes(delegate); err != nil {
		panic(err)
	}

	// testing result: setting "a", "b" attributes to SampleModel instances and getting them back
	var obj1, obj2 models.InterfaceObject
	if x, err := modelInstance.New(); err == nil {
		if obj1, ok = x.(models.InterfaceObject); !ok {
			panic(errors.New("InterfaceObject not impelemented"))
		}
	} else {
		panic(err)
	}

	if x, err := modelInstance.New(); err == nil {
		if obj2, ok = x.(models.InterfaceObject); !ok {
			panic(errors.New("InterfaceObject not impelemented"))
		}
	} else {
		panic(err)
	}

	if err = obj1.Set("a", "object1"); err != nil {
		panic(err)
	}
	if err = obj2.Set("a", "object2"); err != nil {
		panic(err)
	}
	if err = obj1.Set("b", 1.2); err != nil {
		panic(err)
	}
	if err = obj2.Set("b", 3.3); err != nil {
		panic(err)
	}

	if obj1.Get("a") != "object1" || obj1.Get("b") != 1.2 ||
		obj2.Get("a") != "object2" || obj2.Get("b") != 3.3 {
		panic(errors.New(fmt.Sprint("incorrect get values: "+
			"obj1.a=", obj1.Get("a"), ", ",
			"obj1.b=", obj1.Get("b"), ", ",
			"obj2.a=", obj2.Get("a"), ", ",
			"obj2.b=", obj2.Get("b"),
		)))
	}

	if obj1, ok := obj1.(models.InterfaceStorable); ok {
		if err := obj1.Load("1"); err != nil {
			panic(err)
		}
		if err := obj1.Save(); err != nil {
			panic(err)
		}
		if err := obj1.Delete(); err != nil {
			panic(err)
		}
		if err := obj1.SetID("10"); err != nil {
			panic(err)
		}
	} else {
		panic(errors.New("models.InterfaceStorable not implemented"))
	}

	if obj1, ok := obj1.(*SampleModel); ok {
		if !obj1.d || !obj1.s || obj1.l != "1" || obj1.i != "10" {
			panic(errors.New(fmt.Sprint("incorrect get values: "+
				"obj1.l=", obj1.l, ", ",
				"obj1.s=", obj1.s, ", ",
				"obj1.d=", obj1.d, ", ",
				"obj1.i=", obj1.i,
			)))
		}
	} else {
		panic(errors.New("(*SampleModel) conversion error"))
	}
}
Output:

func (*ModelExternalAttributes) AddExternalAttributes

func (it *ModelExternalAttributes) AddExternalAttributes(delegate models.InterfaceAttributesDelegate) error

AddExternalAttributes registers new delegate for a it's attributes - delegate.GetAttributesInfo()

func (*ModelExternalAttributes) Delete

func (it *ModelExternalAttributes) Delete() error

Delete proxies method to external attribute delegates

func (*ModelExternalAttributes) FromHashMap

func (it *ModelExternalAttributes) FromHashMap(input map[string]interface{}) error

FromHashMap updates delegated attributes from given map

func (*ModelExternalAttributes) Get

func (it *ModelExternalAttributes) Get(attribute string) interface{}

Get returns object attribute value or nil

func (*ModelExternalAttributes) GetAttributesInfo

func (it *ModelExternalAttributes) GetAttributesInfo() []models.StructAttributeInfo

GetAttributesInfo represents object as map[string]interface{}

func (*ModelExternalAttributes) GetID

func (it *ModelExternalAttributes) GetID() string

GetID stub function - callback to instance getID()

func (*ModelExternalAttributes) GetImplementationName

func (it *ModelExternalAttributes) GetImplementationName() string

GetImplementationName stub func for Model interface - doing callback to model instance function if possible

func (*ModelExternalAttributes) GetInstance

func (it *ModelExternalAttributes) GetInstance() interface{}

GetInstance returns current instance delegate attached to

func (*ModelExternalAttributes) GetModelName

func (it *ModelExternalAttributes) GetModelName() string

GetModelName stub func for Model interface - returns model name

func (*ModelExternalAttributes) ListExternalAttributes

func (it *ModelExternalAttributes) ListExternalAttributes() map[string]models.InterfaceAttributesDelegate

ListExternalAttributes returns delegate per attribute mapping

func (*ModelExternalAttributes) Load

func (it *ModelExternalAttributes) Load(id string) error

Load proxies method to external attribute delegates

func (*ModelExternalAttributes) RemoveExternalAttributes

func (it *ModelExternalAttributes) RemoveExternalAttributes(delegate models.InterfaceAttributesDelegate) error

RemoveExternalAttributes removes the delegate for it's attributes - delegate.GetAttributesInfo()

func (*ModelExternalAttributes) Save

func (it *ModelExternalAttributes) Save() error

Save proxies method to external attribute delegates

func (*ModelExternalAttributes) Set

func (it *ModelExternalAttributes) Set(attribute string, value interface{}) error

Set sets attribute value to object or returns error

func (*ModelExternalAttributes) SetID

func (it *ModelExternalAttributes) SetID(id string) error

SetID proxies method to external attribute delegates

func (*ModelExternalAttributes) ToHashMap

func (it *ModelExternalAttributes) ToHashMap() map[string]interface{}

ToHashMap returns delegated attributes in map

Jump to

Keyboard shortcuts

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