/
io.go
182 lines (170 loc) · 4.88 KB
/
io.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
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package netparams
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
"cogentcore.org/core/core"
"cogentcore.org/core/gox/indent"
"cogentcore.org/core/iox"
"cogentcore.org/core/iox/jsonx"
"cogentcore.org/core/iox/tomlx"
"github.com/BurntSushi/toml"
"github.com/emer/emergent/v2/params"
)
// WriteGoPrelude writes the start of a go file in package main that starts a
// variable assignment to given variable -- for start of SaveGoCode methods.
func WriteGoPrelude(w io.Writer, varNm string) {
w.Write([]byte("// File generated by netparams.SaveGoCode\n\n"))
w.Write([]byte("package main\n\n"))
w.Write([]byte(`import "github.com/emer/emergent/v2/params"`))
w.Write([]byte(`import "github.com/emer/emergent/v2/netparams"`))
w.Write([]byte("\n\nvar " + varNm + " = "))
}
// OpenJSON opens params from a JSON-formatted file.
func (pr *Sets) OpenJSON(filename core.Filename) error {
*pr = make(Sets) // reset
b, err := ioutil.ReadFile(string(filename))
if err != nil {
log.Println(err)
return err
}
return json.Unmarshal(b, pr)
}
// SaveJSON saves params to a JSON-formatted file.
func (pr *Sets) SaveJSON(filename core.Filename) error {
return jsonx.Save(pr, string(filename))
}
// OpenTOML opens params from a TOML-formatted file.
func (pr *Sets) OpenTOML(filename core.Filename) error {
*pr = make(Sets) // reset
return tomlx.Open(pr, string(filename))
}
// SaveTOML saves params to a TOML-formatted file.
func (pr *Sets) SaveTOML(filename core.Filename) error {
// return tomlx.Save(pr, string(filename)) // pelletier/go-toml produces bad output on maps
return iox.Save(pr, string(filename), func(w io.Writer) iox.Encoder {
return toml.NewEncoder(w)
})
}
// WriteGoCode writes params to corresponding Go initializer code.
func (pr *Sets) WriteGoCode(w io.Writer, depth int) {
w.Write([]byte("netparams.Sets{\n"))
depth++
for nm, st := range *pr {
w.Write(indent.TabBytes(depth))
w.Write([]byte(`"` + nm + `": `))
st.WriteGoCode(w, depth)
}
depth--
w.Write(indent.TabBytes(depth))
w.Write([]byte("}\n"))
}
// StringGoCode returns Go initializer code as a byte string.
func (pr *Sets) StringGoCode() []byte {
var buf bytes.Buffer
pr.WriteGoCode(&buf, 0)
return buf.Bytes()
}
// SaveGoCode saves params to corresponding Go initializer code.
func (pr *Sets) SaveGoCode(filename core.Filename) error {
fp, err := os.Create(string(filename))
defer fp.Close()
if err != nil {
log.Println(err)
return err
}
params.WriteGoPrelude(fp, "SavedParamsSets")
pr.WriteGoCode(fp, 0)
return nil
}
/*
var SetsProps = tree.Props{
"ToolBar": tree.PropSlice{
{"Save", tree.PropSlice{
{"SaveTOML", tree.Props{
"label": "Save As TOML...",
"desc": "save to TOML formatted file",
"icon": "file-save",
"Args": tree.PropSlice{
{"File Name", tree.Props{
"ext": ".toml",
}},
},
}},
{"SaveJSON", tree.Props{
"label": "Save As JSON...",
"desc": "save to JSON formatted file",
"icon": "file-save",
"Args": tree.PropSlice{
{"File Name", tree.Props{
"ext": ".json",
}},
},
}},
{"SaveGoCode", tree.Props{
"label": "Save Code As...",
"desc": "save to Go-formatted initializer code in file",
"icon": "go",
"Args": tree.PropSlice{
{"File Name", tree.Props{
"ext": ".go",
}},
},
}},
}},
{"Open", tree.PropSlice{
{"OpenTOML", tree.Props{
"label": "Open...",
"desc": "open from TOML formatted file",
"icon": "file-open",
"Args": tree.PropSlice{
{"File Name", tree.Props{
"ext": ".toml",
}},
},
}},
{"OpenJSON", tree.Props{
"label": "Open...",
"desc": "open from JSON formatted file",
"icon": "file-open",
"Args": tree.PropSlice{
{"File Name", tree.Props{
"ext": ".json",
}},
},
}},
}},
{"StringGoCode", tree.Props{
"label": "Show Code",
"desc": "shows the Go-formatted initializer code, can be copy / pasted into program",
"icon": "go",
"show-return": true,
}},
{"sep-diffs", tree.BlankProp{}},
{"DiffsAll", tree.Props{
"desc": "between all sets, reports where the same param path is being set to different values",
"icon": "search",
"show-return": true,
}},
{"DiffsFirst", tree.Props{
"desc": "between first set (e.g., the Base set) and rest of sets, reports where the same param path is being set to different values",
"icon": "search",
"show-return": true,
}},
{"DiffsWithin", tree.Props{
"desc": "reports all the cases where the same param path is being set to different values within different sheets in given set",
"icon": "search",
"show-return": true,
"Args": tree.PropSlice{
{"Set Name", tree.Props{}},
},
}},
},
}
*/