Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding error for Mappings with enumerations as keys #44

Open
jcubit opened this issue Feb 18, 2025 · 0 comments
Open

Decoding error for Mappings with enumerations as keys #44

jcubit opened this issue Feb 18, 2025 · 0 comments

Comments

@jcubit
Copy link
Contributor

jcubit commented Feb 18, 2025

It is currently not possible to parse a Pkl module in Swift which has a Mapping property with enum as keys. Example: consider the following Pkl module

// ApplicationConfig.pkl
module ApplicationConfig

typealias Environment = "dev"|"prod"|"qa"

config: Mapping<Environment, String>
// iscConfig.pkl
amends "ApplicationConfig.pkl"

config {
  ["dev"] = "Imaginary Service Company (ISC) configuration"
}

The swift generated type is

// Code generated from Pkl module `ApplicationConfig`. DO NOT EDIT.
import PklSwift

public enum ApplicationConfig {}

extension ApplicationConfig {
    public enum Environment: String, CaseIterable, Decodable, Hashable {
        case dev = "dev"
        case prod = "prod"
        case qa = "qa"
    }

    public struct Module: PklRegisteredType, Decodable, Hashable {
        public static let registeredIdentifier: String = "ApplicationConfig"

        public var config: [Environment: String]

        public init(config: [Environment: String]) {
            self.config = config
        }
    }

    /// Load the Pkl module at the given source and evaluate it into `ApplicationConfig.Module`.
    ///
    /// - Parameter source: The source of the Pkl module.
    public static func loadFrom(source: ModuleSource) async throws -> ApplicationConfig.Module {
        try await PklSwift.withEvaluator { evaluator in
            try await loadFrom(evaluator: evaluator, source: source)
        }
    }

    /// Load the Pkl module at the given source and evaluate it with the given evaluator into
    /// `ApplicationConfig.Module`.
    ///
    /// - Parameter evaluator: The evaluator to use for evaluation.
    /// - Parameter source: The module to evaluate.
    public static func loadFrom(
        evaluator: PklSwift.Evaluator,
        source: PklSwift.ModuleSource
    ) async throws -> ApplicationConfig.Module {
        try await evaluator.evaluateModule(source: source, as: Module.self)
    }
}

Now if we try to parse this in Swift via

import PklSwift

let filepath = ...

let appConfig = try await ApplicationConfig.loadFrom(source: .path(filepath))

we get the error:

Error: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected either list, listing, or set, but got mapping", underlyingError: nil))

The exception seems to be thrown from _PklDecoder in PklUnkeyedDecodingContainer.swift.

I’m using Pkl 0.27.2 and pkl-gen-swift 0.4.1

Thank you in advance for having a look at this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant