Skip to content

Commit

Permalink
Add marker protocols for /scale, /status and /eviction
Browse files Browse the repository at this point in the history
  • Loading branch information
iabudiab committed Apr 11, 2021
1 parent d06032f commit 2b48b08
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Sources/SwiftkubeModelGen/Model/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class Resource: Decodable, Comparable {
var isReplaceableResource: Bool = false
var isDeletableResource: Bool = false
var isCollectionDeletableResource: Bool = false
var isScalableResource: Bool = false
var isEvictableResource: Bool = false
var hasStatus: Bool = false

enum CodingKeys: String, CodingKey {
case type
Expand Down
24 changes: 24 additions & 0 deletions Sources/SwiftkubeModelGen/OpenAPIProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ class OpenAPIProcessor {
let spec = try JSONSerialization.jsonObject(with: data) as! [String: Any]
let paths = spec["paths"] as! [String: [String: Any]]

var subResourcePaths = [String]()
var resourceMap = [String: Resource]()

for (path, definition) in paths {
if let _ = SubResources.first(where: { path.hasSuffix($0) }) {
subResourcePaths.append(path)
continue
}

Expand Down Expand Up @@ -70,6 +74,26 @@ class OpenAPIProcessor {
apiResource.isNamespaced = true
ResourceScope[apiResource.gvk!] = true
}

resourceMap[path] = apiResource
}

for path in subResourcePaths {
let lastSlash = path.lastIndex(of: "/")!
let subResource = path.suffix(from: lastSlash)
let parentResourcePath = path.dropLast("/{name}".count + subResource.count)
let parentResource = resourceMap[String(parentResourcePath)]!

switch subResource {
case "/eviction":
parentResource.isEvictableResource = true
case "/scale":
parentResource.isScalableResource = true
case "/status":
parentResource.hasStatus = true
default:
continue
}
}
}

Expand Down
22 changes: 19 additions & 3 deletions Sources/SwiftkubeModelGen/Stencil+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,28 @@ extension Extension {
if schema.isCollectionDeletableResource {
verbsProtocols.append("CollectionDeletableResource")
}
var additionalProtocols: [String] = []
if schema.isEvictableResource {
additionalProtocols.append("EvictableResource")
}
if schema.isScalableResource {
additionalProtocols.append("ScalableResource")
}
if schema.hasStatus {
additionalProtocols.append("StatusHavingResource")
}

let renderedMain = mainProtocols.joined(separator: ", ")
let renderedVerbs = verbsProtocols.joined(separator: ", ")
let renderedAdditional = additionalProtocols.joined(separator: ", ")

if verbsProtocols.isEmpty {
return mainProtocols.joined(separator: ", ")
if verbsProtocols.isEmpty && additionalProtocols.isEmpty {
return renderedMain + " {"
} else if additionalProtocols.isEmpty {
return renderedMain + ",\n\t\t" + renderedVerbs + "\n\t{"
}

return mainProtocols.joined(separator: ", ") + ",\n\t\t" + verbsProtocols.joined(separator: ", ")
return renderedMain + ",\n\t\t" + renderedVerbs + ",\n\t\t" + renderedAdditional + "\n\t{"
}

registerFilter("P.renderDescription") { input in
Expand Down
11 changes: 9 additions & 2 deletions Sources/SwiftkubeModelGen/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,19 @@ extension String {
}

func cleanupWhitespace() -> String {
let regex = try! NSRegularExpression(pattern: #"^\n\s*\n"#, options: .anchorsMatchLines)
return regex.stringByReplacingMatches(
let doubleLines = try! NSRegularExpression(pattern: #"^\n\s*\n"#, options: .anchorsMatchLines)
let cleanedDoubleLines = doubleLines.stringByReplacingMatches(
in: self,
range: NSRange(startIndex..., in: self),
withTemplate: ""
)

let docWhitespace = try! NSRegularExpression(pattern: #"(^\s*///) $"#, options: .anchorsMatchLines)
return docWhitespace.stringByReplacingMatches(
in: cleanedDoubleLines,
range: NSRange(cleanedDoubleLines.startIndex..., in: cleanedDoubleLines),
withTemplate: "$1"
)
}

func capitalizingFirstLetter() -> String {
Expand Down
12 changes: 5 additions & 7 deletions templates/model/Resource.swift.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Foundation
public extension {{ type.group }}.{{ type.version }} {

{{ resource|R.renderDescription }}
struct {{ type.kind }}: {{ resource|R.protocols }} {
struct {{ type.kind }}: {{ resource|R.protocols }}
{% if resource.isListResource %}

///
Expand Down Expand Up @@ -57,8 +57,8 @@ public extension {{ type.group }}.{{ type.version }} {
self.{{ property.name|P.escapeKeywords }} = {{ property.name|P.escapeKeywords }}{% endfor %}
}
}
}
{% if resource.properties.count > 0 %}
}{% if resource.properties.count > 0 %}

///
/// Codable conformance
///
Expand All @@ -83,9 +83,8 @@ public extension {{ type.group }}.{{ type.version }}.{{ type.kind }} {
{% for property in resource.properties %}
try container.encode({{ property.name|P.escapeKeywords }}, forKey: .{{ property.name|P.escapeKeywords }}){% endfor %}
}
}{% endif %}{% if resource.isListResource %}

}{% endif %}
{% if resource.isListResource %}
// MARK: - {{ type.group }}.{{ type.version }}.{{ type.kind }} + Sequence

///
Expand All @@ -98,5 +97,4 @@ extension {{ type.group }}.{{ type.version }}.{{ type.kind }}: Sequence {
public func makeIterator() -> AnyIterator<{{ type.group }}.{{ type.version }}.{{ type.listItemKind }}> {
AnyIterator(items.makeIterator())
}
}
{% endif %}
}{% endif %}

0 comments on commit 2b48b08

Please sign in to comment.