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