-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (110 loc) · 2.82 KB
/
main.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
package main
import (
"context"
"fmt"
"github.com/jamestrandung/go-cte/sample/dto"
"github.com/jamestrandung/go-cte/sample/service/scaffolding/endpoint"
"go/ast"
"go/token"
"go/types"
"github.com/jamestrandung/go-cte/sample/config"
"github.com/jamestrandung/go-cte/sample/server"
"golang.org/x/tools/go/packages"
)
func main() {
err := config.Engine.VerifyConfigurations()
fmt.Println("Engine configuration error:", err)
server.Serve()
testEngine()
testPlainGo()
}
func testEngine() {
p := endpoint.NewPlan(
dto.CostRequest{
PointA: "Clementi",
PointB: "Changi Airport",
},
server.Dependencies,
)
if err := p.Execute(context.Background()); err != nil {
fmt.Println(err)
}
//config.Print(p.GetTravelCost())
config.Print(p.GetTotalCost())
config.Print(p.GetVATAmount())
}
func testPlainGo() {
quote, err := server.Handler.Handle(context.Background(), dto.CostRequest{
PointA: "Clementi",
PointB: "Changi Airport",
})
if err != nil {
fmt.Println("Plain Go error:", err)
}
config.Print(quote.TotalCost)
config.Print(quote.VATAmount)
}
const loadMode = packages.NeedName |
packages.NeedFiles |
packages.NeedCompiledGoFiles |
packages.NeedImports |
packages.NeedDeps |
packages.NeedTypes |
packages.NeedSyntax |
packages.NeedTypesInfo
func testParsePackage() {
loadConfig := new(packages.Config)
loadConfig.Mode = loadMode
loadConfig.Fset = token.NewFileSet()
pkgs, err := packages.Load(loadConfig, "github.com/jamestrandung/go-cte/sample/...")
if err != nil {
panic(err)
}
for _, pkg := range pkgs {
for _, syn := range pkg.Syntax {
for _, dec := range syn.Decls {
if gen, ok := dec.(*ast.GenDecl); ok && gen.Tok == token.TYPE {
// print doc comment of the type
// fmt.Println(gen.Doc.List[0])
for _, spec := range gen.Specs {
if ts, ok := spec.(*ast.TypeSpec); ok {
obj, ok := pkg.TypesInfo.Defs[ts.Name]
if !ok {
continue
}
typeName, ok := obj.(*types.TypeName)
if !ok {
continue
}
named, ok := typeName.Type().(*types.Named)
if !ok {
continue
}
// print the full name of the type
fmt.Println(named)
fmt.Println(pkg.TypesInfo.Types[ts.Type].Type)
s, ok := named.Underlying().(*types.Struct)
if !ok {
continue
}
// print the struct's fields and tags
for i := 0; i < s.NumFields(); i++ {
idx := fmt.Sprint(i)
fmt.Println("s.Field(", idx, ").Name(): ", s.Field(i).Name())
fmt.Println("s.Tag(", idx, "): ", s.Tag(i))
}
}
}
}
}
}
}
// pkg, err := importer.Default().Import("github.com/jamestrandung/go-cte/core")
// if err != nil {
// fmt.Printf("error: %s\n", err.Error())
// return
// }
// for _, declName := range pkg.Scope().Names() {
// fmt.Println(declName)
// }
}