Skip to content

Commit

Permalink
Update template for AnyKubernetesAPIResource
Browse files Browse the repository at this point in the history
  • Loading branch information
iabudiab committed Nov 7, 2020
1 parent 80bc3d6 commit 9d67f69
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions templates/model/AnyKubernetesAPIResource.swift.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

import Foundation

///
/// A type-erased `KubernetesAPIResource` that wraps the actual resource instance.
///
public struct AnyKubernetesAPIResource: KubernetesAPIResource {

private enum CodingKeys: String, CodingKey {
Expand All @@ -45,8 +48,24 @@ public struct AnyKubernetesAPIResource: KubernetesAPIResource {

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let apiVersionString = try container.decode(String.self, forKey: .apiVersion)
let kindString = try container.decode(String.self, forKey: .kind)

let decodedAPIVersion: String?
if container.contains(.apiVersion) {
decodedAPIVersion = try container.decode(String.self, forKey: .apiVersion)
} else {
decodedAPIVersion = decoder.userInfo[CodingUserInfoKey.apiVersion] as? String
}

let decodedKind: String?
if container.contains(.kind) {
decodedKind = try container.decode(String.self, forKey: .kind)
} else {
decodedKind = decoder.userInfo[CodingUserInfoKey.kind] as? String
}

guard let apiVersionString = decodedAPIVersion, let kindString = decodedKind else {
throw SwiftkubeModelError.decodingError("Couldn't decode apiVersion and/or kind at: \(container.codingPath)")
}

let gvk = GroupVersionKind(rawValue: "\(apiVersionString)/\(kindString)")

Expand Down

0 comments on commit 9d67f69

Please sign in to comment.