Refactor GoVPP
[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                 }
29         }
30         return -1
31 }
32
33 // binapiTypes is a set of types used VPP binary API for translation to Go types
34 var binapiTypes = map[string]string{
35         "u8":  "uint8",
36         "i8":  "int8",
37         "u16": "uint16",
38         "i16": "int16",
39         "u32": "uint32",
40         "i32": "int32",
41         "u64": "uint64",
42         "i64": "int64",
43         "f64": "float64",
44 }
45
46 func usesInitialism(s string) string {
47         if u := strings.ToUpper(s); commonInitialisms[u] {
48                 return u
49         } else if su, ok := specialInitialisms[u]; ok {
50                 return su
51         }
52         return ""
53 }
54
55 // commonInitialisms is a set of common initialisms that need to stay in upper case.
56 var commonInitialisms = map[string]bool{
57         "ACL": true,
58         "API": true,
59         //"ASCII": true, // there are only two use cases for ASCII which already have initialism before and after
60         "CPU":   true,
61         "CSS":   true,
62         "DNS":   true,
63         "DHCP":  true,
64         "EOF":   true,
65         "GUID":  true,
66         "HTML":  true,
67         "HTTP":  true,
68         "HTTPS": true,
69         "ID":    true,
70         "IP":    true,
71         "ICMP":  true,
72         "JSON":  true,
73         "LHS":   true,
74         "QPS":   true,
75         "PID":   true,
76         "RAM":   true,
77         "RHS":   true,
78         "RPC":   true,
79         "SLA":   true,
80         "SMTP":  true,
81         "SQL":   true,
82         "SSH":   true,
83         "TCP":   true,
84         "TLS":   true,
85         "TTL":   true,
86         "UDP":   true,
87         "UI":    true,
88         "UID":   true,
89         "UUID":  true,
90         "URI":   true,
91         "URL":   true,
92         "UTF8":  true,
93         "VM":    true,
94         "VPN":   true,
95         "XML":   true,
96         "XMPP":  true,
97         "XSRF":  true,
98         "XSS":   true,
99 }
100
101 // specialInitialisms is a set of special initialisms that need part to stay in upper case.
102 var specialInitialisms = map[string]string{
103         "IPV": "IPv",
104         //"IPV4": "IPv4",
105         //"IPV6": "IPv6",
106 }
107
108 // camelCaseName returns correct name identifier (camelCase).
109 func camelCaseName(name string) (should string) {
110         name = strings.Title(name)
111
112         // Fast path for simple cases: "_" and all lowercase.
113         if name == "_" {
114                 return name
115         }
116         allLower := true
117         for _, r := range name {
118                 if !unicode.IsLower(r) {
119                         allLower = false
120                         break
121                 }
122         }
123         if allLower {
124                 return name
125         }
126
127         // Split camelCase at any lower->upper transition, and split on underscores.
128         // Check each word for common initialisms.
129         runes := []rune(name)
130         w, i := 0, 0 // index of start of word, scan
131         for i+1 <= len(runes) {
132                 eow := false // whether we hit the end of a word
133                 if i+1 == len(runes) {
134                         eow = true
135                 } else if runes[i+1] == '_' {
136                         // underscore; shift the remainder forward over any run of underscores
137                         eow = true
138                         n := 1
139                         for i+n+1 < len(runes) && runes[i+n+1] == '_' {
140                                 n++
141                         }
142
143                         // Leave at most one underscore if the underscore is between two digits
144                         if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
145                                 n--
146                         }
147
148                         copy(runes[i+1:], runes[i+n+1:])
149                         runes = runes[:len(runes)-n]
150                 } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
151                         // lower->non-lower
152                         eow = true
153                 }
154                 i++
155                 if !eow {
156                         continue
157                 }
158
159                 // [w,i) is a word.
160                 word := string(runes[w:i])
161                 if u := usesInitialism(word); u != "" {
162                         // Keep consistent case, which is lowercase only at the start.
163                         if w == 0 && unicode.IsLower(runes[w]) {
164                                 u = strings.ToLower(u)
165                         }
166                         // All the common initialisms are ASCII,
167                         // so we can replace the bytes exactly.
168                         copy(runes[w:], []rune(u))
169                 } else if w > 0 && strings.ToLower(word) == word {
170                         // already all lowercase, and not the first word, so uppercase the first character.
171                         runes[w] = unicode.ToUpper(runes[w])
172                 }
173                 w = i
174         }
175         return string(runes)
176 }