forked from pquerna/ffjson
/
parser.go
142 lines (121 loc) · 3.17 KB
/
parser.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
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
/**
* Copyright 2014 Paul Querna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package generator
import (
"flag"
"fmt"
"github.com/pquerna/ffjson/shared"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"regexp"
"strings"
)
var noEncoder = flag.Bool("noencoder", false, "Do not generate encoder functions")
var noDecoder = flag.Bool("nodecoder", false, "Do not generate decoder functions")
type StructField struct {
Name string
}
type StructInfo struct {
Name string
Options shared.StructOptions
}
func NewStructInfo(name string) *StructInfo {
return &StructInfo{
Name: name,
Options: shared.StructOptions{
SkipDecoder: *noDecoder,
SkipEncoder: *noEncoder,
},
}
}
var skipre = regexp.MustCompile("(.*)ffjson:(\\s*)((skip)|(ignore))(.*)")
var skipdec = regexp.MustCompile("(.*)ffjson:(\\s*)((skipdecoder)|(nodecoder))(.*)")
var skipenc = regexp.MustCompile("(.*)ffjson:(\\s*)((skipencoder)|(noencoder))(.*)")
func shouldInclude(d *ast.Object) (bool, error) {
ts, ok := d.Decl.(*ast.TypeSpec)
if !ok {
return false, fmt.Errorf("Unknown type without TypeSec: %v", d)
}
_, ok = ts.Type.(*ast.StructType)
if !ok {
ident, ok := ts.Type.(*ast.Ident)
if !ok || ident.Name == "" {
return false, nil
}
// It must be in this package, and not a pointer alias
if strings.Contains(ident.Name, ".") || strings.Contains(ident.Name, "*") {
return false, nil
}
// if Obj is nil, we have an external type or built-in.
if ident.Obj == nil || ident.Obj.Decl == nil {
return false, nil
}
return shouldInclude(ident.Obj)
}
return true, nil
}
func ExtractStructs(inputPath string) (string, []*StructInfo, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, inputPath, nil, parser.ParseComments)
if err != nil {
return "", nil, err
}
packageName := f.Name.String()
structs := make(map[string]*StructInfo)
for k, d := range f.Scope.Objects {
if d.Kind == ast.Typ {
incl, err := shouldInclude(d)
if err != nil {
return "", nil, err
}
if incl {
stobj := NewStructInfo(k)
structs[k] = stobj
}
}
}
files := map[string]*ast.File{
inputPath: f,
}
pkg, _ := ast.NewPackage(fset, files, nil, nil)
d := doc.New(pkg, f.Name.String(), doc.AllDecls)
for _, t := range d.Types {
if skipre.MatchString(t.Doc) {
delete(structs, t.Name)
} else {
if skipdec.MatchString(t.Doc) {
s, ok := structs[t.Name]
if ok {
s.Options.SkipDecoder = true
}
}
if skipenc.MatchString(t.Doc) {
s, ok := structs[t.Name]
if ok {
s.Options.SkipEncoder = true
}
}
}
}
rv := make([]*StructInfo, 0)
for _, v := range structs {
rv = append(rv, v)
}
return packageName, rv, nil
}