Make the warnings for validating services more obvious
[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         Messages   []Message
12         Services   []Service
13         RefMap     map[string]string
14 }
15
16 // MessageType represents the type of a VPP message
17 type MessageType int
18
19 const (
20         requestMessage MessageType = iota // VPP request message
21         replyMessage                      // VPP reply message
22         eventMessage                      // VPP event message
23         otherMessage                      // other VPP message
24 )
25
26 // Message represents VPP binary API message
27 type Message struct {
28         Name   string
29         CRC    string
30         Fields []Field
31 }
32
33 // Type represents VPP binary API type
34 type Type struct {
35         Name   string
36         CRC    string
37         Fields []Field
38 }
39
40 // Union represents VPP binary API union
41 type Union struct {
42         Name   string
43         CRC    string
44         Fields []Field
45 }
46
47 // Field represents VPP binary API object field
48 type Field struct {
49         Name     string
50         Type     string
51         Length   int
52         SizeFrom string
53 }
54
55 func (f *Field) IsArray() bool {
56         return f.Length > 0 || f.SizeFrom != ""
57 }
58
59 // Enum represents VPP binary API enum
60 type Enum struct {
61         Name    string
62         Type    string
63         Entries []EnumEntry
64 }
65
66 // EnumEntry represents VPP binary API enum entry
67 type EnumEntry struct {
68         Name  string
69         Value interface{}
70 }
71
72 // Service represents VPP binary API service
73 type Service struct {
74         Name        string
75         RequestType string
76         ReplyType   string
77         Stream      bool
78         Events      []string
79 }
80
81 func (s Service) MethodName() string {
82         reqTyp := camelCaseName(s.RequestType)
83
84         // method name is same as parameter type name by default
85         method := reqTyp
86         if s.Stream {
87                 // use Dump as prefix instead of suffix for stream services
88                 if m := strings.TrimSuffix(method, "Dump"); method != m {
89                         method = "Dump" + m
90                 }
91         }
92
93         return method
94 }
95
96 func (s Service) IsDumpService() bool {
97         return s.Stream
98 }
99
100 func (s Service) IsEventService() bool {
101         return len(s.Events) > 0
102 }
103
104 func (s Service) IsRequestService() bool {
105         // some binapi messages might have `null` reply (for example: memclnt)
106         return s.ReplyType != "" && s.ReplyType != "null" // not null
107 }
108
109 func getSizeOfType(typ *Type) (size int) {
110         for _, field := range typ.Fields {
111                 if n := getBinapiTypeSize(field.Type); n > 0 {
112                         if field.Length > 0 {
113                                 size += n * field.Length
114                         } else {
115                                 size += n
116                         }
117                 }
118         }
119         return size
120 }