Add support for aliases and boolean type
[govpp.git] / cmd / binapi-generator / objects.go
1 package main
2
3 import "strings"
4
5 // Package represents collection of objects parsed from VPP binary API JSON data
6 type Package struct {
7         APIVersion string
8         Enums      []Enum
9         Unions     []Union
10         Types      []Type
11         Aliases    []Alias
12         Messages   []Message
13         Services   []Service
14         RefMap     map[string]string
15 }
16
17 // MessageType represents the type of a VPP message
18 type MessageType int
19
20 const (
21         requestMessage MessageType = iota // VPP request message
22         replyMessage                      // VPP reply message
23         eventMessage                      // VPP event message
24         otherMessage                      // other VPP message
25 )
26
27 // Message represents VPP binary API message
28 type Message struct {
29         Name   string
30         CRC    string
31         Fields []Field
32 }
33
34 // Type represents VPP binary API type
35 type Type struct {
36         Name   string
37         CRC    string
38         Fields []Field
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 // Union represents VPP binary API union
49 type Union 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         SizeFrom string
61 }
62
63 func (f *Field) IsArray() bool {
64         return f.Length > 0 || f.SizeFrom != ""
65 }
66
67 // Enum represents VPP binary API enum
68 type Enum struct {
69         Name    string
70         Type    string
71         Entries []EnumEntry
72 }
73
74 // EnumEntry represents VPP binary API enum entry
75 type EnumEntry struct {
76         Name  string
77         Value interface{}
78 }
79
80 // Service represents VPP binary API service
81 type Service struct {
82         Name        string
83         RequestType string
84         ReplyType   string
85         Stream      bool
86         Events      []string
87 }
88
89 func (s Service) MethodName() string {
90         reqTyp := camelCaseName(s.RequestType)
91
92         // method name is same as parameter type name by default
93         method := reqTyp
94         if s.Stream {
95                 // use Dump as prefix instead of suffix for stream services
96                 if m := strings.TrimSuffix(method, "Dump"); method != m {
97                         method = "Dump" + m
98                 }
99         }
100
101         return method
102 }
103
104 func (s Service) IsDumpService() bool {
105         return s.Stream
106 }
107
108 func (s Service) IsEventService() bool {
109         return len(s.Events) > 0
110 }
111
112 func (s Service) IsRequestService() bool {
113         // some binapi messages might have `null` reply (for example: memclnt)
114         return s.ReplyType != "" && s.ReplyType != "null" // not null
115 }