Swiftpack.co - finestructure/ValueCodable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by finestructure.
finestructure/ValueCodable 0.2.0
A Value struct to decode arbitrary data
⭐️ 6
🕓 18 weeks ago
.package(url: "https://github.com/finestructure/ValueCodable.git", from: "0.2.0")

ValueCodable

Build Status

ValueCodable is a swift library to decode data of unknown structure in a type safe way.

For instance, say you have a yml file with the following content

name: foo
data:
    obj:
        a: 1
        b: two
    list:
        - 1
        - two
        - foo: bar

where the format of data is unknown beforehand. It is not straightforward to write a decoder for the arbitrary data structure.

Whith ValueCodable, the decoding is quite simple:

let s = """
    name: foo
    data:
        obj:
            a: 1
            b: two
        list:
            - 1
            - two
            - foo: bar
    """

struct Test: Decodable {
    let name: String
    // capture the unstructure data as "Value"
    let data: Value
}

let t = try YAMLDecoder().decode(Test.self, from: s)

// access "data" via subscript
print(t.data["obj"])       // Optional(["a": 1, "b": "two"])
print(t.data["list"])      // Optional([1, "two", ["foo": "bar"]])
print(t.data["list"]?[0])  // Optional(1)

// act on the decoded types
switch t.data["list"]?[2] {
case let .some(.dictionary(dict)):
    print(dict)            // ["foo": "bar"]
default:
    print("unhandled type")
}

// access propertyies via key paths
print(t.data["obj.b"])         // Optional("two")
print(t.data["list[0]"])       // Optional(1)
print(t.data["list[2].foo"])   // Optional("bar")

Supported types

Value currently supports the following types:

public enum Value: Equatable {
    case bool(Bool)
    case int(Int)
    case string(String)
    case double(Double)
    case dictionary([Key: Value])
    case array([Value])
    case null
}

When decoding, ValueCodable will attempt to decode into the most suitable type.

GitHub

link
Stars: 6
Last commit: 18 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

4 years ago
  • Xcode 11.1 / Swift 5.1 (macOS)
  • Swift 5.1.1 (Linux)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics