forked from tinylib/msgp
/
directives.go
98 lines (83 loc) · 2.17 KB
/
directives.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
package parse
import (
"fmt"
"github.com/tinylib/msgp/gen"
"go/ast"
"strings"
)
const linePrefix = "//msgp:"
type directive func([]string, *FileSet) error
// map of all recognized directives
//
// to add a directive, define a func([]string, *FileSet) error
// and then add it to this list.
var directives = map[string]directive{
"shim": applyShim,
"ignore": ignore,
"tuple": astuple,
}
// find all comment lines that begin with //msgp:
func yieldComments(c []*ast.CommentGroup) []string {
var out []string
for _, cg := range c {
for _, line := range cg.List {
if strings.HasPrefix(line.Text, linePrefix) {
out = append(out, strings.TrimPrefix(line.Text, linePrefix))
}
}
}
return out
}
//msgp:shim {Type} as:{Newtype} using:{toFunc/fromFunc}
func applyShim(text []string, f *FileSet) error {
if len(text) != 4 {
return fmt.Errorf("shim directive should have 3 arguments; found %d", len(text)-1)
}
name := text[1]
be := gen.Ident(strings.TrimPrefix(strings.TrimSpace(text[2]), "as:")) // parse as::{base}
if name[0] == '*' {
name = name[1:]
be.Needsref(true)
}
be.Alias(name)
usestr := strings.TrimPrefix(strings.TrimSpace(text[3]), "using:") // parse using::{method/method}
methods := strings.Split(usestr, "/")
if len(methods) != 2 {
return fmt.Errorf("expected 2 using::{} methods; found %d (%q)", len(methods), text[3])
}
be.ShimToBase = methods[0]
be.ShimFromBase = methods[1]
infof("applying shim for %s to %s...\n", name, be.Value.String())
f.Identities[name] = be
return nil
}
//msgp:ignore {TypeA} {TypeB}...
func ignore(text []string, f *FileSet) error {
if len(text) < 2 {
return nil
}
for _, item := range text[1:] {
name := strings.TrimSpace(item)
if _, ok := f.Identities[name]; ok {
delete(f.Identities, name)
infof("ignoring: %s...\n", name)
}
}
return nil
}
//msgp:tuple {TypeA} {TypeB}...
func astuple(text []string, f *FileSet) error {
if len(text) < 2 {
return nil
}
for _, item := range text[1:] {
name := strings.TrimSpace(item)
if el, ok := f.Identities[name]; ok {
if st, ok := el.(*gen.Struct); ok {
st.AsTuple = true
infof("using type %s as tuple...\n", name)
}
}
}
return nil
}