|
| 1 | +// |
| 2 | +// CadenceTypeTest |
| 3 | +// |
| 4 | +// Copyright 2022 Outblock Pty Ltd |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +import BigInt |
| 20 | +import Foundation |
| 21 | + |
| 22 | +public protocol FlowEncodable { |
| 23 | + func toFlowArgument() -> Flow.Argument? |
| 24 | +} |
| 25 | + |
| 26 | +extension Int: FlowEncodable { |
| 27 | + public func toFlowArgument() -> Flow.Argument? { |
| 28 | + return .init(type: .int, value: .int(self)) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +extension String: FlowEncodable { |
| 33 | + public func toFlowArgument() -> Flow.Argument? { |
| 34 | + return .init(type: .string, value: .string(self)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +extension Bool: FlowEncodable { |
| 39 | + public func toFlowArgument() -> Flow.Argument? { |
| 40 | + return .init(type: .bool, value: .bool(self)) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +extension Double: FlowEncodable { |
| 45 | + public func toFlowArgument() -> Flow.Argument? { |
| 46 | + return .init(type: .ufix64, value: .ufix64(Decimal(self))) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +extension Decimal: FlowEncodable { |
| 51 | + public func toFlowArgument() -> Flow.Argument? { |
| 52 | + return .init(type: .ufix64, value: .ufix64(self)) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +extension Int8: FlowEncodable { |
| 57 | + public func toFlowArgument() -> Flow.Argument? { |
| 58 | + return .init(type: .int8, value: .int8(self)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +extension UInt8: FlowEncodable { |
| 63 | + public func toFlowArgument() -> Flow.Argument? { |
| 64 | + return .init(type: .uint8, value: .uint8(self)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +extension Int16: FlowEncodable { |
| 69 | + public func toFlowArgument() -> Flow.Argument? { |
| 70 | + return .init(type: .int16, value: .int16(self)) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +extension UInt16: FlowEncodable { |
| 75 | + public func toFlowArgument() -> Flow.Argument? { |
| 76 | + return .init(type: .uint16, value: .uint16(self)) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +extension Int32: FlowEncodable { |
| 81 | + public func toFlowArgument() -> Flow.Argument? { |
| 82 | + return .init(type: .int32, value: .int32(self)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension UInt32: FlowEncodable { |
| 87 | + public func toFlowArgument() -> Flow.Argument? { |
| 88 | + return .init(type: .uint32, value: .uint32(self)) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +extension Int64: FlowEncodable { |
| 93 | + public func toFlowArgument() -> Flow.Argument? { |
| 94 | + return .init(type: .int64, value: .int64(self)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +extension UInt64: FlowEncodable { |
| 99 | + public func toFlowArgument() -> Flow.Argument? { |
| 100 | + return .init(type: .uint64, value: .uint64(self)) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +extension BigInt: FlowEncodable { |
| 105 | + public func toFlowArgument() -> Flow.Argument? { |
| 106 | + return .init(type: .int128, value: .int128(self)) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +extension BigUInt: FlowEncodable { |
| 111 | + public func toFlowArgument() -> Flow.Argument? { |
| 112 | + return .init(type: .uint128, value: .uint128(self)) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +extension Array: FlowEncodable where Element: FlowEncodable { |
| 117 | + public func toFlowArgument() -> Flow.Argument? { |
| 118 | + let arguments = compactMap { $0.toFlowArgument() } |
| 119 | + return .init(type: .array, value: .array(arguments.toValue())) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +extension Optional: FlowEncodable where Wrapped: FlowEncodable { |
| 124 | + public func toFlowArgument() -> Flow.Argument? { |
| 125 | + switch self { |
| 126 | + case .none: |
| 127 | + return .init(type: .optional, value: .optional(nil)) |
| 128 | + case .some(let value): |
| 129 | + return .init(type: .optional, value: .optional(value.toFlowArgument()?.value)) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +extension Dictionary: FlowEncodable where Key: FlowEncodable, Value: FlowEncodable { |
| 135 | + public func toFlowArgument() -> Flow.Argument? { |
| 136 | + let entries = compactMap { key, value -> Flow.Argument.Dictionary? in |
| 137 | + guard let keyArg = key.toFlowArgument(), |
| 138 | + let valueArg = value.toFlowArgument() else { |
| 139 | + return nil |
| 140 | + } |
| 141 | + return Flow.Argument.Dictionary(key: keyArg.value, value: valueArg.value) |
| 142 | + } |
| 143 | + return .init(type: .dictionary, value: .dictionary(entries)) |
| 144 | + } |
| 145 | +} |
0 commit comments