Skip to content

Commit

Permalink
Merge pull request #107 from keefertaylor/annots
Browse files Browse the repository at this point in the history
Implement annotations on MichelsonParameters
  • Loading branch information
keefertaylor authored Jul 30, 2019
2 parents c15dcb8 + d9f0330 commit 97a8891
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 26 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ env:
before_install:
- gem install slather
- gem install cocoapods
- brew update
- brew outdated carthage || brew upgrade carthage
- travis_wait 120 carthage bootstrap --platform iOS --no-use-binaries
before_deploy:
- travis_wait 120 carthage build --no-skip-current --platform iOS --cache-builds
Expand Down
16 changes: 16 additions & 0 deletions Tests/TezosKit/MichelsonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ final class MichelsonTests: XCTestCase {
XCTAssertEqual(encoded, Helpers.orderJSONString(expected))
}

func testAnnotation() {
guard let annotation = MichelsonAnnotation(annotation: "@tezoskit") else {
XCTFail()
return
}
let michelson = PairMichelsonParameter(
left: MichelsonTests.michelsonInt,
right: MichelsonTests.michelsonString,
annotations: [ annotation ]
)

let expected = "{\"prim\": \"Pair\", \"args\": [ \(MichelsonTests.expectedMichelsonIntEncoding), \(MichelsonTests.expectedMichelsonStringEncoding) ], \"annots\": [\"\(annotation.value)\"] }"
let encoded = JSONUtils.jsonString(for: michelson.networkRepresentation)
XCTAssertEqual(encoded, Helpers.orderJSONString(expected))
}

// MARK: - Dexter / Token Contract

func testTransferTokens() {
Expand Down
10 changes: 8 additions & 2 deletions TezosKit/Michelson/AbstractMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ public class AbstractMichelsonParameter: MichelsonParameter {
public let networkRepresentation: [String: Any]

/// - Parameter networkRepresentation: A dictionary representation of the parameter which can be encoded to JSON.
public init(networkRepresentation: [String: Any]) {
self.networkRepresentation = networkRepresentation
/// - Parameter annotations: Optional annotations
public init(networkRepresentation: [String: Any], annotations: [MichelsonAnnotation]? = nil) {
var annotationAugmentedDictionary = networkRepresentation
if let annotations = annotations {
annotationAugmentedDictionary[MichelineConstants.annotations] = annotations.map { $0.value }
}

self.networkRepresentation = annotationAugmentedDictionary
}
}
4 changes: 2 additions & 2 deletions TezosKit/Michelson/BoolMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Foundation

/// A representation of a boolean parameter in Michelson.
public class BoolMichelsonParameter: AbstractMichelsonParameter {
public init(bool: Bool) {
public init(bool: Bool, annotations: [MichelsonAnnotation]? = nil) {
let stringRep = bool ? MichelineConstants.true : MichelineConstants.false
super.init(networkRepresentation: [MichelineConstants.primitive: stringRep ])
super.init(networkRepresentation: [MichelineConstants.primitive: stringRep ], annotations: annotations)
}
}
8 changes: 4 additions & 4 deletions TezosKit/Michelson/BytesMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import Foundation

/// A representation of a bytes parameter in Michelson.
public class BytesMichelsonParameter: AbstractMichelsonParameter {
public convenience init?(bytes: [UInt8]) {
public convenience init?(bytes: [UInt8], annotations: [MichelsonAnnotation]? = nil) {
guard let hex = CodingUtil.binToHex(bytes) else {
return nil
}
self.init(hex: hex)
self.init(hex: hex, annotations: annotations)
}

public init(hex: Hex) {
super.init(networkRepresentation: [ MichelineConstants.bytes: hex ])
public init(hex: Hex, annotations: [MichelsonAnnotation]? = nil) {
super.init(networkRepresentation: [ MichelineConstants.bytes: hex ], annotations: annotations)
}
}
4 changes: 2 additions & 2 deletions TezosKit/Michelson/IntMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation

/// A representation of an integer parameter in Michelson.
public class IntMichelsonParameter: AbstractMichelsonParameter {
public init(int: Int) {
super.init(networkRepresentation: [MichelineConstants.int: "\(int)"])
public init(int: Int, annotations: [MichelsonAnnotation]? = nil) {
super.init(networkRepresentation: [MichelineConstants.int: "\(int)"], annotations: annotations)
}
}
5 changes: 3 additions & 2 deletions TezosKit/Michelson/LeftMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import Foundation

/// A representation of a left parameter in Michelson.
public class LeftMichelsonParameter: AbstractMichelsonParameter {
public init(arg: MichelsonParameter) {
public init(arg: MichelsonParameter, annotations: [MichelsonAnnotation]? = nil) {
let argArray = [ arg.networkRepresentation ]
super.init(
networkRepresentation: [
MichelineConstants.primitive: MichelineConstants.left,
MichelineConstants.args: argArray
]
],
annotations: annotations
)
}
}
7 changes: 5 additions & 2 deletions TezosKit/Michelson/NoneMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import Foundation

/// A representation of a none param in Michelson.
public class NoneMichelsonParameter: AbstractMichelsonParameter {
public init() {
super.init(networkRepresentation: [ MichelineConstants.primitive: MichelineConstants.none ])
public init(annotations: [MichelsonAnnotation]? = nil) {
super.init(
networkRepresentation: [ MichelineConstants.primitive: MichelineConstants.none ],
annotations: annotations
)
}
}
5 changes: 3 additions & 2 deletions TezosKit/Michelson/PairMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import Foundation

// A representation of a pair parameter in Michelson.
public class PairMichelsonParameter: AbstractMichelsonParameter {
public init(left: MichelsonParameter, right: MichelsonParameter) {
public init(left: MichelsonParameter, right: MichelsonParameter, annotations: [MichelsonAnnotation]? = nil) {
let argArray = [ left.networkRepresentation, right.networkRepresentation ]
super.init(
networkRepresentation: [
MichelineConstants.primitive: MichelineConstants.pair,
MichelineConstants.args: argArray
]
],
annotations: annotations
)
}
}
5 changes: 3 additions & 2 deletions TezosKit/Michelson/RightMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import Foundation

// A representation of a right parameter in Michelson.
public class RightMichelsonParameter: AbstractMichelsonParameter {
public init(arg: MichelsonParameter) {
public init(arg: MichelsonParameter, annotations: [MichelsonAnnotation]? = nil) {
let argArray = [ arg.networkRepresentation ]
super.init(
networkRepresentation: [
MichelineConstants.primitive: MichelineConstants.right,
MichelineConstants.args: argArray
]
],
annotations: annotations
)
}
}
5 changes: 3 additions & 2 deletions TezosKit/Michelson/SomeMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import Foundation

// A representation of a some parameter in Michelson.
public class SomeMichelsonParameter: AbstractMichelsonParameter {
public init(some: MichelsonParameter) {
public init(some: MichelsonParameter, annotations: [MichelsonAnnotation]? = nil) {
super.init(
networkRepresentation: [
MichelineConstants.primitive: MichelineConstants.some,
MichelineConstants.args: [
some.networkRepresentation
]
]
],
annotations: annotations
)
}
}
4 changes: 2 additions & 2 deletions TezosKit/Michelson/StringMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation

/// A representation of a string parameter in Michelson.
public class StringMichelsonParameter: AbstractMichelsonParameter {
public init(string: String) {
super.init(networkRepresentation: [MichelineConstants.string: string])
public init(string: String, annotations: [MichelsonAnnotation]? = nil) {
super.init(networkRepresentation: [MichelineConstants.string: string], annotations: annotations)
}
}
7 changes: 5 additions & 2 deletions TezosKit/Michelson/UnitMichelsonParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import Foundation

/// A representation of an unit parameter in Michelson.
public class UnitMichelsonParameter: AbstractMichelsonParameter {
public init() {
super.init(networkRepresentation: [ MichelineConstants.primitive: MichelineConstants.unit ])
public init(annotations: [MichelsonAnnotation]? = nil) {
super.init(
networkRepresentation: [ MichelineConstants.primitive: MichelineConstants.unit ],
annotations: annotations
)
}
}

0 comments on commit 97a8891

Please sign in to comment.