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