Support imported type aliases
[govpp.git] / cmd / binapi-generator / objects.go
1 package main
2
3 import "fmt"
4
5 // Package represents collection of objects parsed from VPP binary API JSON data
6 type Package struct {
7         Name     string
8         Version  string
9         CRC      string
10         Services []Service
11         Enums    []Enum
12         Aliases  []Alias
13         Types    []Type
14         Unions   []Union
15         Messages []Message
16         RefMap   map[string]string
17         Imports  map[string]Import
18 }
19
20 type Import struct {
21         Package string
22 }
23
24 // Service represents VPP binary API service
25 type Service struct {
26         Name        string
27         RequestType string
28         ReplyType   string
29         Stream      bool
30         Events      []string
31 }
32
33 // Enum represents VPP binary API enum
34 type Enum struct {
35         Name    string
36         Type    string
37         Entries []EnumEntry
38 }
39
40 // EnumEntry represents VPP binary API enum entry
41 type EnumEntry struct {
42         Name  string
43         Value interface{}
44 }
45
46 // Alias represents VPP binary API alias
47 type Alias struct {
48         Name   string
49         Type   string
50         Length int
51 }
52
53 // Type represents VPP binary API type
54 type Type struct {
55         Name   string
56         CRC    string
57         Fields []Field
58 }
59
60 // Field represents VPP binary API object field
61 type Field struct {
62         Name         string
63         Type         string
64         Length       int
65         SpecifiedLen bool
66         SizeFrom     string
67         Meta         FieldMeta
68 }
69
70 // FieldMeta represents VPP binary API meta info for field
71 type FieldMeta struct {
72         Limit   int
73         Default string
74 }
75
76 // Union represents VPP binary API union
77 type Union struct {
78         Name   string
79         CRC    string
80         Fields []Field
81 }
82
83 // Message represents VPP binary API message
84 type Message struct {
85         Name   string
86         CRC    string
87         Fields []Field
88 }
89
90 // MessageType represents the type of a VPP message
91 type MessageType int
92
93 const (
94         requestMessage MessageType = iota // VPP request message
95         replyMessage                      // VPP reply message
96         eventMessage                      // VPP event message
97         otherMessage                      // other VPP message
98 )
99
100 // printPackage prints all loaded objects for package
101 func printPackage(pkg *Package) {
102         logf("package: %s %s (%s)", pkg.Name, pkg.Version, pkg.CRC)
103         if len(pkg.Enums) > 0 {
104                 logf(" %d enums:", len(pkg.Enums))
105                 for _, enum := range pkg.Enums {
106                         logf("  - %s: %+v", enum.Name, enum)
107                 }
108         }
109         if len(pkg.Unions) > 0 {
110                 logf(" %d unions:", len(pkg.Unions))
111                 for _, union := range pkg.Unions {
112                         logf("  - %s: %+v", union.Name, union)
113                 }
114         }
115         if len(pkg.Types) > 0 {
116                 logf(" %d types:", len(pkg.Types))
117                 for _, typ := range pkg.Types {
118                         logf("  - %s (%d fields): %+v", typ.Name, len(typ.Fields), typ)
119                 }
120         }
121         if len(pkg.Messages) > 0 {
122                 logf(" %d messages:", len(pkg.Messages))
123                 for _, msg := range pkg.Messages {
124                         logf("  - %s (%d fields) %s", msg.Name, len(msg.Fields), msg.CRC)
125                 }
126         }
127         if len(pkg.Services) > 0 {
128                 logf(" %d services:", len(pkg.Services))
129                 for _, svc := range pkg.Services {
130                         var info string
131                         if svc.Stream {
132                                 info = "(STREAM)"
133                         } else if len(svc.Events) > 0 {
134                                 info = fmt.Sprintf("(EVENTS: %v)", svc.Events)
135                         }
136                         logf("  - %s: %q -> %q %s", svc.Name, svc.RequestType, svc.ReplyType, info)
137                 }
138         }
139 }