Remove custom Pair type (#21)

This commit is contained in:
Philip K.F. Hölzenspies 2024-05-03 17:31:40 +02:00 committed by GitHub
parent 2bcb25f0b7
commit b23fd2369f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 7 additions and 79 deletions

View File

@ -1,45 +0,0 @@
// ===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ===----------------------------------------------------------------------===//
import Foundation
/// Pair is the Swift representation of Pkl's `pkl.Pair`.
public struct Pair<A, B>: Hashable where A: Hashable, B: Hashable {
public init(_ first: A, _ second: B) {
self.first = first
self.second = second
}
/// The value of the first component of this ``Pair``.
let first: A
/// The value of the second component of this ``Pair``.
let second: B
}
extension Pair: CustomStringConvertible where A: CustomStringConvertible, B: CustomStringConvertible {
public var description: String {
"Pair(\(self.first.description), \(self.second.description))"
}
}
extension Pair: Decodable where A: Decodable, B: Decodable {
public init(from decoder: Decoder) throws {
var partsDecoder = try decoder.unkeyedContainer()
self.first = try partsDecoder.decode(A.self)
self.second = try partsDecoder.decode(B.self)
}
}

View File

@ -49,11 +49,6 @@ extension _PklDecoder {
throw error("Expected an array at slot 2, but got \(value[1].debugDataTypeDescription)")
}
self.members = members
} else if value.count == 3 {
guard pklType == .pair else {
throw error("Expected to find a Pair type marker, but found \(pklType)")
}
self.members = Array(value[1...2])
} else {
throw error("Expected 2 or 3 values, but found \(value.count)")
}

View File

@ -1,3 +1,2 @@
res1: Pair<Int, String> = Pair(42, "Hello")
res2: Duration = 10.h
res3: DataSize = 1.2345.gib
res1: Duration = 10.h
res2: DataSize = 1.2345.gib

View File

@ -7,16 +7,13 @@ extension ApiTypes {
public struct Module: PklRegisteredType, Decodable, Hashable {
public static let registeredIdentifier: String = "ApiTypes"
public var res1: Pair<Int, String>
public var res1: Duration
public var res2: Duration
public var res2: DataSize
public var res3: DataSize
public init(res1: Pair<Int, String>, res2: Duration, res3: DataSize) {
public init(res1: Duration, res2: DataSize) {
self.res1 = res1
self.res2 = res2
self.res3 = res3
}
}

View File

@ -77,9 +77,8 @@ class FixturesTest: XCTestCase {
XCTAssertEqual(
result,
ApiTypes.Module(
res1: Pair(42, "Hello"),
res2: .hours(10),
res3: .gibibytes(1.2345)
res1: .hours(10),
res2: .gibibytes(1.2345)
)
)
}

View File

@ -144,15 +144,5 @@ mappedHigherOrderTypes: Mapping<Class|TypeAlias, (reflect.DeclaredType, reflect.
generateType(typeArg, enclosing, seenMappings)
)
}
[Pair] = (type, enclosing, seenMappings) -> new Type.Declared {
typeName = "Pair"
swiftModuleName = "Pkl"
local typeArgKey = type.typeArguments.getOrNull(0)
local typeArgValue = type.typeArguments.getOrNull(1)
typeArguments = List(
if (typeArgKey == null) anyType else generateType(typeArgKey, enclosing, seenMappings),
if (typeArgValue == null) anyType else generateType(typeArgValue, enclosing, seenMappings)
)
}
}

View File

@ -53,19 +53,12 @@ local class Nullables {
local reflectedNullables = reflect.Class(Nullables)
local class Pairs {
res1: Pair<String, String>
res2: Pair<String, String?>
}
local class Nothing {
res1: nothing
}
local nothingType: reflect.Type = reflect.Class(Nothing).properties["res1"].type
local reflectedPairs = reflect.Class(Pairs)
local mod = reflect.Module(module).moduleClass
local function generateType(typ: reflect.Type) = typegen.generateType(typ, mod, List()).render("")