Documentation
¶
Overview ¶
Package namemap addresses the situation where a software has to mediate between different data sources where each source (domain) has a different ID (name) for one thing. The NameMap provides an efficient tool to map such names from one domain to another.
Besides the types and methods for name mapping this package provides a simple file format to define such mappings that can easily be parsed for application. As it is often desirable one of the domains can be defined as the standard domain the file format allows for easily define one domain as the standard domain. E.g.
[\input output l10n:EN l10n:DE] ( note rem remark Bemerkung) ( warn warnig warning Warnung)
defines a map that has an 'input', 'output' domain an additionaly has some natural language domains for user interfaces (e.g. GUI). The 'input' domain is selected to be the standard domain.
Index ¶
- Constants
- func IgnDom(mapped string, ignore int) string
- type From
- func (fnm *From) Base() *NameMap
- func (nm *From) Check(mapHint string, domainHint string) error
- func (nm *From) FromIdx() int
- func (fnm *From) Map(term string, toDomains ...int) (mapped string, domain int)
- func (fnm *From) MapNm(term string, toNames ...string) (string, int)
- func (fnm From) To(appendStd bool, toDomains ...string) FromTo
- func (nm From) Verify(mapHint string, domainHint string) From
- type FromTo
- type NameMap
- func (nm *NameMap) Def(domain2name map[string]string)
- func (nm *NameMap) DomainIdx(domain string) int
- func (nm *NameMap) DomainName(idx int) string
- func (nm *NameMap) ForEach(domain int, apply func(value string))
- func (nm NameMap) From(fromDomain string, fallback bool) From
- func (nm NameMap) FromStd() From
- func (nm *NameMap) Load(rd io.Reader) (err error)
- func (nm *NameMap) LoadFile(name string) error
- func (nm *NameMap) Map(fromDomain int, term string, toDomains ...int) (mapped string, domain int)
- func (nm *NameMap) MapNm(fromNm string, term string, toNames ...string) (string, int)
- func (nm *NameMap) Save(wr io.Writer, undef string) (err error)
- func (nm *NameMap) Set(keyDom int, keyName string, setDom int, setName string)
- func (nm *NameMap) SetStdDomain(domain string)
- func (nm NameMap) To(appendStd bool, toDomains ...string) To
- type To
- func (tnm *To) Base() *NameMap
- func (nm *To) Check(mapHint string, domainHint string) error
- func (tnm To) From(fromDomain string, fallback bool) FromTo
- func (tnm To) FromStd() FromTo
- func (tnm *To) Map(fromDomain int, term string) (mapped string, domain int)
- func (tnm *To) MapNm(fromName string, term string) (string, int)
- func (nm *To) ToIdxs() []int
- func (nm To) Verify(mapHint string, domainHint string) To
- type UnknownDomain
Examples ¶
Constants ¶
const ( Major = 0 Minor = 2 Bugfix = 0 Quality = "b" BuildNo = 2 )
Variables ¶
This section is empty.
Functions ¶
func IgnDom ¶
IgnDom is a utility function that ignores the domain of a mapped name. This can be used for all .Map and .MapNm methods if the returned domain is irrelevant.
Example ¶
nmDef := strings.NewReader( `[\input output l10n:EN l10n:DE] (note rem remark \undef) (warn warnig warning Warnung)`) nm := NameMap{} if err := nm.Load(nmDef); err != nil { panic(err) } fmt.Println(IgnDom(nm.MapNm("input", "note", "l10n:DE", "l10n:EN")))
Output: remark
Types ¶
type From ¶
type From struct {
// contains filtered or unexported fields
}
type FromTo ¶
type FromTo struct {
// contains filtered or unexported fields
}
type NameMap ¶
type NameMap struct { StdDomain int // contains filtered or unexported fields }
NameMap is the basic data structure to map a name from one input domain to another. Use Load or MustLoad to read a NameMap from a definition file.
Example ¶
nm := NewNameMap("key", "local") nm.Set(0, "akey", 1, "aloc") err := nm.Save(os.Stdout, "null") if err != nil { fmt.Println(err) }
Output: [key local] (akey aloc)
func MustLoad ¶
MustLoad loads the NameMap definition from file 'filename' and panics if an error occurs.
func (*NameMap) Def ¶
Example ¶
TODO cannot rely on column order => might fail occasionally => rewrite as test
nm := NewNameMap() nm.Def(map[string]string{ "input": "note", "output": "rem", "l10n:EN": "remark", }) nm.Def(map[string]string{ "input": "warn", "output": "warnig", "l10n:EN": "warning", "l10n:DE": "Warnung", }) nm.SetStdDomain("input") nm.Save(os.Stdout, "null")
Output: [\input output l10n:EN l10n:DE] (note rem remark \null) (warn warnig warning Warnung)
func (*NameMap) DomainIdx ¶
DomainIdx returns the index of a domain given the domains name. Using the index for name mapping avoids string look ups and should be used in performance critical applications. If no domain with the given domain name exists, -1 is returned.
func (*NameMap) DomainName ¶
DomainName returns the name of the domain with index idx. If the index is out of range, the empty string is returned.
func (NameMap) From ¶
From creates a "from-map" that maps from the chosen fromDomain to one of the other domains. If nm has not fromDomain and fallback is true then the resulting "from-map" maps from nm's standard domain.
func (*NameMap) Load ¶
Load load the definition of the NameMap from 'rd'. Definitions are texts formatted as an XSX table (see https://godoc.org/github.com/fractalqb/xsx/table).
Example ¶
nmDef := strings.NewReader( `[\input output l10n:EN l10n:DE] (note rem remark \undef) (warn warnig warning Warnung)`) nm := NameMap{} if err := nm.Load(nmDef); err != nil { panic(err) } mapped, inDom := nm.MapNm("input", "warn", "l10n:DE", "l10n:EN") fmt.Printf("input name 'warn' maps to '%s' in domain %s\n", mapped, nm.DomainName(inDom)) mapped, inDom = nm.MapNm("output", "rem", "l10n:DE", "l10n:EN") fmt.Printf("output name 'rem' maps to '%s' in domain %s\n", mapped, nm.DomainName(inDom))
Output: input name 'warn' maps to 'Warnung' in domain l10n:DE output name 'rem' maps to 'remark' in domain l10n:EN
func (*NameMap) Map ¶
Map maps 'term' from 'fromDomain' to the corresponding name in the first matching 'toDomains' element. If no matching 'toDomains' element is found the 'term' itself is returned as 'mapped' and 'domain' is -1. Otherwise 'domain' contains the domain index of the matching target domain.
func (*NameMap) MapNm ¶
MapNm determines all domain indices for 'fromNm' and 'toNames' and calls NameMap.Map().
func (*NameMap) Set ¶
type To ¶
type To struct {
// contains filtered or unexported fields
}