Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package util |
| 18 | |
| 19 | import "strings" |
| 20 | |
| 21 | // ToCanonicalName converts Golang package/type name into canonical OpenAPI name. |
| 22 | // Examples: |
| 23 | // Input: k8s.io/api/core/v1.Pod |
| 24 | // Output: io.k8s.api.core.v1.Pod |
| 25 | // |
| 26 | // Input: k8s.io/api/core/v1 |
| 27 | // Output: io.k8s.api.core.v1 |
| 28 | func ToCanonicalName(name string) string { |
| 29 | nameParts := strings.Split(name, "/") |
| 30 | // Reverse first part. e.g., io.k8s... instead of k8s.io... |
| 31 | if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") { |
| 32 | parts := strings.Split(nameParts[0], ".") |
| 33 | for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 { |
| 34 | parts[i], parts[j] = parts[j], parts[i] |
| 35 | } |
| 36 | nameParts[0] = strings.Join(parts, ".") |
| 37 | } |
| 38 | return strings.Join(nameParts, ".") |
| 39 | } |