added support for string type
[govpp.git] / cmd / binapi-generator / definitions.go
1 // Copyright (c) 2018 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18         "strconv"
19         "strings"
20         "unicode"
21 )
22
23 func getBinapiTypeSize(binapiType string) int {
24         if _, ok := binapiTypes[binapiType]; ok {
25                 b, err := strconv.Atoi(strings.TrimLeft(binapiType, "uif"))
26                 if err == nil {
27                         return b / 8
28                 } else {
29                         return 1
30                 }
31         }
32         return -1
33 }
34
35 // binapiTypes is a set of types used VPP binary API for translation to Go types
36 var binapiTypes = map[string]string{
37         "bool":   "bool",
38         "u8":     "uint8",
39         "i8":     "int8",
40         "u16":    "uint16",
41         "i16":    "int16",
42         "u32":    "uint32",
43         "i32":    "int32",
44         "u64":    "uint64",
45         "i64":    "int64",
46         "f64":    "float64",
47         "string": "string",
48 }
49
50 func usesInitialism(s string) string {
51         if u := strings.ToUpper(s); commonInitialisms[u] {
52                 return u
53         } else if su, ok := specialInitialisms[u]; ok {
54                 return su
55         }
56         return ""
57 }
58
59 // commonInitialisms is a set of common initialisms that need to stay in upper case.
60 var commonInitialisms = map[string]bool{
61         "ACL": true,
62         "API": true,
63         //"ASCII": true, // there are only two use cases for ASCII which already have initialism before and after
64         "CPU":   true,
65         "CSS":   true,
66         "DNS":   true,
67         "DHCP":  true,
68         "EOF":   true,
69         "GUID":  true,
70         "HTML":  true,
71         "HTTP":  true,
72         "HTTPS": true,
73         "ID":    true,
74         "IP":    true,
75         "ICMP":  true,
76         "JSON":  true,
77         "LHS":   true,
78         "QPS":   true,
79         "PID":   true,
80         "RAM":   true,
81         "RHS":   true,
82         "RPC":   true,
83         "SLA":   true,
84         "SMTP":  true,
85         "SQL":   true,
86         "SSH":   true,
87         "TCP":   true,
88         "TLS":   true,
89         "TTL":   true,
90         "UDP":   true,
91         "UI":    true,
92         "UID":   true,
93         "UUID":  true,
94         "URI":   true,
95         "URL":   true,
96         "UTF8":  true,
97         "VM":    true,
98         "VPN":   true,
99         "XML":   true,
100         "XMPP":  true,
101         "XSRF":  true,
102         "XSS":   true,
103 }
104
105 // specialInitialisms is a set of special initialisms that need part to stay in upper case.
106 var specialInitialisms = map[string]string{
107         "IPV": "IPv",
108         //"IPV4": "IPv4",
109         //"IPV6": "IPv6",
110 }
111
112 // camelCaseName returns correct name identifier (camelCase).
113 func camelCaseName(name string) (should string) {
114         name = strings.Title(name)
115
116         // Fast path for simple cases: "_" and all lowercase.
117         if name == "_" {
118                 return name
119         }
120         allLower := true
121         for _, r := range name {
122                 if !unicode.IsLower(r) {
123                         allLower = false
124                         break
125                 }
126         }
127         if allLower {
128                 return name
129         }
130
131         // Split camelCase at any lower->upper transition, and split on underscores.
132         // Check each word for common initialisms.
133         runes := []rune(name)
134         w, i := 0, 0 // index of start of word, scan
135         for i+1 <= len(runes) {
136                 eow := false // whether we hit the end of a word
137                 if i+1 == len(runes) {
138                         eow = true
139                 } else if runes[i+1] == '_' {
140                         // underscore; shift the remainder forward over any run of underscores
141                         eow = true
142                         n := 1
143                         for i+n+1 < len(runes) && runes[i+n+1] == '_' {
144                                 n++
145                         }
146
147                         // Leave at most one underscore if the underscore is between two digits
148                         if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
149                                 n--
150                         }
151
152                         copy(runes[i+1:], runes[i+n+1:])
153                         runes = runes[:len(runes)-n]
154                 } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
155                         // lower->non-lower
156                         eow = true
157                 }
158                 i++
159                 if !eow {
160                         continue
161                 }
162
163                 // [w,i) is a word.
164                 word := string(runes[w:i])
165                 if u := usesInitialism(word); u != "" {
166                         // Keep consistent case, which is lowercase only at the start.
167                         if w == 0 && unicode.IsLower(runes[w]) {
168                                 u = strings.ToLower(u)
169                         }
170                         // All the common initialisms are ASCII,
171                         // so we can replace the bytes exactly.
172                         copy(runes[w:], []rune(u))
173                 } else if w > 0 && strings.ToLower(word) == word {
174                         // already all lowercase, and not the first word, so uppercase the first character.
175                         runes[w] = unicode.ToUpper(runes[w])
176                 }
177                 w = i
178         }
179         return string(runes)
180 }