-
Notifications
You must be signed in to change notification settings - Fork 0
/
desc-map.go
210 lines (174 loc) · 6.03 KB
/
desc-map.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package mrnesbits
// desc-map.go holds structs, methods, and data structures used to define and then
// access mapping of a mrnesbits model's computational pattern function instances to
// CPUs in the underlaying mrnes computer and network model
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"os"
"path"
)
// A CompPatternMap describes how funcs in an instantiated [CompPattern]
// are mapped to hosts
type CompPatternMap struct {
// PatternName identifies the name of the pattern instantiation being mapped
PatternName string `json:"patternname" yaml:"patternname"`
// mapping of func labels to hosts. Key is Label attribute of Func
FuncMap map[string]string `json:"funcmap" yaml:"funcmap"`
}
// CreateCompPatternMap is a constructor.
func CreateCompPatternMap(ptnName string) *CompPatternMap {
cpm := new(CompPatternMap)
cpm.PatternName = ptnName
cpm.FuncMap = make(map[string]string)
return cpm
}
// AddMapping inserts into the CompPatternMap a binding of Func label to a host. Optionally,
// an error might be returned if a binding of that Func has already been made, and is different.
func (cpm *CompPatternMap) AddMapping(funcLabel string, hostname string, overwrite bool) error {
// only check for overwrite if asked to
if !overwrite {
host, present := cpm.FuncMap[funcLabel]
if present && host != hostname {
return fmt.Errorf("attempt to overwrite mapping of func label %s in %s",
funcLabel, cpm.PatternName)
}
}
// store the mapping
cpm.FuncMap[funcLabel] = hostname
return nil
}
// WriteToFile stores the CompPatternMap struct to the file whose name is given.
// Serialization to json or to yaml is selected based on the extension of this name.
func (cpm *CompPatternMap) WriteToFile(filename string) error {
pathExt := path.Ext(filename)
var bytes []byte
var merr error = nil
if pathExt == ".yaml" || pathExt == ".YAML" || pathExt == ".yml" {
bytes, merr = yaml.Marshal(*cpm)
} else if pathExt == ".json" || pathExt == ".JSON" {
bytes, merr = json.MarshalIndent(*cpm, "", "\t")
}
if merr != nil {
panic(merr)
}
f, cerr := os.Create(filename)
if cerr != nil {
panic(cerr)
}
_, werr := f.WriteString(string(bytes[:]))
if werr != nil {
panic(werr)
}
f.Close()
return werr
}
// ReadCompPatternMap deserializes a byte slice holding a representation of an CompPatternMap struct.
// If the input argument of dict (those bytes) is empty, the file whose name is given is read
// to acquire them. A deserialized representation is returned, or an error if one is generated
// from a file read or the deserialization.
func ReadCompPatternMap(filename string, useYAML bool, dict []byte) (*CompPatternMap, error) {
var err error
// if the dict slice of bytes is empty we get them from the file whose name is an argument
if len(dict) == 0 {
dict, err = os.ReadFile(filename)
if err != nil {
return nil, err
}
}
example := CompPatternMap{}
if useYAML {
err = yaml.Unmarshal(dict, &example)
} else {
err = json.Unmarshal(dict, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// A CompPatternMapDict holds copies of CompPatternMap structs in a map that is
// indexed by the PatternName of resident CompPatternMaps
type CompPatternMapDict struct {
DictName string `json:"dictname" yaml:"dictname"`
Map map[string]CompPatternMap `json:"map" yaml:"map"`
}
// CreateCompPatternMapDict is a constructor.
// Saves the dictionary name and initializes the map of CompPatternMaps to-be-stored.
func CreateCompPatternMapDict(name string) *CompPatternMapDict {
cpmd := new(CompPatternMapDict)
cpmd.DictName = name
cpmd.Map = make(map[string]CompPatternMap)
return cpmd
}
// AddCompPatternMap includes in the dictionary a CompPatternMap that is provided as input.
// Optionally an error may be returned if an entry for the associated CompPattern exists already.
func (cpmd *CompPatternMapDict) AddCompPatternMap(cpm *CompPatternMap, overwrite bool) error {
if !overwrite {
_, present := cpmd.Map[cpm.PatternName]
if present {
return fmt.Errorf("attempt to overwrite mapping of %s to comp pattern map dictionary", cpm.PatternName)
}
}
cpmd.Map[cpm.PatternName] = *cpm
return nil
}
// RecoverCompPatternMap returns a CompPatternMap associated with the CompPattern named in the input parameters.
// It returns also a flag denoting whether the identified CompPattern has an entry in the dictionary.
func (cpmd *CompPatternMapDict) RecoverCompPatternMap(pattern string) (*CompPatternMap, bool) {
cpm, present := cpmd.Map[pattern]
if !present {
return nil, false
}
cpm.PatternName = pattern
return &cpm, true
}
// ReadCompPatternMapDict deserializes a byte slice holding a representation of an CompPatternMapDict struct.
// If the input argument of dict (those bytes) is empty, the file whose name is given is read
// to acquire them. A deserialized representation is returned, or an error if one is generated
// from a file read or the deserialization.
func ReadCompPatternMapDict(filename string, useYAML bool, dict []byte) (*CompPatternMapDict, error) {
var err error
if len(dict) == 0 {
dict, err = os.ReadFile(filename)
if err != nil {
return nil, err
}
}
example := CompPatternMapDict{}
if useYAML {
err = yaml.Unmarshal(dict, &example)
} else {
err = json.Unmarshal(dict, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// WriteToFile stores the CompPatternMapDict struct to the file whose name is given.
// Serialization to json or to yaml is selected based on the extension of this name.
func (cpmd *CompPatternMapDict) WriteToFile(filename string) error {
pathExt := path.Ext(filename)
var bytes []byte
var merr error = nil
if pathExt == ".yaml" || pathExt == ".YAML" || pathExt == ".yml" {
bytes, merr = yaml.Marshal(*cpmd)
} else if pathExt == ".json" || pathExt == ".JSON" {
bytes, merr = json.MarshalIndent(*cpmd, "", "\t")
}
if merr != nil {
panic(merr)
}
f, cerr := os.Create(filename)
if cerr != nil {
panic(cerr)
}
_, werr := f.WriteString(string(bytes[:]))
if werr != nil {
panic(werr)
}
f.Close()
return werr
}