2021-02-08 17:40:36 +00:00
|
|
|
package csvlocode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-28 14:01:31 +00:00
|
|
|
"io/fs"
|
2021-02-10 18:06:00 +00:00
|
|
|
"sync"
|
2021-02-08 17:40:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Prm groups the required parameters of the Table's constructor.
|
|
|
|
//
|
|
|
|
// All values must comply with the requirements imposed on them.
|
|
|
|
// Passing incorrect parameter values will result in constructor
|
|
|
|
// failure (error or panic depending on the implementation).
|
|
|
|
type Prm struct {
|
|
|
|
// Path to UN/LOCODE csv table.
|
|
|
|
//
|
|
|
|
// Must not be empty.
|
|
|
|
Path string
|
|
|
|
|
|
|
|
// Path to csv table of UN/LOCODE Subdivisions.
|
|
|
|
//
|
|
|
|
// Must not be empty.
|
|
|
|
SubDivPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Table is a descriptor of the UN/LOCODE table in csv format.
|
|
|
|
//
|
|
|
|
// For correct operation, Table must be created
|
|
|
|
// using the constructor (New) based on the required parameters
|
|
|
|
// and optional components. After successful creation,
|
|
|
|
// The Table is immediately ready to work through API.
|
|
|
|
type Table struct {
|
|
|
|
paths []string
|
|
|
|
|
2021-06-28 14:01:31 +00:00
|
|
|
mode fs.FileMode
|
2021-02-08 17:40:36 +00:00
|
|
|
|
|
|
|
subDivPath string
|
2021-02-10 18:06:00 +00:00
|
|
|
|
|
|
|
subDivOnce sync.Once
|
|
|
|
|
|
|
|
mSubDiv map[subDivKey]subDivRecord
|
2021-02-08 17:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const invalidPrmValFmt = "invalid parameter %s (%T):%v"
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func panicOnPrmValue(n string, v any) {
|
2021-02-08 17:40:36 +00:00
|
|
|
panic(fmt.Sprintf(invalidPrmValFmt, n, v, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of the Table.
|
|
|
|
//
|
|
|
|
// Panics if at least one value of the parameters is invalid.
|
|
|
|
//
|
|
|
|
// The created Table does not require additional
|
|
|
|
// initialization and is completely ready for work.
|
|
|
|
func New(prm Prm, opts ...Option) *Table {
|
|
|
|
switch {
|
|
|
|
case prm.Path == "":
|
|
|
|
panicOnPrmValue("Path", prm.Path)
|
|
|
|
case prm.SubDivPath == "":
|
|
|
|
panicOnPrmValue("SubDivPath", prm.SubDivPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
o := defaultOpts()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Table{
|
|
|
|
paths: append(o.extraPaths, prm.Path),
|
|
|
|
mode: o.mode,
|
|
|
|
subDivPath: prm.SubDivPath,
|
|
|
|
}
|
|
|
|
}
|