forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.go
88 lines (74 loc) · 2.36 KB
/
catalog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package ion
import (
"bytes"
"fmt"
"io"
"strings"
)
// A Catalog provides access to shared symbol tables.
type Catalog interface {
FindExact(name string, version int) SharedSymbolTable
FindLatest(name string) SharedSymbolTable
}
// A basicCatalog wraps an in-memory collection of shared symbol tables.
type basicCatalog struct {
ssts map[string]SharedSymbolTable
latest map[string]SharedSymbolTable
}
// NewCatalog creates a new basic catalog containing the given symbol tables.
func NewCatalog(ssts ...SharedSymbolTable) Catalog {
cat := &basicCatalog{
ssts: make(map[string]SharedSymbolTable),
latest: make(map[string]SharedSymbolTable),
}
for _, sst := range ssts {
cat.add(sst)
}
return cat
}
// Add adds a shared symbol table to the catalog.
func (c *basicCatalog) add(sst SharedSymbolTable) {
key := fmt.Sprintf("%v/%v", sst.Name(), sst.Version())
c.ssts[key] = sst
cur, ok := c.latest[sst.Name()]
if !ok || sst.Version() > cur.Version() {
c.latest[sst.Name()] = sst
}
}
// FindExact attempts to find a shared symbol table with the given name and version.
func (c *basicCatalog) FindExact(name string, version int) SharedSymbolTable {
key := fmt.Sprintf("%v/%v", name, version)
return c.ssts[key]
}
// FindLatest finds the shared symbol table with the given name and largest version.
func (c *basicCatalog) FindLatest(name string) SharedSymbolTable {
return c.latest[name]
}
// A System is a reader factory wrapping a catalog.
type System struct {
Catalog Catalog
}
// NewReader creates a new reader using this system's catalog.
func (s System) NewReader(in io.Reader) Reader {
return NewReaderCat(in, s.Catalog)
}
// NewReaderStr creates a new reader using this system's catalog.
func (s System) NewReaderStr(in string) Reader {
return NewReaderCat(strings.NewReader(in), s.Catalog)
}
// NewReaderBytes creates a new reader using this system's catalog.
func (s System) NewReaderBytes(in []byte) Reader {
return NewReaderCat(bytes.NewReader(in), s.Catalog)
}
// Unmarshal unmarshals Ion data using this system's catalog.
func (s System) Unmarshal(data []byte, v interface{}) error {
r := s.NewReaderBytes(data)
d := NewDecoder(r)
return d.DecodeTo(v)
}
// UnmarshalStr unmarshals Ion data using this system's catalog.
func (s System) UnmarshalStr(data string, v interface{}) error {
r := s.NewReaderStr(data)
d := NewDecoder(r)
return d.DecodeTo(v)
}