Code Monkey home page Code Monkey logo

Comments (4)

LebJe avatar LebJe commented on August 26, 2024

@stackotter, I've pushed an implementation to strictDecoding. Can you try it and see if it works for you?

let decoder = TOMLDecoder(strictDecoding: true)

struct A: Decodable {
    let a: String
    let b: B

    struct B: Decodable {
        let c: Double
    }
}

let toml = """
a = "abc"

[b]
c = 25.353
d = "def"
f = [1, 2, 3]
"""

do {
    let a = try decoder.decode(A.self, from: toml)
} catch let error as UnexpectedKeysError {
    print("\(error.keys) were not decoded")
}

from tomlkit.

stackotter avatar stackotter commented on August 26, 2024

Hi @LebJe, the code snippet you have provided does seem to work correctly. However, when I tried to enable strict decoding in swift-bundler, I seem to have run into an issue. Here's the input toml:

description = "A simple 'Hello, World!' SwiftUI app."
platforms = ["macOS"]
minimum_swift_version = "5.5"

And here's the code I'm using to decode that snippet.

import Foundation
import TOMLKit
import Version

/// The contents of a template's manifest file.
struct TemplateManifest: Codable {
  /// A short description of the package.
  var description: String
  /// The list of supported platforms.
  var platforms: [String]
  /// The minimum Swift version required to use the template.
  var minimumSwiftVersion: Version
  /// The system dependencies required by this template (keyed by the user-facing dependency name).
  var systemDependencies: [String: SystemDependency]?

  private enum CodingKeys: String, CodingKey {
    case description
    case platforms
    case minimumSwiftVersion = "minimum_swift_version"
    case systemDependencies = "system_dependencies"
  }

  /// Loads a template's manifest file.
  /// - Parameters:
  ///   - file: The manifest file to load.
  ///   - template: The name of the template that the manifest is for.
  /// - Returns: The loaded manifest, or a failure if the file could not be read or decoded.
  static func load(from file: URL, template: String) -> Result<TemplateManifest, TemplaterError> {
    let contents: String
    do {
      contents = try String.init(contentsOf: file)
    } catch {
      return .failure(.failedToReadTemplateManifest(template: template, manifest: file, error))
    }

    let manifest: TemplateManifest
    do {
      var decoder = TOMLDecoder(strictDecoding: true)

      // Set the Version decoding method to tolerant
      decoder.userInfo[.decodingMethod] = DecodingMethod.tolerant

      manifest = try decoder.decode(TemplateManifest.self, from: contents)
    } catch {
      return .failure(.failedToDecodeTemplateManifest(template: template, manifest: file, error))
    }

    return .success(manifest)
  }
}

Without specifying strictDecoding: true, the TemplateManifest toml successfully decodes, and the minimum_swift_version key can be read. However, with strictDecoding enabled, I get the following error: TOMLKit.UnexpectedKeysError(keys: ["minimum_swift_version"]).

from tomlkit.

LebJe avatar LebJe commented on August 26, 2024

@stackotter, sorry for the long delay. I have fixed the issue you posted, and changed UnexpectedKeysError’s keys property from an Array of keys, to a Dictionary whose keys are the un-decoded TOML keys, and whose values are the CodingKey’s that leads to the TOML key.
If this works for you, I'll create a new release.

from tomlkit.

stackotter avatar stackotter commented on August 26, 2024

Thanks! This fixes the issue I was having :)

Sorry that I took a while to get around to testing this

from tomlkit.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.