-
Notifications
You must be signed in to change notification settings - Fork 1
/
settingsjson.go
209 lines (197 loc) · 5.32 KB
/
settingsjson.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
package gosudachi
import (
"encoding/json"
"fmt"
"io"
"path/filepath"
)
type SettingsJSON struct {
BaseConfig
path string
inputTextPlugin []json.RawMessage
oovProviderPlugin []json.RawMessage
pathRewritePlugin []json.RawMessage
editConnectionCostPlugin []json.RawMessage
}
func NewSettingsJSON() *SettingsJSON {
return &SettingsJSON{}
}
func (settings *SettingsJSON) GetBaseConfig() *BaseConfig {
return &settings.BaseConfig
}
func (settings *SettingsJSON) ParseSettingsJSON(defpath string, reader io.Reader) error {
internalBaseConfig := &struct {
Path *string
SystemDict *string
CharacterDefinitionFile *string
Utf16String *bool
UserDict *[]string
InputTextPlugin *[]json.RawMessage
OovProviderPlugin *[]json.RawMessage
PathRewritePlugin *[]json.RawMessage
EditConnectionCostPlugin *[]json.RawMessage
}{}
decoder := json.NewDecoder(reader)
err := decoder.Decode(internalBaseConfig)
if err != nil {
return err
}
if internalBaseConfig.Path == nil && settings.path != "" {
settings.path = defpath
} else if internalBaseConfig.Path != nil {
settings.path = *internalBaseConfig.Path
}
if internalBaseConfig.SystemDict != nil {
settings.SystemDict = settings.getPath(*internalBaseConfig.SystemDict)
}
if internalBaseConfig.CharacterDefinitionFile != nil {
settings.CharacterDefinitionFile = settings.getPath(*internalBaseConfig.CharacterDefinitionFile)
}
if internalBaseConfig.Utf16String != nil {
settings.Utf16String = *internalBaseConfig.Utf16String
}
if internalBaseConfig.UserDict != nil {
for _, ud := range *internalBaseConfig.UserDict {
settings.UserDict = append(settings.UserDict, settings.getPath(ud))
}
}
if internalBaseConfig.InputTextPlugin != nil {
settings.inputTextPlugin = *internalBaseConfig.InputTextPlugin
}
if internalBaseConfig.OovProviderPlugin != nil {
settings.oovProviderPlugin = *internalBaseConfig.OovProviderPlugin
}
if internalBaseConfig.PathRewritePlugin != nil {
settings.pathRewritePlugin = *internalBaseConfig.PathRewritePlugin
}
if internalBaseConfig.EditConnectionCostPlugin != nil {
settings.editConnectionCostPlugin = *internalBaseConfig.EditConnectionCostPlugin
}
return nil
}
func (settings *SettingsJSON) getPath(path string) string {
if path == "" || filepath.IsAbs(path) || settings.path == "" {
return path
}
return filepath.Join(settings.path, path)
}
func (settings *SettingsJSON) GetInputTextPluginArray(makeproc MakeInputTextPluginFunc) ([]InputTextPlugin, error) {
ret := []InputTextPlugin{}
pname := &struct {
Class *string
Name *string
}{}
for _, raw := range settings.inputTextPlugin {
err := json.Unmarshal(raw, pname)
if err != nil {
return ret, err
}
var name string
if pname.Class != nil {
name = *pname.Class
}
if pname.Name != nil {
name = *pname.Name
}
plugin := makeproc(name)
if plugin == nil {
return ret, fmt.Errorf("InputTextPlugin: %s is unknown", name)
}
err = json.Unmarshal(raw, plugin.GetConfigStruct())
if err != nil {
return ret, err
}
ret = append(ret, plugin)
}
return ret, nil
}
func (settings *SettingsJSON) GetOovProviderPluginArray(makeproc MakeOovProviderPluginFunc) ([]OovProviderPlugin, error) {
ret := []OovProviderPlugin{}
pname := &struct {
Class *string
Name *string
}{}
for _, raw := range settings.oovProviderPlugin {
err := json.Unmarshal(raw, pname)
if err != nil {
return ret, err
}
var name string
if pname.Class != nil {
name = *pname.Class
}
if pname.Name != nil {
name = *pname.Name
}
plugin := makeproc(name)
if plugin == nil {
return ret, fmt.Errorf("OovProviderPlugin: %s is unknown", name)
}
err = json.Unmarshal(raw, plugin.GetConfigStruct())
if err != nil {
return ret, err
}
ret = append(ret, plugin)
}
return ret, nil
}
func (settings *SettingsJSON) GetEditConnectionCostPluginArray(makeproc MakeEditConnectionCostPluginFunc) ([]EditConnectionCostPlugin, error) {
ret := []EditConnectionCostPlugin{}
pname := &struct {
Class *string
Name *string
}{}
for _, raw := range settings.editConnectionCostPlugin {
err := json.Unmarshal(raw, pname)
if err != nil {
return ret, err
}
var name string
if pname.Class != nil {
name = *pname.Class
}
if pname.Name != nil {
name = *pname.Name
}
plugin := makeproc(name)
if plugin == nil {
return ret, fmt.Errorf("EditConnectionCostPlugin: %s is unknown", name)
}
err = json.Unmarshal(raw, plugin.GetConfigStruct())
if err != nil {
return ret, err
}
ret = append(ret, plugin)
}
return ret, nil
}
func (settings *SettingsJSON) GetPathRewritePluginArray(makeproc MakePathRewritePluginFunc) ([]PathRewritePlugin, error) {
ret := []PathRewritePlugin{}
pname := &struct {
Class *string
Name *string
}{}
for _, raw := range settings.pathRewritePlugin {
err := json.Unmarshal(raw, pname)
if err != nil {
return ret, err
}
var name string
if pname.Class != nil {
name = *pname.Class
}
if pname.Name != nil {
name = *pname.Name
}
plugin := makeproc(name)
if plugin == nil {
return ret, fmt.Errorf("PathRewritePlugin: %s is unknown", name)
}
err = json.Unmarshal(raw, plugin.GetConfigStruct())
if err != nil {
return ret, err
}
ret = append(ret, plugin)
}
return ret, nil
}