Documentation ¶
Overview ¶
Package maptrans provides a generic map to manipulate dictionaries.
There are many cases where we have some JSON object and we want to convert it to another JSON object. Since Go is strongly typed, a useful approach is to first translate JSON object into a map from string to an interface. Such map is called Maptrans. We can then define a translation of one Maptrans into another Maptrans. Translations are defined by the specially constructed Maptrans.
Translation Types ¶
Translating field to another field with a different name ¶
The simplest case is when we take a field from one object and present it in the result under a different name. In this case we just write the source and destination strings.
Example of name conversions
var someMap map[string]interface{} = map[string]interface{}{ "uuid": "UUID", "name": "Name", "age": "Age", }
Translating field to another field and changing value.
We can provide a function which will translate field value to another value. The function can also perform some verification of the input. For this we need to describe convewrsion using MapElement object which is defined as
type MapElement struct { TargetName string // Name of destination field MapFunc MapFunc // Function that value to new value ModFunc ModFunc // Function for object modification Type TranslationType // Type of translation Mandatory bool // The field must be present if true SubTranslation ObjectMapDescription // Subtranslation map for children }
There are several predefined MapFunc translators:
- IDMap translates any value to itself. It can be used to map common objects to themselves.
- StringMap translates string to a string, trimming leading and trailing spaces.
- StringToLowerMap translates a string to lower-case string (and trims spaces).
- IdentifierMap does a string translation but rejects invalid identifiers. An identifier should start with a letter or underscore and have only letters, digits and underscores in it.
- IPAddrMap does a string translation of IP addresses which should be valid.
- CIDRMap does a string translation of IP addresses in a slash notation, e.g . 1.2.3.4/24
- BoolMap converts boolean or string to a boolean.
- UUIDMap converts string to a string verifying that the source string is a valid UUID
- StringArrayMap converts array of strings into another array of strings.
When Mandatory field is specified, the field must be present in the source object.
Translating maps to maps ¶
To translate one map into asnother, the Type should be specified as ObjectTranslationn. The SubTranslation is the translation specification for the internal object.
Translating array of objects.
To translate an arary of objects into another array of objects, the Type should be specified as ObjectArrayTranslation. The SubTranslation defines translation for each element of an array.
Using values to modify the original objects.
Example JSON object
{ "name": "myname" "value": { "fruit": "apple" } }
If we want to present this as a "flat" object
{ "name": "myname" "fruit": "apple" }
we need a ObjectArrayTranslation method.
Example
var translationDescr = map[string]interface{}{ "name": "Name" "uuid": maptrans.MapElement{ TargetName: "UUID", Mandatory: true, MapFunc: maptrans.UUIDMap, }, "alias": maptrans.MapElement{ TargetName: "Alias", MapFunc: maptrans.IdentifierMap, }, "force": maptrans.MapElement{ TargetName: "Force", MapFunc: maptrans.BoolMap, }, "info": maptrans.MapElement{ TargetName: "Info", Mandatory: true, Type: maptrans.ObjectTranslation, SubTranslation: map[string]interface{}{ "Port": maptrans.MapElement{ Name: "port", Mandatory: false, MapFunc: maptrans.IntegerMap, }, "IPAddress": maptrans.MapElement{ TargetName: "address", Mandatory: true, MapFunc: maptrans.CIDRMap, }, "Route": maptrans.MapElement{ TargetName: "route", Type: maptrans.ObjectArrayTranslation, Mandatory: false, SubTranslation: map[string]interface{}{ "Destination": maptrans.MapElement{ TargetName: "destination", Mandatory: true, MapFunc: maptrans.CIDRMap, }, "Gateway": maptrans.MapElement{ TargetName: "gateway", Mandatory: true, MapFunc: maptrans.IPAddrMap, }, }, }, }, }, }
Index ¶
- func BoolMap(src interface{}) (interface{}, error)
- func BoolToStrMap(src interface{}) (interface{}, error)
- func CIDRMap(src interface{}) (interface{}, error)
- func IDMap(src interface{}) (interface{}, error)
- func IPAddrMap(src interface{}) (interface{}, error)
- func IdentifierMap(src interface{}) (interface{}, error)
- func IntegerMap(val interface{}) (interface{}, error)
- func IsSimilar(src map[string]interface{}, dst map[string]interface{}, ...) (bool, error)
- func StringArrayMap(src interface{}) (interface{}, error)
- func StringMap(src interface{}) (interface{}, error)
- func StringToLowerMap(src interface{}) (interface{}, error)
- func StringToUpperMap(src interface{}) (interface{}, error)
- func Translate(src map[string]interface{}, description map[string]interface{}) (map[string]interface{}, error)
- func UUIDMap(src interface{}) (interface{}, error)
- type Description
- type InsertFunc
- type InternalError
- type InvalidPropertyError
- type MapFunc
- type MissingAttributeError
- type ModFunc
- type TranslationType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolMap ¶
func BoolMap(src interface{}) (interface{}, error)
BoolMap translates boolean interface into a boolean
func BoolToStrMap ¶
func BoolToStrMap(src interface{}) (interface{}, error)
BoolToStrMap translates boolean interface into a string
func CIDRMap ¶
func CIDRMap(src interface{}) (interface{}, error)
CIDRMap verifies that the argument is a valid IP address in CIDR notation notation
func IDMap ¶
func IDMap(src interface{}) (interface{}, error)
IDMap translates an object to itself. This is the easiest way to deal with embedded objects.
func IPAddrMap ¶
func IPAddrMap(src interface{}) (interface{}, error)
IPAddrMap verifies that the argument is a valid IP address
func IdentifierMap ¶
func IdentifierMap(src interface{}) (interface{}, error)
IdentifierMap is similar to StringMap but verifies that the string contains only valid characters for identifiers
func IntegerMap ¶
func IntegerMap(val interface{}) (interface{}, error)
IntegerMap Converts numbers to strings
func IsSimilar ¶
func IsSimilar(src map[string]interface{}, dst map[string]interface{}, descr map[string]interface{}) (bool, error)
IsSimilar verifies that dst object matches src object according to description
func StringArrayMap ¶
func StringArrayMap(src interface{}) (interface{}, error)
StringArrayMap translates array of strings
func StringMap ¶
func StringMap(src interface{}) (interface{}, error)
StringMap translates string interface into a string (trimming spaces)
func StringToLowerMap ¶
func StringToLowerMap(src interface{}) (interface{}, error)
StringToLowerMap translates string interface into a string with lower case
func StringToUpperMap ¶
func StringToUpperMap(src interface{}) (interface{}, error)
StringToUpperMap translates string interface into a string with upper case
func Translate ¶
func Translate(src map[string]interface{}, description map[string]interface{}) (map[string]interface{}, error)
Translate is the main function that converts source map[string]interface{} to destination map[string]interface{} using specified description. Usually source comes from JSON decoding.The following translations are applied:
- If translation is defined as "SrcName": "DstName", the field 'SrcName' changes its name to 'DstName' in the resulting map while preserving the value.
- If TranslationType in the description is CustomTranslation, the MapFunc is called on the source value, the result is written in the destination map using TargetName as a key.
- If TranslationType is MapTranslation, it means that the source value is a map that requires further translation which we apply using SubTranslation definition. The result is written using TargetName as a key.
- If TranslationType is MapArrayTranslation, the source is an array of objects (maps). In this case each element is translated using SubTranslation as the description and the resulting array of objects is written using TargetName as the key
- If TranslationType is ModifyTranslation, we pass the source and destination maps together with the field value to the ModFunc and it is up to it to put proper value in the destination map
- If TranslationType is InsertTranslation, we are inserting key that isn't in the source map. In this case we call the InsertFunc and it inserts value (or values) in the destination map.
Types ¶
type Description ¶
type Description struct { InsertFunc InsertFunc // Function to insert element Mandatory bool // The field must be present if true MapFunc MapFunc // Function that maps value to new value ModFunc ModFunc // Function for object modification SubTranslation map[string]interface{} // Sub-translation map for children TargetName string // Name of destination field Type TranslationType // Type of translation }
Description defines translation definition Translations are defined as either "name": "newName" or "name": Description A SubTranslation is just another embedded translation for a field.
type InsertFunc ¶
InsertFunc is used to insert a new element into the map. Parameters:
Source map Destination map Name of the destination element
Returns: a value that will be inserted in the map using TargetName.
type InternalError ¶
type InternalError struct {
Reason string
}
InternalError is a programming error - it should never happen
func NewInternalError ¶
func NewInternalError(reason string) *InternalError
NewInternalError returns an instance of an internal error with specified reason
func (*InternalError) Error ¶
func (e *InternalError) Error() string
type InvalidPropertyError ¶
InvalidPropertyError is an error indicating that a user-provided parameter is bad.
func NewInvalidProp ¶
func NewInvalidProp(name string, reason string) *InvalidPropertyError
NewInvalidProp returns an instance of InvalidPropertyError
func (*InvalidPropertyError) Error ¶
func (e *InvalidPropertyError) Error() string
type MapFunc ¶
type MapFunc func(interface{}) (interface{}, error)
MapFunc is a function that converts one interface to another. This is a generic function that maps one value to some other value. All translations are usually defined as MapFunc.
type MissingAttributeError ¶
type MissingAttributeError struct {
Name string
}
MissingAttributeError is caused by a map attribute that is mandatory but is missing
func NewMissingAttributeError ¶
func NewMissingAttributeError(name string) *MissingAttributeError
NewMissingAttributeError returns an instance of an error for a missing attribute
func (*MissingAttributeError) Error ¶
func (e *MissingAttributeError) Error() string
type ModFunc ¶
ModFunc takes a source map(before translation), the destination map (with some transations already applied) and a value and modifies the map. It returns the error, if any. Parameters:
Source map Destination map Value from the source map
type TranslationType ¶
type TranslationType int
TranslationType identifies type of element translation to perform. It is used as an enum.
const ( // CustomTranslation (default) means that a function should be provided // for a translation CustomTranslation TranslationType = iota // MapTranslation means that translation defines an embedded map MapTranslation // MapArrayTranslation means that the translation defines an array of // maps MapArrayTranslation // ModifyTranslation modifies the map based on the input value ModifyTranslation // InsertTranslation inserts a missing value to an existing map InsertTranslation )