forked from ogen-go/ent2ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotation.go
48 lines (38 loc) · 871 Bytes
/
annotation.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
package ent2ogen
import (
"encoding/json"
"entgo.io/ent/entc/gen"
"entgo.io/ent/schema"
)
var _ schema.Annotation = (*Annotation)(nil)
type Annotation struct {
BindTo string
}
func Bind() Annotation {
return Annotation{}
}
func BindTo(schema string) Annotation {
return Annotation{
BindTo: schema,
}
}
// Name implements schema.Annotation interface.
func (Annotation) Name() string { return "Ent2ogen" }
// Decode unmarshalls the annotation.
func (a *Annotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
return err
}
return json.Unmarshal(buf, a)
}
func annotation(ants gen.Annotations) (*Annotation, error) {
ant := &Annotation{}
if ants != nil && ants[ant.Name()] != nil {
if err := ant.Decode(ants[ant.Name()]); err != nil {
return nil, err
}
return ant, nil
}
return nil, nil
}