namemap

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2020 License: MIT Imports: 8 Imported by: 0

README

namemap

Test Coverage Go Report Card GoDoc

import "git.fractalqb.de/fractalqb/namemap"


Map between unique names from dirfferent sources

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

Examples

Constants

View Source
const (
	Major   = 0
	Minor   = 2
	Bugfix  = 0
	Quality = "b"
	BuildNo = 2
)

Variables

This section is empty.

Functions

func IgnDom

func IgnDom(mapped string, ignore int) string

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
}

func (*From) Base

func (fnm *From) Base() *NameMap

func (*From) Check

func (nm *From) Check(mapHint string, domainHint string) error

func (*From) FromIdx

func (nm *From) FromIdx() int

func (*From) Map

func (fnm *From) Map(term string, toDomains ...int) (mapped string, domain int)

func (*From) MapNm

func (fnm *From) MapNm(term string, toNames ...string) (string, int)

func (From) To

func (fnm From) To(appendStd bool, toDomains ...string) FromTo

func (From) Verify

func (nm From) Verify(mapHint string, domainHint string) From

type FromTo

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

func (*FromTo) Base

func (nm *FromTo) Base() *NameMap

func (*FromTo) Check

func (nm *FromTo) Check(mapHint string, domainHint string) error

func (*FromTo) FromIdx

func (nm *FromTo) FromIdx() int

func (*FromTo) Map

func (nm *FromTo) Map(term string) (mapped string, domain int)

func (*FromTo) ToIdxs

func (nm *FromTo) ToIdxs() []int

func (FromTo) Verify

func (nm FromTo) Verify(mapHint string, domainHint string) FromTo

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

func MustLoad(filename string) *NameMap

MustLoad loads the NameMap definition from file 'filename' and panics if an error occurs.

func NewNameMap

func NewNameMap(domains ...string) *NameMap

func (*NameMap) Def

func (nm *NameMap) Def(domain2name map[string]string)
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

func (nm *NameMap) DomainIdx(domain string) int

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

func (nm *NameMap) DomainName(idx int) string

DomainName returns the name of the domain with index idx. If the index is out of range, the empty string is returned.

func (*NameMap) ForEach

func (nm *NameMap) ForEach(domain int, apply func(value string))

func (NameMap) From

func (nm NameMap) From(fromDomain string, fallback bool) 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) FromStd

func (nm NameMap) FromStd() From

func (*NameMap) Load

func (nm *NameMap) Load(rd io.Reader) (err error)

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) LoadFile

func (nm *NameMap) LoadFile(name string) error

func (*NameMap) Map

func (nm *NameMap) Map(fromDomain int, term string, toDomains ...int) (mapped string, domain int)

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

func (nm *NameMap) MapNm(fromNm string, term string, toNames ...string) (string, int)

MapNm determines all domain indices for 'fromNm' and 'toNames' and calls NameMap.Map().

func (*NameMap) Save

func (nm *NameMap) Save(wr io.Writer, undef string) (err error)

func (*NameMap) Set

func (nm *NameMap) Set(keyDom int, keyName string, setDom int, setName string)

func (*NameMap) SetStdDomain

func (nm *NameMap) SetStdDomain(domain string)

func (NameMap) To

func (nm NameMap) To(appendStd bool, toDomains ...string) To

type To

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

func (*To) Base

func (tnm *To) Base() *NameMap

func (*To) Check

func (nm *To) Check(mapHint string, domainHint string) error

func (To) From

func (tnm To) From(fromDomain string, fallback bool) FromTo

func (To) FromStd

func (tnm To) FromStd() FromTo

func (*To) Map

func (tnm *To) Map(fromDomain int, term string) (mapped string, domain int)

func (*To) MapNm

func (tnm *To) MapNm(fromName string, term string) (string, int)

func (*To) ToIdxs

func (nm *To) ToIdxs() []int

func (To) Verify

func (nm To) Verify(mapHint string, domainHint string) To

type UnknownDomain

type UnknownDomain struct {
	MapHint    string
	DomainHint string
}

func (*UnknownDomain) Error

func (err *UnknownDomain) Error() string

Jump to

Keyboard shortcuts

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