-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
133 lines (109 loc) · 2.79 KB
/
data.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
/*
* Bulldozer Framework
* Copyright (C) DesertBit
*/
package plugin
import (
"encoding/gob"
"fmt"
"github.com/desertbit/bulldozer/template"
)
const (
contextStoreKeyData = "budPluginData"
)
func init() {
// Register the custom type.
gob.Register(&storeData{})
}
//##########################//
//### Plugin data struct ###//
//##########################//
type storeData struct {
ID string
PluginType string
Section string
Args Args
AdditionalStyleClasses []string
}
type Data struct {
data *storeData
value interface{}
}
func newData(id string, pluginType string) *Data {
return &Data{
data: &storeData{
ID: id,
PluginType: pluginType,
},
}
}
// Section returns the plugin template section.
func (d *Data) Section() interface{} {
return d.data.Section
}
// Args returns the plugin template arguments.
func (d *Data) Args() Args {
return d.data.Args
}
// Value returns the interface value which was returned by the Prepare method.
func (d *Data) Value() interface{} {
return d.value
}
func (d *Data) prepareValue(vars ...Plugin) error {
// Obtain the plugin interface.
var plugin Plugin
if len(vars) > 0 {
plugin = vars[0]
} else {
// No plugin interface passed as optional argument.
// Obtain it with the saved plugin type string.
p := template.GetPlugin(d.data.PluginType)
if p == nil {
return fmt.Errorf("plugin: failed to prepare data value: plugin with type '%s' not found!", d.data.PluginType)
}
// Assert to plugin template type.
tp, ok := p.(*templatePlugin)
if !ok {
return fmt.Errorf("plugin: failed to prepare data value: failed to assert plugin with type '%s'!", d.data.PluginType)
}
plugin = tp.plugin
}
// Call the plugin interface prepare method to
// obtain the value.
d.value = plugin.Prepare(d)
return nil
}
//##############//
//### Public ###//
//##############//
// GetData obtains the plugin data from the context.
// The context has to be a plugin context.
func GetData(c *template.Context) (*Data, error) {
// Get the plugin store data from the context store.
i, ok := c.StoreGet(contextStoreKeyData)
if !ok {
return nil, fmt.Errorf("plugin: failed to obtain data value from context store: does not exists!")
}
// Assertion.
d, ok := i.(*storeData)
if !ok {
return nil, fmt.Errorf("plugin: failed to assert data interface to store data type!")
}
// Create a new data value.
data := &Data{
data: d,
}
// Obtain the value by calling the prepare method.
err := data.prepareValue()
if err != nil {
return nil, err
}
return data, nil
}
//###############//
//### Private ###//
//###############//
func setDataToContext(c *template.Context, data *Data) {
// Save the plugin store data to the context store.
c.StoreSet(contextStoreKeyData, data.data)
}