4b424f5e7ff0429906c3cf78ed814211e289be4d
[govpp.git] / cmd / binapi-generator / objects.go
1 package main
2
3 import (
4         "strings"
5 )
6
7 // Package represents collection of objects parsed from VPP binary API JSON data
8 type Package struct {
9         APIVersion 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 // Union represents VPP binary API union
56 type Union struct {
57         Name   string
58         CRC    string
59         Fields []Field
60 }
61
62 // Message represents VPP binary API message
63 type Message struct {
64         Name   string
65         CRC    string
66         Fields []Field
67 }
68
69 // MessageType represents the type of a VPP message
70 type MessageType int
71
72 const (
73         requestMessage MessageType = iota // VPP request message
74         replyMessage                      // VPP reply message
75         eventMessage                      // VPP event message
76         otherMessage                      // other VPP message
77 )
78
79 // Field represents VPP binary API object field
80 type Field struct {
81         Name     string
82         Type     string
83         Length   int
84         SizeFrom string
85 }
86
87 func (f *Field) IsArray() bool {
88         return f.Length > 0 || f.SizeFrom != ""
89 }
90
91 func (s Service) MethodName() string {
92         reqTyp := camelCaseName(s.RequestType)
93
94         // method name is same as parameter type name by default
95         method := reqTyp
96         if s.Stream {
97                 // use Dump as prefix instead of suffix for stream services
98                 if m := strings.TrimSuffix(method, "Dump"); method != m {
99                         method = "Dump" + m
100                 }
101         }
102
103         return method
104 }
105
106 func (s Service) IsDumpService() bool {
107         return s.Stream
108 }
109
110 func (s Service) IsEventService() bool {
111         return len(s.Events) > 0
112 }
113
114 func (s Service) IsRequestService() bool {
115         // some binapi messages might have `null` reply (for example: memclnt)
116         return s.ReplyType != "" && s.ReplyType != "null" // not null
117 }