This repository was archived by the owner on Sep 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmetadata.go
More file actions
155 lines (128 loc) · 3.42 KB
/
Copy pathmetadata.go
File metadata and controls
155 lines (128 loc) · 3.42 KB
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
/*
* Copyright 2024 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2024 Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package metadata
import (
"fmt"
"strings"
"github.com/tidwall/gjson"
)
const MetadataVersion = 2
type TypeMap map[string]*TypeDefinition
type FunctionMap map[string]*Function
type Metadata struct {
Plugin string `json:"plugin"`
Module string `json:"module"`
SDK string `json:"sdk"`
BuildId string `json:"buildId"`
BuildTime string `json:"buildTs"`
GitRepo string `json:"gitRepo,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`
FnExports FunctionMap `json:"fnExports,omitempty"`
FnImports FunctionMap `json:"fnImports,omitempty"`
Types TypeMap `json:"types,omitempty"`
}
type Docs struct {
Lines []string `json:"lines"`
}
type Function struct {
Name string `json:"-"`
Parameters []*Parameter `json:"parameters,omitempty"`
Results []*Result `json:"results,omitempty"`
Docs *Docs `json:"docs,omitempty"`
}
type TypeDefinition struct {
Name string `json:"-"`
Id uint32 `json:"id,omitempty"`
Fields []*Field `json:"fields,omitempty"`
Docs *Docs `json:"docs,omitempty"`
}
type Parameter struct {
Name string `json:"name"`
Type string `json:"type"`
Default *any `json:"default,omitempty"`
}
type Result struct {
Name string `json:"name,omitempty"`
Type string `json:"type"`
}
type Field struct {
Name string `json:"name"`
Type string `json:"type"`
Docs *Docs `json:"docs,omitempty"`
}
func (p *Parameter) UnmarshalJSON(data []byte) error {
// We need to manually unmarshal the JSON to distinguish between a null default
// value and the absence of a default value.
gjson.ParseBytes(data).ForEach(func(key, value gjson.Result) bool {
switch key.String() {
case "name":
p.Name = value.String()
case "type":
p.Type = value.String()
case "default":
val := value.Value()
if val == nil {
p.Default = new(any)
} else {
p.Default = &val
}
}
return true
})
return nil
}
func (m *Metadata) NameAndVersion() (name string, version string) {
return parseNameAndVersion(m.Plugin)
}
func (m *Metadata) Name() string {
name, _ := m.NameAndVersion()
return name
}
func (m *Metadata) Version() string {
_, version := m.NameAndVersion()
return version
}
func (m *Metadata) SdkNameAndVersion() (name string, version string) {
return parseNameAndVersion(m.SDK)
}
func (m *Metadata) SdkName() string {
name, _ := m.SdkNameAndVersion()
return name
}
func (m *Metadata) SdkVersion() string {
_, version := m.SdkNameAndVersion()
return version
}
func (m *Metadata) GetTypeDefinition(typ string) (*TypeDefinition, error) {
switch typ {
case "[]byte":
return &TypeDefinition{typ, 1, nil, nil}, nil
case "string":
return &TypeDefinition{typ, 2, nil, nil}, nil
}
def, ok := m.Types[typ]
if !ok {
return nil, fmt.Errorf("info for type %s not found in plugin %s", typ, m.Name())
}
return def, nil
}
func (m *Metadata) GetExportedFunctions() []*Function {
var fns []*Function
for _, fn := range m.FnExports {
fns = append(fns, fn)
}
return fns
}
func parseNameAndVersion(s string) (name string, version string) {
i := strings.LastIndex(s, "@")
if i == -1 {
return s, ""
}
return s[:i], s[i+1:]
}