Skip to content

Commit

Permalink
Add GVR extensions template for instantiating with a resource name
Browse files Browse the repository at this point in the history
  • Loading branch information
iabudiab committed Jun 5, 2022
1 parent fad1525 commit 61c7a3c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/SwiftkubeModelGen/ModelGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct ModelGen: ParsableCommand {
RenderTemplate(environment: environment, template: GroupVersionKindResourceNameTemplate()),
RenderTemplate(environment: environment, template: GroupVersionKindDefaultResourcesTemplate()),
RenderTemplate(environment: environment, template: GroupVersionKindAPIResourceTemplate()),
RenderTemplate(environment: environment, template: GroupVersionResourceResourceNameTemplate()),
RenderTemplate(environment: environment, template: GroupVersionResourceAPIResourceTemplate()),
RenderTemplate(environment: environment, template: GroupVersionResourceDefaultResourcesTemplate()),
RenderTemplate(environment: environment, template: AnyKubernetesAPIResourceTemplate()),
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftkubeModelGen/Template/Templates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ struct GroupVersionKindAPIResourceTemplate: TemplateType {
}
}

struct GroupVersionResourceResourceNameTemplate: TemplateType {

let stencilTemplate = "GroupVersionResource+ResourceName.swift.stencil"

func destination(basePath: Path) -> Path {
basePath + Path("GroupVersionResource+ResourceName.swift")
}
}

struct GroupVersionResourceAPIResourceTemplate: TemplateType {

let stencilTemplate = "GroupVersionResource+KubernetesAPIResource.swift.stencil"
Expand Down
81 changes: 81 additions & 0 deletions templates/model/GroupVersionResource+ResourceName.swift.stencil
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
}
}
}

0 comments on commit 61c7a3c

Please sign in to comment.