-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_and_value.go
59 lines (51 loc) · 1.12 KB
/
type_and_value.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
package config_tv
import (
"encoding/json"
"github.com/pkg/errors"
"os"
)
const (
TypePath = "path"
TypeEmbed = "embed"
)
type TypePluginMap map[string]TypeAndValuePlugin
func (x TypePluginMap) AddPlugin(pluginName string, plugin TypeAndValuePlugin) {
x[pluginName] = plugin
}
type TypeAndValue struct {
Type string
Value string
}
func (x TypeAndValue) String() string {
rawData, _ := json.Marshal(x)
return string(rawData)
}
func (x TypeAndValue) ReadRawDataNoPlugin() []byte {
switch x.Type {
case TypePath:
rawData, err := os.ReadFile(x.Value)
if err != nil {
panic(errors.Wrapf(err, "err read from path:%s", x.Value))
}
return rawData
case TypeEmbed:
return []byte(x.Value)
default:
panic(errors.Errorf("unsupported type:%s", x.Type))
}
}
func (x TypeAndValue) ReadRawData(typePluginMap TypePluginMap) []byte {
switch x.Type {
case TypePath:
return x.ReadRawDataNoPlugin()
case TypeEmbed:
return x.ReadRawDataNoPlugin()
default:
plugin, ok := typePluginMap[x.Type]
if !ok {
panic(errors.Errorf("unsupported type:%s", x.Type))
}
var rawData = plugin.ReadRawData(x)
return rawData
}
}