-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GVR extensions template for instantiating with a resource name
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
templates/model/GroupVersionResource+ResourceName.swift.stencil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// Copyright 2020 Swiftkube Project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
/// | ||
/// Generated by Swiftkube:ModelGen | ||
/// Kubernetes {{meta.modelVersion}} | ||
/// | ||
|
||
import Foundation | ||
|
||
extension GroupVersionResource { | ||
|
||
/// Creates a new instance of `GroupVersionResource` for the provided resource name. | ||
/// | ||
/// A resource name can be: | ||
/// - Lower-cased singular resource kind | ||
/// - Lower-cased plural resource name | ||
/// - Lower-cased short resource name | ||
/// | ||
/// ```swift | ||
/// let gvk = GroupVersionResource(for: "deployment") | ||
/// let gvk = GroupVersionResource(for: "deployments") | ||
/// let gvk = GroupVersionResource(for: "deploy") | ||
/// ``` | ||
/// | ||
/// - Parameter resourceName: The resource plural or singular name or its kind. | ||
public init?(for resourceName: String) { | ||
if let gvk = GroupVersionResource.tryKind(resourceName) { | ||
self = gvk | ||
} else if let gvk = GroupVersionResource.tryPlural(resourceName) { | ||
self = gvk | ||
} else if let gvk = GroupVersionResource.tryShort(resourceName) { | ||
self = gvk | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
private static func tryKind(_ kind: String) -> GroupVersionResource? { | ||
switch kind.lowercased() { | ||
{% for gvk in newestGroupVersionKinds %} | ||
case "{{ gvk|GVK.kind|lowercase }}": | ||
return GroupVersionResource(group: "{{ gvk|GVK.group }}", version: "{{ gvk|GVK.version }}", resource: "{{ gvk|GVK.plural }}"){% endfor %} | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
private static func tryPlural(_ plural: String) -> GroupVersionResource? { | ||
switch plural { | ||
{% for gvk in pluralGroupVersionKinds %} | ||
case "{{ gvk|GVK.plural }}": | ||
return GroupVersionResource(group: "{{ gvk|GVK.group }}", version: "{{ gvk|GVK.version }}", resource: "{{ gvk|GVK.plural }}"){% endfor %} | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
private static func tryShort(_ short: String) -> GroupVersionResource? { | ||
switch short { | ||
{% for gvk in shortGroupVersionKinds %} | ||
case {{ gvk|GVK.short }}: | ||
return GroupVersionResource(group: "{{ gvk|GVK.group }}", version: "{{ gvk|GVK.version }}", resource: "{{ gvk|GVK.plural }}"){% endfor %} | ||
default: | ||
return nil | ||
} | ||
} | ||
} |