ModelField

public struct ModelField<T> : Decodable where T : ModelFieldProtocol

An abstraction for Internet Archive-style metadata fields.

Internet Archive metadata fields can be stored as strings or an array of strings. Typically we want to use these fields in a native types (Int, Double, Date, URL, etc). The ModelField struct does a few things to make handling these values more convenient:

  • Provides a generic interface to any native type that is parsable from a string
  • Converts the fields from strings to their native type
  • Normalizes the response to an array of objects
  • Provides a convenience value accessor to get the first value of the array since most fields are single values

Native types are wrapped in ModelFieldProtocol objects like IAInt and IADate to handle the string to native conversion.

Example Usage:

struct Foo: Decodable {
  let foo: ModelField<IAInt>
  let bar: ModelField<IAString>
}
let json: String = "{ \"foo\": \"3\", \"bar\": [\"boop\", \"bop\"] }"
let data = json.data(using: .utf8)!
let results: Foo = try! JSONDecoder().decode(Foo.self, from: data)
results.foo.values => [3]
results.foo.value => 3
results.bar.values => ["boop", "bop"]
results.bar.value => "boop"
  • A convenience accessor for the first value of the values array

    Declaration

    Swift

    public var value: T.FieldType? { get }
  • An array of values of type T

    Declaration

    Swift

    public var values: [T.FieldType]
  • Declaration

    Swift

    public init(from decoder: Decoder) throws