-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtype.go
More file actions
98 lines (79 loc) · 1.45 KB
/
type.go
File metadata and controls
98 lines (79 loc) · 1.45 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
package raiden
import (
"encoding/json"
"github.com/sev-2/raiden/pkg/supabase/objects"
)
const (
DefaultTypeSchema = "public"
)
type (
Type interface {
Name() string
Schema() string
Format() string
Enums() []string
Attributes() []objects.TypeAttribute
Comment() *string
}
)
// ----- base type default function -----
type TypeBase struct {
Value any
}
func (*TypeBase) Name() string {
return ""
}
func (*TypeBase) Schema() string {
return DefaultTypeSchema
}
func (*TypeBase) Format() string {
return ""
}
func (*TypeBase) Enums() []string {
return []string{}
}
func (*TypeBase) Attributes() []objects.TypeAttribute {
return []objects.TypeAttribute{}
}
func (*TypeBase) Comment() *string {
return nil
}
func (t *TypeBase) SetValue(v any) {
if len(t.Enums()) == 0 {
t.Value = v
return
}
var found bool
for _, en := range t.Enums() {
if !found && en == v {
found = true
}
}
if !found {
return
}
t.Value = v
}
func (t *TypeBase) GetValue() any {
return t.Value
}
// Implement custom JSON marshaling
func (t TypeBase) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Value)
}
// Implement custom JSON unmarshaling
func (t *TypeBase) UnmarshalJSON(data []byte) error {
var v any
if err := json.Unmarshal(data, &v); err != nil {
return err
}
t.Value = v
return nil
}
// String returns the enum as a string
func (t *TypeBase) String() string {
if str, ok := t.Value.(string); ok {
return str
}
return ""
}