/
getast.go
1145 lines (1033 loc) · 26.8 KB
/
getast.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package parse
import (
"bytes"
"fmt"
"go/ast"
"path/filepath"
//"go/importer"
"go/format"
"go/parser"
"go/token"
"go/types"
"os"
"reflect"
"sort"
"strconv"
"strings"
"github.com/glycerine/greenpack/cfg"
"github.com/glycerine/greenpack/gen"
// "github.com/shurcooL/go-goon"
"github.com/glycerine/greenpack/msgp"
"golang.org/x/tools/go/loader"
)
var StopOnError bool
// A FileSet is the in-memory representation of a
// parsed file.
type FileSet struct {
Package string // package name
Specs map[string]ast.Expr // type specs in file
Identities map[string]gen.Elem // processed from specs
Directives []string // raw preprocessor directives
Imports []*ast.ImportSpec // imports
Cfg *cfg.GreenConfig
ZebraSchemaId int64
PackageInfo *loader.PackageInfo
LoadedProg *loader.Program
QuickPack map[string]*loader.PackageInfo
Fset *token.FileSet
InterfaceTypeNames map[string]bool // so we can heuristically identify interfaces.
}
// File parses a file at the relative path
// provided and produces a new *FileSet.
// If you pass in a path to a directory, the entire
// directory will be parsed.
// If unexport is false, only exported identifiers are included in the FileSet.
// If the resulting FileSet would be empty, an error is returned.
func File(c *cfg.GreenConfig) (*FileSet, error) {
ok, isDir := fileOrDir(c.GoFile)
if !ok {
return nil, fmt.Errorf("error: path '%s' does not exist", c.GoFile)
}
name := c.GoFile
pushstate(name)
defer popstate()
fs := &FileSet{
Specs: make(map[string]ast.Expr),
Identities: make(map[string]gen.Elem),
Cfg: c,
InterfaceTypeNames: make(map[string]bool),
}
var filenames []string
var err error
if isDir {
filenames, err = ListOfGoFilesInDir(name)
if err != nil {
return nil, err
}
} else {
filenames = []string{name}
}
packageName, err := getPackageNameFromGoFile(filenames[0])
if err != nil {
return nil, err
}
fset := token.NewFileSet()
fs.Fset = fset
// loading/type checking is needed to get the constants involved
// in array definitions. For example, the constant
// `n` in the declaration `type S struct { arr [n]int }`;
// `n` might be a constant in another package.
// Hence we must load and thereby fully resolve
// constants; we can't get away with doing a shallow parse.
lc := loader.Config{
Fset: fset,
CreatePkgs: []loader.PkgSpec{{Filenames: filenames}},
ParserMode: parser.ParseComments,
}
lprog, err := lc.Load()
if err != nil {
return nil, fmt.Errorf("error in getast.go: loader.Load() error: '%v'", err)
}
fs.LoadedProg = lprog
pkgInfo := lprog.Package(packageName)
if pkgInfo == nil {
panic(fmt.Errorf("load of '%s' for package name '%s' failed", name, packageName))
}
if !pkgInfo.TransitivelyErrorFree {
panic(fmt.Errorf("loader detected (possibly transitive) error during package load"))
}
// map from short package import name to the package
quickPack := make(map[string]*loader.PackageInfo)
for k, v := range lprog.AllPackages {
quickPack[k.Name()] = v
}
fs.QuickPack = quickPack
fs.Package = pkgInfo.Pkg.Name()
fs.PackageInfo = pkgInfo
gotZebraSchema := false
if isDir {
for _, fl := range pkgInfo.Files {
pushstate(fl.Name.Name)
fs.Directives = append(fs.Directives, yieldComments(fl.Comments)...)
fs.getInterfaceNames(fl)
if !gotZebraSchema {
// must get zebraSchemaId prior to FileExports(), as it dumps non-exports.
fs.getZebraSchemaId(fl)
gotZebraSchema = true
}
if !c.Unexported {
ast.FileExports(fl)
}
fs.getTypeSpecs(fl)
popstate()
}
} else {
if len(pkgInfo.Files) != 1 {
fmt.Printf("debug: expected single file, but got: len(pkgInfo.Files) = %v\n", len(pkgInfo.Files))
panic("huh?!? what to do with multiple or zero files here?")
}
f := pkgInfo.Files[0]
fs.Directives = yieldComments(f.Comments)
fs.getZebraSchemaId(f)
if !c.Unexported {
ast.FileExports(f)
}
fs.getTypeSpecs(f)
}
if len(fs.Specs) == 0 {
return nil, fmt.Errorf("no definitions in %s", name)
}
err = fs.process()
if err != nil {
return nil, err
}
fs.applyDirectives()
fs.propInline()
return fs, nil
}
// applyDirectives applies all of the directives that
// are known to the parser. additional method-specific
// directives remain in f.Directives
func (f *FileSet) applyDirectives() {
newdirs := make([]string, 0, len(f.Directives))
for _, d := range f.Directives {
chunks := strings.Split(d, " ")
if len(chunks) > 0 {
if fn, ok := directives[chunks[0]]; ok {
pushstate(chunks[0])
err := fn(chunks, f)
if err != nil {
warnln(err.Error())
}
popstate()
} else {
newdirs = append(newdirs, d)
}
}
}
f.Directives = newdirs
}
// A linkset is a graph of unresolved
// identities.
//
// Since gen.Ident can only represent
// one level of type indirection (e.g. Foo -> uint8),
// type declarations like `type Foo Bar`
// aren't resolve-able until we've processed
// everything else.
//
// The goal of this dependency resolution
// is to distill the type declaration
// into just one level of indirection.
// In other words, if we have:
//
// type A uint64
// type B A
// type C B
// type D C
//
// ... then we want to end up
// figuring out that D is just a uint64.
type linkset map[string]*gen.BaseElem
func (f *FileSet) resolve(ls linkset) {
progress := true
for progress && len(ls) > 0 {
progress = false
for name, elem := range ls {
real, ok := f.Identities[elem.TypeName()]
if ok {
// copy the old type descriptor,
// alias it to the new value,
// and insert it into the resolved
// identities list
progress = true
nt := real.Copy()
nt.Alias(name)
f.Identities[name] = nt
delete(ls, name)
}
}
}
// what's left can't be resolved
for name, elem := range ls {
warnf("couldn't resolve type %s (%s)\n", name, elem.TypeName())
}
}
// process takes the contents of f.Specs and
// uses them to populate f.Identities
func (f *FileSet) process() error {
deferred := make(linkset)
parse:
for name, def := range f.Specs {
pushstate(name)
el, err := f.parseExpr(def, false)
if err != nil {
return err
}
if el == nil {
warnln("failed to parse")
popstate()
continue parse
}
// push unresolved identities into
// the graph of links and resolve after
// we've handled every possible named type.
if be, ok := el.(*gen.BaseElem); ok && be.Value == gen.IDENT {
deferred[name] = be
popstate()
continue parse
}
el.Alias(name)
f.Identities[name] = el
popstate()
}
if len(deferred) > 0 {
f.resolve(deferred)
}
return nil
}
func strToMethod(s string) gen.Method {
switch s {
case "encode":
return gen.Encode
case "decode":
return gen.Decode
case "test":
return gen.Test
case "size":
return gen.Size
case "marshal":
return gen.Marshal
case "unmarshal":
return gen.Unmarshal
default:
return 0
}
}
func (f *FileSet) applyDirs(p *gen.Printer) {
// apply directives of the form
//
// //msgp:encode ignore {{TypeName}}
//
loop:
for _, d := range f.Directives {
chunks := strings.Split(d, " ")
if len(chunks) > 1 {
for i := range chunks {
chunks[i] = strings.TrimSpace(chunks[i])
}
m := strToMethod(chunks[0])
if m == 0 {
warnf("unknown pass name: %q\n", chunks[0])
continue loop
}
if fn, ok := passDirectives[chunks[1]]; ok {
pushstate(chunks[1])
err := fn(m, chunks[2:], p)
if err != nil {
warnf("error applying directive: %s\n", err)
}
popstate()
} else {
warnf("unrecognized directive %q\n", chunks[1])
}
} else {
warnf("empty directive: %q\n", d)
}
}
}
func (f *FileSet) PrintTo(p *gen.Printer) error {
f.applyDirs(p)
names := make([]string, 0, len(f.Identities))
for name := range f.Identities {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
el := f.Identities[name]
el.SetVarname("z")
pushstate(el.TypeName())
err := p.Print(el)
popstate()
if err != nil {
return err
}
}
return nil
}
// getTypeSpecs extracts all of the *ast.TypeSpecs in the file
// into fs.Identities, but does not set the actual element
func (fs *FileSet) getTypeSpecs(f *ast.File) {
// collect all imports...
fs.Imports = append(fs.Imports, f.Imports...)
// check all declarations...
for i := range f.Decls {
switch g := f.Decls[i].(type) {
case *ast.GenDecl:
// and check the specs...
for _, s := range g.Specs {
// for ast.TypeSpecs....
switch ts := s.(type) {
case *ast.TypeSpec:
switch ts.Type.(type) {
// this is the list of parse-able
// type specs
case *ast.StructType,
*ast.ArrayType,
*ast.StarExpr,
*ast.MapType,
*ast.Ident:
fs.Specs[ts.Name.Name] = ts.Type
}
}
}
}
}
}
func fieldName(f *ast.Field) string {
switch len(f.Names) {
case 0:
return stringify(f.Type)
case 1:
return f.Names[0].Name
default:
return f.Names[0].Name + " (and others)"
}
}
type zid struct {
zid int64
fieldName string
origPos int
}
type zidSetSlice []zid
func (p zidSetSlice) Len() int { return len(p) }
// sort negatives to the end, so that our
// non-zid (-1) fields come last. This lets
// us write deterministically the zid fields,
// in order. For -1 fields without zid, sort them by
// their original appearance order to
// maintain stability.
func (p zidSetSlice) Less(i, j int) bool {
if p[i].zid < 0 && p[j].zid < 0 {
return p[i].origPos < p[j].origPos
}
if p[i].zid >= 0 && p[j].zid >= 0 {
return p[i].zid < p[j].zid
}
if p[i].zid < 0 && p[j].zid >= 0 {
// sort negative to the end
return false
}
// p[i].zid >= 0 && p[j].zid < 0
return true // sort negatives to the end
}
func (p zidSetSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (fs *FileSet) parseFieldList(fl *ast.FieldList) ([]gen.StructField, error) {
if fl == nil || fl.NumFields() == 0 {
return nil, nil
}
out := make([]gen.StructField, 0, fl.NumFields())
var zidSet []zid
var origPos int
hasZid := false
for _, field := range fl.List {
pushstate(fieldName(field))
fds, err := fs.getField(field)
if err != nil {
fatalf(err.Error())
return nil, err
}
for _, x := range fds {
//fmt.Printf("\n on field '%#v'\n", x)
if x.ZebraId >= 0 {
hasZid = true
}
zidSet = append(zidSet, zid{zid: x.ZebraId, fieldName: x.FieldName, origPos: origPos})
origPos++
}
if len(fds) > 0 {
out = append(out, fds...)
} else {
warnln(fmt.Sprintf("ignored. heh, on '%#v'", fs))
}
popstate()
}
// check zidSet sequential from 0, no gaps, no duplicates
if hasZid {
sort.Sort(zidSetSlice(zidSet))
if zidSet[0].zid != 0 {
return nil, fmt.Errorf("zid (zebra id tags on struct fields) must start at 0; lowest zid was '%v' at field '%v'", zidSet[0].zid, zidSet[0].fieldName)
}
for i := range zidSet {
if zidSet[i].zid >= 0 {
if zidSet[i].zid != int64(i) {
return nil, fmt.Errorf("zid sequence interrupted - commit conflict possible! gap or duplicate in zid sequence (saw %v; expected %v), near field '%v'", zidSet[i].zid, i, zidSet[i].fieldName)
}
}
}
// now re-arrange fields into zid order,
// so that writes are deterministic, and
// we don't have randomized field order.
sortedOut := make([]gen.StructField, len(out))
if len(out) != len(zidSet) {
panic("size mismatch, out and zidSet must be the same len")
}
for i := range zidSet {
sortedOut[i] = out[zidSet[i].origPos]
}
out = sortedOut
}
return out, nil
}
func anyMatches(haystack []string, needle string) bool {
needle = strings.TrimSpace(needle)
for _, v := range haystack {
tr := strings.TrimSpace(v)
if tr == needle {
return true
}
}
return false
}
// translate *ast.Field into []gen.StructField
func (fs *FileSet) getField(f *ast.Field) ([]gen.StructField, error) {
sf := make([]gen.StructField, 1)
var extension bool
var omitempty bool
var skip bool
var deprecated bool
var showzero bool
var zebraId int64 = -1
var isIface bool
var isPointer bool
// parse tag; otherwise field name is field tag
if f.Tag != nil {
alltags := reflect.StructTag(strings.Trim(f.Tag.Value, "`"))
body := alltags.Get("msg")
tags := strings.Split(body, ",")
if len(tags) == 2 && tags[1] == "extension" {
extension = true
}
// must use msg:",omitempty" if no alt name, to
// mark a field omitempty. this avoids confusion
// with any alt name, which always comes first.
if len(tags) > 1 && anyMatches(tags[1:], "omitempty") {
omitempty = true
}
if len(tags) > 1 && anyMatches(tags[1:], "deprecated") {
deprecated = true
}
if len(tags) > 1 && anyMatches(tags[1:], "showzero") {
showzero = true
}
if len(tags) > 1 && anyMatches(tags[1:], "iface") {
isIface = true
}
// ignore "-" fields
if tags[0] == "-" {
skip = true
// can't return early, need to track deprecated zids.
//return nil, nil
}
if len(tags[0]) > 0 {
sf[0].FieldTag = tags[0]
}
// check deprecated
dep := alltags.Get("deprecated")
if dep == "true" {
deprecated = true
// ignore these too, but still need them to detect
// gaps in the zebra:id fields
}
// check zebra
zebra := alltags.Get("zid")
if zebra != "" {
// must be a non-negative number. "-" or negative
// are marked as skipped.
z := strings.Trim(zebra, " \t")
if len(z) > 0 && z[0] == '-' {
skip = true
} else {
id, err := strconv.Atoi(zebra)
if err != nil {
where := ""
if len(f.Names) > 0 {
where = " on '" + f.Names[0].Name + "'"
}
err2 := fmt.Errorf("bad `zid` tag%s, could not convert"+
" '%v' to non-zero integer: %v", where, zebra, err)
fatalf(err2.Error())
return nil, err2
}
if id < 0 {
skip = true
} else {
zebraId = int64(id)
//fmt.Printf("\n we see zebraId: %v\n", zebraId)
}
}
if len(f.Names) > 1 {
// we can't have one zid for two fields.
err2 := fmt.Errorf("error: problem with the `zid` tag '%v' on '%s' and '%s': only one zid per field allowed. Move each to its own line and give each its own zid tag.", zebra, f.Names[0].Name, f.Names[1].Name)
fatalf(err2.Error())
return nil, err2
}
}
}
// New: auto-detection of interfaces and slices of interfaces.
// This interface detection method should always be avail, even without full load + typecheck.
//
// But it might be susceptible to false positives on name collisions?? for out of package names in particular?
// We could always go back to forcing users to manually annotate interfaces,
// but that is somewhat painful. We'd prefer greenpack not crash on the user
// because they didn't mark a type manually as an interface.
//
if !isIface {
typnm := extractTypeName(f.Type)
//fmt.Printf("\n DEBUG: extracted type name '%s'\n", typnm)
if fs.InterfaceTypeNames[typnm] {
//fmt.Printf("\n DEBUG: detected that f.Names[0]='%s' is an interface\n", typnm)
isIface = true
}
}
ex, err := fs.parseExpr(f.Type, isIface)
if err != nil {
fatalf(err.Error())
return nil, err
}
if ex == nil {
skip = true
//fmt.Printf("\n we see nil field %#v\n", f.Names[0])
// struct{} type fields, must track for zid checking.
// so we can't return early here.
} else {
ex.SetZid(zebraId)
}
if _, isPtr := ex.(*gen.Ptr); isPtr {
isPointer = true
}
// try hard to identify if isIface.
// this might only be avail on full load.
if !isIface && fs != nil && fs.PackageInfo != nil &&
len(fs.PackageInfo.Info.Types) > 0 {
if tv, ok := fs.PackageInfo.Info.Types[f.Type]; ok {
isIface = types.IsInterface(tv.Type)
}
}
sf[0].Deprecated = deprecated
sf[0].OmitEmpty = omitempty
sf[0].ZebraId = zebraId
sf[0].Skip = skip
sf[0].ShowZero = showzero
sf[0].IsIface = isIface
sf[0].IsPointer = isPointer
// parse field name
switch len(f.Names) {
case 0:
sf[0].FieldName = embedded(f.Type)
case 1:
sf[0].FieldName = f.Names[0].Name
default:
// this is for a multiple in-line declaration,
// e.g. type A struct { One, Two int }
sf = sf[0:0]
for _, nm := range f.Names {
sf = append(sf, gen.StructField{
FieldTag: nm.Name,
FieldName: nm.Name,
FieldElem: ex.Copy(),
OmitEmpty: omitempty,
Deprecated: deprecated,
ZebraId: zebraId,
Skip: skip,
FieldTagZidClue: msgp.Clue2Field(nm.Name, ex.TypeClue(), zebraId),
IsIface: isIface,
IsPointer: isPointer,
})
}
return sf, nil
}
sf[0].FieldElem = ex
if sf[0].FieldTag == "" {
sf[0].FieldTag = sf[0].FieldName
}
if !skip {
sf[0].FieldTagZidClue = msgp.Clue2Field(sf[0].FieldTag, ex.TypeClue(), zebraId)
}
// validate extension
if extension {
switch ex := ex.(type) {
case *gen.Ptr:
if b, ok := ex.Value.(*gen.BaseElem); ok {
b.Value = gen.Ext
} else {
warnln("couldn't cast to extension.")
return nil, nil
}
case *gen.BaseElem:
ex.Value = gen.Ext
default:
warnln("couldn't cast to extension.")
return nil, nil
}
}
return sf, nil
}
// extract embedded field name
//
// so, for a struct like
//
// type A struct {
// io.Writer
// }
//
// we want "Writer"
func embedded(f ast.Expr) string {
switch f := f.(type) {
case *ast.Ident:
return f.Name
case *ast.StarExpr:
return embedded(f.X)
case *ast.SelectorExpr:
return f.Sel.Name
default:
// other possibilities are disallowed
return ""
}
}
// stringify a field type name
func stringify(e ast.Expr) string {
switch e := e.(type) {
case *ast.Ident:
return e.Name
case *ast.StarExpr:
return "*" + stringify(e.X)
case *ast.SelectorExpr:
return stringify(e.X) + "." + e.Sel.Name
case *ast.ArrayType:
if e.Len == nil {
return "[]" + stringify(e.Elt)
}
return fmt.Sprintf("[%s]%s", stringify(e.Len), stringify(e.Elt))
case *ast.InterfaceType:
if e.Methods == nil || e.Methods.NumFields() == 0 {
return "interface{}"
}
}
return "<BAD>"
}
// recursively translate ast.Expr to gen.Elem; nil means type not supported
// expected input types:
// - *ast.MapType (map[T]J)
// - *ast.Ident (name)
// - *ast.ArrayType ([(sz)]T)
// - *ast.StarExpr (*T)
// - *ast.StructType (struct {})
// - *ast.SelectorExpr (a.B)
// - *ast.InterfaceType (interface {})
func (fs *FileSet) parseExpr(e ast.Expr, isIface bool) (gen.Elem, error) {
switch e := e.(type) {
case *ast.MapType:
if k, ok := e.Key.(*ast.Ident); ok && k.Name == "string" {
in, err := fs.parseExpr(e.Value, false)
panicOn(err)
if in != nil {
return &gen.Map{Value: in, KeyTyp: "String", KeyDeclTyp: "string"}, nil
}
}
// support int64/int32/int keys
if k, ok := e.Key.(*ast.Ident); ok {
in, err := fs.parseExpr(e.Value, isIface)
if err != nil {
fatalf(err.Error())
}
if in != nil {
switch k.Name {
case "int64":
return &gen.Map{Value: in, KeyTyp: "Int64", KeyDeclTyp: "int64"}, nil
case "int32":
return &gen.Map{Value: in, KeyTyp: "Int32", KeyDeclTyp: "int32"}, nil
case "int":
return &gen.Map{Value: in, KeyTyp: "Int", KeyDeclTyp: "int"}, nil
}
}
}
return nil, nil
case *ast.Ident:
if !isIface && e.Obj != nil && fs != nil && fs.PackageInfo != nil &&
len(fs.PackageInfo.Info.Types) > 0 {
if tv, ok := fs.PackageInfo.Info.Types[e]; ok {
isIface = types.IsInterface(tv.Type)
}
}
b := gen.Ident(e.Name, isIface)
// work to resove this expression
// can be done later, once we've resolved
// everything else.
if b.Value == gen.IDENT {
if _, ok := fs.Specs[e.Name]; !ok {
warnf("non-local identifier: %s\n", e.Name)
}
}
return b, nil
case *ast.ArrayType:
// special case for []byte
if e.Len == nil {
if i, ok := e.Elt.(*ast.Ident); ok && i.Name == "byte" {
return &gen.BaseElem{Value: gen.Bytes}, nil
}
}
// return early if we don't know
// what the slice element type is
els, err := fs.parseExpr(e.Elt, isIface)
if err != nil {
return nil, err
}
if els == nil {
return nil, nil
}
// array and not a slice
if e.Len != nil {
switch s := e.Len.(type) {
case *ast.BasicLit:
return &gen.Array{
SizeNamed: s.Value,
SizeResolved: s.Value,
Els: els,
}, nil
case *ast.Ident:
return &gen.Array{
SizeNamed: s.String(),
SizeResolved: s.String(),
Els: els,
}, nil
case *ast.SelectorExpr:
// fmt.Printf("debug SelectorExpr s where s.X is type %T/%#v:\n", s.X, s.X) // *ast.Ident
// get the package, e.g. msgp in the _generated/def.go
// type Things struct{Arr [msgp.ExtensionPrefixSize]float64} example.
var obj types.Object
// default to current pkg
selPkg := fs.PackageInfo
// but actually lookup in the imported package, if one is used:
switch y := s.X.(type) {
case *ast.Ident:
found := false
selPkg, found = fs.QuickPack[y.Name]
if !found {
fmt.Fprintf(os.Stderr, "%v",
fmt.Errorf("\nparse/getast.go:parseExpr() fatal "+
"error: could not find package "+
"named '%s' for selector '%s'. Try "+
"omitting the -no-load flag if it is in use.\n",
y.Name, stringify(s)))
os.Exit(1)
}
default:
// ignore, no package
fmt.Printf("ignoring, no package; s.X=%#v\n", s.X)
}
// get the scope:
_, obj = selPkg.Pkg.Scope().LookupParent(s.Sel.Name, token.NoPos)
switch cnst := obj.(type) {
case *types.Const:
asStr := cnst.Val().String()
//fmt.Printf("debug s.Sel.Name '%s' resolved to '%s'\n", s.Sel.Name, asStr)
return &gen.Array{
SizeNamed: stringify(s),
SizeResolved: asStr,
Els: els,
}, nil
default:
panic(fmt.Errorf("what to do with type %T here???", cnst))
}
default:
return nil, nil
}
}
return &gen.Slice{Els: els}, nil
case *ast.StarExpr:
v, err := fs.parseExpr(e.X, isIface)
if err != nil {
return nil, err
}
if v != nil {
return &gen.Ptr{Value: v}, nil
}
return nil, nil
case *ast.StructType:
fields, err := fs.parseFieldList(e.Fields)
if err != nil {
return nil, err
}
skipN := 0
for i := range fields {
if fields[i].Skip {
skipN++
}
}
if len(fields) > 0 {
return &gen.Struct{Fields: fields, SkipCount: skipN}, nil
}
return nil, nil
case *ast.SelectorExpr:
if !isIface && e.Sel.Obj != nil && fs != nil && fs.PackageInfo != nil &&
len(fs.PackageInfo.Info.Types) > 0 {
if tv, ok := fs.PackageInfo.Info.Types[e]; ok {
isIface = types.IsInterface(tv.Type)
}
}
return gen.Ident(stringify(e), isIface), nil
case *ast.InterfaceType:
// support `interface{}`
if len(e.Methods.List) == 0 {
return &gen.BaseElem{Value: gen.Intf}, nil
}
return nil, nil
default: // other types not supported
return nil, nil
}
}
func infof(s string, v ...interface{}) {
pushstate(s)
fmt.Printf(strings.Join(logctx, ": "), v...)
popstate()
}
func infoln(s string) {
pushstate(s)
fmt.Println(strings.Join(logctx, ": "))
popstate()
}
func warnf(s string, v ...interface{}) {
pushstate(s)
fmt.Printf(strings.Join(logctx, ": "), v...)
popstate()
}
func warnln(s string) {
pushstate(s)
fmt.Println(strings.Join(logctx, ": "))
popstate()
}
func fatalf(s string, v ...interface{}) {
pushstate(s)
fmt.Printf(strings.Join(logctx, ": "), v...)
popstate()
}
var logctx []string
// push logging state
func pushstate(s string) {
logctx = append(logctx, s)
}
// pop logging state
func popstate() {
logctx = logctx[:len(logctx)-1]
}
func panicOn(err error) {
if err != nil {
panic(err)
}
}
func (fs *FileSet) getZebraSchemaId(f *ast.File) {
//fmt.Printf("\n starting getZebraSchemaId\n")
for i := range f.Decls {
switch g := f.Decls[i].(type) {
case *ast.GenDecl:
for _, s := range g.Specs {
switch ts := s.(type) {
case *ast.ValueSpec:
if len(ts.Names) > 0 && len(ts.Values) > 0 {
if ts.Names[0].Name == "greenSchemaId64" {
switch specid := ts.Values[0].(type) {
case *ast.BasicLit:
//fmt.Printf("\n !!!!! \n got a BasicLit %T/%#v\n", specid, specid)
n, err := strconv.ParseInt(specid.Value, 0, 64)