change to interface{}

This commit is contained in:
Alexander Wang 2024-10-15 15:37:49 -07:00
parent cec62cb5c4
commit 1f9e1ea2cf
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
6 changed files with 85 additions and 11 deletions

View file

@ -1465,9 +1465,23 @@ func compileConfig(ir *d2ir.Map) (*d2target.Config, error) {
}
f = configMap.GetField("data")
if f != nil && f.Map() != nil {
config.Data = make(map[string]string)
config.Data = make(map[string]interface{})
for _, f := range f.Map().Fields {
config.Data[f.Name] = f.Primary().Value.ScalarString()
if f.Primary() != nil {
config.Data[f.Name] = f.Primary().Value.ScalarString()
} else if f.Composite != nil {
var arr []interface{}
switch c := f.Composite.(type) {
case *d2ir.Array:
for _, f := range c.Values {
switch c := f.(type) {
case *d2ir.Scalar:
arr = append(arr, c.String())
}
}
}
config.Data[f.Name] = arr
}
}
}

View file

@ -4581,12 +4581,14 @@ vars: {
d2-config: {
data: {
cat: hat
later: [1;5;2]
}
}
}
`, ``)
assert.Equal(t, 1, len(config.Data))
assert.Equal(t, 2, len(config.Data))
assert.Equal(t, "hat", config.Data["cat"])
assert.Equal(t, "1", (config.Data["later"]).([]interface{})[0])
},
},
}

View file

@ -60,6 +60,11 @@ type Graph struct {
// Object.Level uses the location of a nested graph
RootLevel int `json:"rootLevel,omitempty"`
// Currently this holds data embedded from source code configuration variables
// Plugins only have access to exported graph, so this data structure allows
// carrying arbitrary metadata that any plugin might handle
Data map[string]interface{} `json:"data,omitempty"`
}
func NewGraph() *Graph {

View file

@ -71,6 +71,9 @@ func Compile(ctx context.Context, input string, compileOpts *CompileOptions, ren
applyConfigs(config, compileOpts, renderOpts)
applyDefaults(compileOpts, renderOpts)
if config != nil {
g.Data = config.Data
}
d, err := compile(ctx, g, compileOpts, renderOpts)
if d != nil {

View file

@ -49,7 +49,7 @@ type Config struct {
DarkThemeOverrides *ThemeOverrides `json:"darkThemeOverrides,omitempty"`
// Data is a data structure for holding user-defined data
// useful for plugins that allow users to configure within source code
Data map[string]string `json:"data,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}
type ThemeOverrides struct {

View file

@ -3,11 +3,11 @@
"name": "",
"isFolderOnly": true,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,0:0:0-8:0:60",
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,0:0:0-9:0:81",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,1:0:1-7:1:59",
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,1:0:1-8:1:80",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,1:0:1-1:4:5",
"path": [
@ -27,11 +27,11 @@
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,1:6:7-7:1:59",
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,1:6:7-8:1:80",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,2:1:10-6:3:57",
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,2:1:10-7:3:78",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,2:1:10-2:10:19",
"path": [
@ -51,11 +51,11 @@
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,2:12:21-6:3:57",
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,2:12:21-7:3:78",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,3:2:25-5:5:53",
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,3:2:25-6:5:74",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,3:2:25-3:6:29",
"path": [
@ -75,7 +75,7 @@
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,3:8:31-5:5:53",
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,3:8:31-6:5:74",
"nodes": [
{
"map_key": {
@ -109,6 +109,56 @@
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,5:6:54-5:20:68",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,5:6:54-5:11:59",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,5:6:54-5:11:59",
"value": [
{
"string": "later",
"raw_string": "later"
}
]
}
}
]
},
"primary": {},
"value": {
"array": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,5:13:61-6:0:69",
"nodes": [
{
"number": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,5:14:62-5:15:63",
"raw": "1",
"value": "1"
}
},
{
"number": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,5:16:64-5:17:65",
"raw": "5",
"value": "5"
}
},
{
"number": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/config/data.d2,5:18:66-5:19:67",
"raw": "2",
"value": "2"
}
}
]
}
}
}
}
]
}