properties

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2019 License: GPL-2.0 Imports: 10 Imported by: 0

README

GoDoc

Package properties is a library for handling maps of hierarchical properties.

This library is mainly used in the Arduino platform software to handle configurations made of key/value pairs stored in files with an INI like syntax.

This map also keeps the insertion order when ranging through the Keys() method.

For more information read the docs here.

Documentation

Overview

Package properties is a library for handling maps of hierarchical properties. This library is mainly used in the Arduino platform software to handle configurations made of key/value pairs stored in files with an INI like syntax, for example:

...
uno.name=Arduino/Genuino Uno
uno.upload.tool=avrdude
uno.upload.protocol=arduino
uno.upload.maximum_size=32256
uno.upload.maximum_data_size=2048
uno.upload.speed=115200
uno.build.mcu=atmega328p
uno.build.f_cpu=16000000L
uno.build.board=AVR_UNO
uno.build.core=arduino
uno.build.variant=standard
diecimila.name=Arduino Duemilanove or Diecimila
diecimila.upload.tool=avrdude
diecimila.upload.protocol=arduino
diecimila.build.f_cpu=16000000L
diecimila.build.board=AVR_DUEMILANOVE
diecimila.build.core=arduino
diecimila.build.variant=standard
...

This library has methods to parse this kind of files into a Map object.

The Map internally keeps the insertion order so it can be retrieved later when cycling through the key sets.

The Map object has many helper methods to accomplish some common operation on this kind of data like cloning, merging, comparing and also extracting a submap or generating a map-of-submaps from the first "level" of the hierarchy.

On the Arduino platform the properties are used to populate command line recipes so there are some methods to help this task like SplitQuotedString or ExpandPropsInString.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteUnexpandedPropsFromString

func DeleteUnexpandedPropsFromString(str string) string

DeleteUnexpandedPropsFromString removes all the brace markers "{xxx}" that are not expanded into a value using the Map.ExpandPropsInString() method.

func GetOSSuffix

func GetOSSuffix() string

GetOSSuffix returns the os name used to filter os-specific properties in Load* functions

func MergeMapsOfProperties

func MergeMapsOfProperties(target map[string]*Map, sources ...map[string]*Map) map[string]*Map

MergeMapsOfProperties merge the map-of-Maps (obtained from the method FirstLevelOf()) into the target map-of-Maps.

func SplitQuotedString

func SplitQuotedString(src string, quoteChars string, acceptEmptyArguments bool) ([]string, error)

SplitQuotedString splits a string by spaces and at the same time allows to use spaces in a single element of the split using quote characters.

For example the call:

SplitQuotedString(`This 'is an' "Hello World!" example`, `'"`, false)

returns the following array:

[]string{"This", "is an", "Hello World!", "example"}

Types

type Map

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

Map is a container of properties

func Load

func Load(filepath string) (*Map, error)

Load reads a properties file and makes a Map out of it.

func LoadFromPath

func LoadFromPath(path *paths.Path) (*Map, error)

LoadFromPath reads a properties file and makes a Map out of it.

func LoadFromSlice

func LoadFromSlice(lines []string) (*Map, error)

LoadFromSlice reads a properties file from an array of string and makes a Map out of it

func NewFromHashmap

func NewFromHashmap(orig map[string]string) *Map

NewFromHashmap creates a new Map from the given map[string]string. Insertion order is not preserved.

func NewMap

func NewMap() *Map

NewMap returns a new Map

func SafeLoad

func SafeLoad(filepath string) (*Map, error)

SafeLoad is like Load, except that it returns an empty Map if the specified file doesn't exists

func SafeLoadFromPath

func SafeLoadFromPath(path *paths.Path) (*Map, error)

SafeLoadFromPath is like LoadFromPath, except that it returns an empty Map if the specified file doesn't exists

func (*Map) AsMap

func (m *Map) AsMap() map[string]string

AsMap return the underlying map[string]string. This is useful if you need to for ... range but without the requirement of the ordered elements.

func (*Map) Clone

func (m *Map) Clone() *Map

Clone makes a copy of the Map

func (*Map) ContainsKey

func (m *Map) ContainsKey(key string) bool

ContainsKey returns true

func (*Map) Dump

func (m *Map) Dump() string

Dump returns a representation of the map in golang source format

func (*Map) Equals

func (m *Map) Equals(other *Map) bool

Equals returns true if the current Map contains the same key/value pairs of the Map passed as argument, the order of insertion does not matter.

func (*Map) EqualsWithOrder

func (m *Map) EqualsWithOrder(other *Map) bool

EqualsWithOrder returns true if the current Map contains the same key/value pairs of the Map passed as argument with the same order of insertion.

func (*Map) ExpandPropsInString

func (m *Map) ExpandPropsInString(str string) string

ExpandPropsInString use the Map to replace values into a format string. The format string should contains markers between braces, for example:

"The selected upload protocol is {upload.protocol}."

Each marker is replaced by the corresponding value of the Map. The values in the Map may contains other markers, they are evaluated recursively up to 10 times.

func (*Map) FirstLevelKeys

func (m *Map) FirstLevelKeys() []string

FirstLevelKeys returns the keys in the first level of the hierarchy of the current Map. For example the following Map:

properties.Map{
  "uno.name": "Arduino/Genuino Uno",
  "uno.upload.tool": "avrdude",
  "uno.upload.protocol": "arduino",
  "uno.upload.maximum_size": "32256",
  "diecimila.name": "Arduino Duemilanove or Diecimila",
  "diecimila.upload.tool": "avrdude",
  "diecimila.upload.protocol": "arduino",
  "diecimila.bootloader.tool": "avrdude",
  "diecimila.bootloader.low_fuses": "0xFF",
}

will produce the following result:

[]string{
  "uno",
  "diecimila",
}

the order of the original map is preserved

func (*Map) FirstLevelOf

func (m *Map) FirstLevelOf() map[string]*Map

FirstLevelOf generates a map-of-Maps using the first level of the hierarchy of the current Map. For example the following Map:

properties.Map{
  "uno.name": "Arduino/Genuino Uno",
  "uno.upload.tool": "avrdude",
  "uno.upload.protocol": "arduino",
  "uno.upload.maximum_size": "32256",
  "diecimila.name": "Arduino Duemilanove or Diecimila",
  "diecimila.upload.tool": "avrdude",
  "diecimila.upload.protocol": "arduino",
  "diecimila.bootloader.tool": "avrdude",
  "diecimila.bootloader.low_fuses": "0xFF",
}

is transformed into the following map-of-Maps:

map[string]Map{
  "uno" : properties.Map{
    "name": "Arduino/Genuino Uno",
    "upload.tool": "avrdude",
    "upload.protocol": "arduino",
    "upload.maximum_size": "32256",
  },
  "diecimila" : properties.Map{
    "name=Arduino Duemilanove or Diecimila
    "upload.tool": "avrdude",
    "upload.protocol": "arduino",
    "bootloader.tool": "avrdude",
    "bootloader.low_fuses": "0xFF",
  }
}

func (*Map) Get

func (m *Map) Get(key string) string

Get retrieve the value corresponding to key

func (*Map) GetBoolean

func (m *Map) GetBoolean(key string) bool

GetBoolean returns true if the map contains the specified key and the value equals to the string "true", in any other case returns false.

func (*Map) GetOk

func (m *Map) GetOk(key string) (string, bool)

GetOk retrieve the value corresponding to key and return a true/false indicator to check if the key is present in the map (true if the key is present)

func (*Map) GetPath

func (m *Map) GetPath(key string) *paths.Path

GetPath returns a paths.Path object using the map value as path. The function returns nil if the key is not present.

func (*Map) Keys

func (m *Map) Keys() []string

Keys returns an array of the keys contained in the Map

func (*Map) MarshalJSON

func (m *Map) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*Map) Merge

func (m *Map) Merge(sources ...*Map) *Map

Merge merges other Maps into this one. Each key/value of the merged Maps replaces the key/value present in the original Map.

func (*Map) Remove

func (m *Map) Remove(key string)

Remove removes the key from the map

func (*Map) Set

func (m *Map) Set(key, value string)

Set inserts or replaces an existing key-value pair in the map

func (*Map) SetBoolean

func (m *Map) SetBoolean(key string, value bool)

SetBoolean sets the specified key to the string "true" or "false" if the value is respectively true or false.

func (*Map) SetPath

func (m *Map) SetPath(key string, value *paths.Path)

SetPath saves the paths.Path object in the map using the path as value of the map

func (*Map) Size

func (m *Map) Size() int

Size return the number of elements in the map

func (*Map) SubTree

func (m *Map) SubTree(rootKey string) *Map

SubTree extracts a sub Map from an existing map using the first level of the keys hierarchy as selector. For example the following Map:

properties.Map{
  "uno.name": "Arduino/Genuino Uno",
  "uno.upload.tool": "avrdude",
  "uno.upload.protocol": "arduino",
  "uno.upload.maximum_size": "32256",
  "diecimila.name": "Arduino Duemilanove or Diecimila",
  "diecimila.upload.tool": "avrdude",
  "diecimila.upload.protocol": "arduino",
  "diecimila.bootloader.tool": "avrdude",
  "diecimila.bootloader.low_fuses": "0xFF",
}

after calling SubTree("uno") will be transformed in:

properties.Map{
  "name": "Arduino/Genuino Uno",
  "upload.tool": "avrdude",
  "upload.protocol": "arduino",
  "upload.maximum_size": "32256",
},

func (*Map) UnmarshalJSON

func (m *Map) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler interface

func (*Map) Values

func (m *Map) Values() []string

Values returns an array of the values contained in the Map. Duplicated values are repeated in the list accordingly.

Jump to

Keyboard shortcuts

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