From d893c0dc4e14649b479376153be263b3374b50e1 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Wed, 8 Jun 2022 13:52:21 +0500 Subject: [PATCH 01/12] PKG -- [types] add new AnyStruct type --- packages/types/src/types.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index 8109bf649..97b6098df 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -746,3 +746,24 @@ export const Path = type( }, v => v ) + +export const AnyStruct = type( + "AnyStruct", + (v) => { + if (isNumber(v) || isInteger(v) || isString(v) || isBoolean(v)) + return { + type: "AnyStruct", + value: v.toString(), + } + else if (isArray(v)) { + return { + type: "AnyStruct", + value: isArray(children) + ? children.map((c, i) => c.asArgument(v[i])) + : v.map((x) => children.asArgument(x)), + } + } + throwTypeError("Expected AnyStruct") + }, + (v) => v +) From 3d5b5d837bf728f2edad51565f87c360dbfa7dc0 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Wed, 8 Jun 2022 14:11:18 +0500 Subject: [PATCH 02/12] PKG -- [types] add integer validation for AnyStruct and call numberValuesDeprecationNotice with Integer as type --- packages/types/src/types.js | 58 +++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index 97b6098df..543d52214 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -600,7 +600,7 @@ export const _Array = (children = []) => v => v ) -export {_Array as Array} +export { _Array as Array } export const Dictionary = (children = []) => type( @@ -611,15 +611,15 @@ export const Dictionary = (children = []) => type: "Dictionary", value: isArray(children) ? children.map((c, i) => ({ - key: c.key.asArgument(v[i].key), - value: c.value.asArgument(v[i].value), - })) + key: c.key.asArgument(v[i].key), + value: c.value.asArgument(v[i].value), + })) : isArray(v) - ? v.map(x => ({ + ? v.map(x => ({ key: children.key.asArgument(x.key), value: children.value.asArgument(x.value), })) - : [ + : [ { key: children.key.asArgument(v.key), value: children.value.asArgument(v.value), @@ -642,13 +642,13 @@ export const Event = (id, fields = []) => id: id, fields: isArray(fields) ? fields.map((c, i) => ({ - name: v.fields[i].name, - value: c.value.asArgument(v.fields[i].value), - })) + name: v.fields[i].name, + value: c.value.asArgument(v.fields[i].value), + })) : v.fields.map(x => ({ - name: x.name, - value: fields.value.asArgument(x.value), - })), + name: x.name, + value: fields.value.asArgument(x.value), + })), }, } throwTypeError("Expected Object for type Event") @@ -667,13 +667,13 @@ export const Resource = (id, fields = []) => id: id, fields: isArray(fields) ? fields.map((c, i) => ({ - name: v.fields[i].name, - value: c.value.asArgument(v.fields[i].value), - })) + name: v.fields[i].name, + value: c.value.asArgument(v.fields[i].value), + })) : v.fields.map(x => ({ - name: x.name, - value: fields.value.asArgument(x.value), - })), + name: x.name, + value: fields.value.asArgument(x.value), + })), }, } throwTypeError("Expected Object for type Resource") @@ -692,13 +692,13 @@ export const Struct = (id, fields = []) => id: id, fields: isArray(fields) ? fields.map((c, i) => ({ - name: v.fields[i].name, - value: c.value.asArgument(v.fields[i].value), - })) + name: v.fields[i].name, + value: c.value.asArgument(v.fields[i].value), + })) : v.fields.map(x => ({ - name: x.name, - value: fields.value.asArgument(x.value), - })), + name: x.name, + value: fields.value.asArgument(x.value), + })), }, } throwTypeError("Expected Object for type Struct") @@ -750,18 +750,12 @@ export const Path = type( export const AnyStruct = type( "AnyStruct", (v) => { - if (isNumber(v) || isInteger(v) || isString(v) || isBoolean(v)) + if (isNumber(v) || isInteger(v)) { + numberValuesDeprecationNotice("Integers") return { type: "AnyStruct", value: v.toString(), } - else if (isArray(v)) { - return { - type: "AnyStruct", - value: isArray(children) - ? children.map((c, i) => c.asArgument(v[i])) - : v.map((x) => children.asArgument(x)), - } } throwTypeError("Expected AnyStruct") }, From d11945e45b74b0a4ed63d86c0b1507de46bae67c Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Wed, 8 Jun 2022 14:23:27 +0500 Subject: [PATCH 03/12] PKG -- [types] add [U]Fix64 validation for AnyStruct and call UFix64AndFix64NumberDeprecationNotice --- packages/types/src/types.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index 543d52214..140a43042 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -750,13 +750,35 @@ export const Path = type( export const AnyStruct = type( "AnyStruct", (v) => { - if (isNumber(v) || isInteger(v)) { - numberValuesDeprecationNotice("Integers") + if (isNumber(v)) { + if (isInteger(v)) + numberValuesDeprecationNotice("Integer") + else + UFix64AndFix64NumberDeprecationNotice() + return { type: "AnyStruct", value: v.toString(), } } + if (isString(v)) { + const vParts = v.split(".") + if (vParts.length !== 2) { + throwTypeError( + `Expected one decimal but found ${vParts.length} in the [U]Fix64 value. Find out more about [U]Fix64 types here: https://docs.onflow.org/cadence/json-cadence-spec/#fixed-point-numbers` + ) + } + if (vParts[1].length == 0 || vParts[1].length > 8) { + throwTypeError( + `Expected at least one digit, and at most 8 digits following the decimal of the [U]Fix64 value but found ${vParts[1].length} digits. Find out more about [U]Fix64 types here: https://docs.onflow.org/cadence/json-cadence-spec/#fixed-point-numbers` + ) + } + + return { + type: "AnyStruct", + value: v, + } + } throwTypeError("Expected AnyStruct") }, (v) => v From 277f4155dbf7ffda9b0d4e00d7d0eacacd748374 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Wed, 8 Jun 2022 14:52:28 +0500 Subject: [PATCH 04/12] PKG -- [types] add test-cases for AnyStruct to check all Integers type values --- packages/types/src/types.js | 25 +- packages/types/src/types.test.js | 425 ++++++++++++++++--------------- 2 files changed, 230 insertions(+), 220 deletions(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index 140a43042..b5982f02f 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -762,22 +762,25 @@ export const AnyStruct = type( } } if (isString(v)) { - const vParts = v.split(".") - if (vParts.length !== 2) { - throwTypeError( - `Expected one decimal but found ${vParts.length} in the [U]Fix64 value. Find out more about [U]Fix64 types here: https://docs.onflow.org/cadence/json-cadence-spec/#fixed-point-numbers` - ) - } - if (vParts[1].length == 0 || vParts[1].length > 8) { - throwTypeError( - `Expected at least one digit, and at most 8 digits following the decimal of the [U]Fix64 value but found ${vParts[1].length} digits. Find out more about [U]Fix64 types here: https://docs.onflow.org/cadence/json-cadence-spec/#fixed-point-numbers` - ) - } + if (v.includes(".")) { + const vParts = v.split(".") + if (vParts.length !== 2) { + throwTypeError( + `Expected one decimal but found ${vParts.length} in the [U]Fix64 value. Find out more about [U]Fix64 types here: https://docs.onflow.org/cadence/json-cadence-spec/#fixed-point-numbers` + ) + } + if (vParts[1].length == 0 || vParts[1].length > 8) { + throwTypeError( + `Expected at least one digit, and at most 8 digits following the decimal of the [U]Fix64 value but found ${vParts[1].length} digits. Find out more about [U]Fix64 types here: https://docs.onflow.org/cadence/json-cadence-spec/#fixed-point-numbers` + ) + } + } return { type: "AnyStruct", value: v, } + } throwTypeError("Expected AnyStruct") }, diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index ccea19f30..eda0b9e5f 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -1,228 +1,235 @@ import * as t from "./types.js" -;[ - [t.Identity, 0, 0, 0], - [t.Identity, "a", "a", "a"], - [t.Identity, null, null, null], - [t.Int, "1", {type: "Int", value: "1"}, "1"], - [t.UInt, "1", {type: "UInt", value: "1"}, "1"], - [t.Int8, "8", {type: "Int8", value: "8"}, "8"], - [t.UInt8, "8", {type: "UInt8", value: "8"}, "8"], - [t.Int16, "16", {type: "Int16", value: "16"}, "16"], - [t.UInt16, "16", {type: "UInt16", value: "16"}, "16"], - [t.Int32, "32", {type: "Int32", value: "32"}, "32"], - [t.UInt32, "32", {type: "UInt32", value: "32"}, "32"], - [t.Int64, "64", {type: "Int64", value: "64"}, "64"], - [t.UInt64, "64", {type: "UInt64", value: "64"}, "64"], - [t.Int128, "128", {type: "Int128", value: "128"}, "128"], - [t.UInt128, "128", {type: "UInt128", value: "128"}, "128"], - [t.Int256, "256", {type: "Int256", value: "256"}, "256"], - [t.UInt256, "256", {type: "UInt256", value: "256"}, "256"], - [t.Word8, "8", {type: "Word8", value: "8"}, "8"], - [t.Word16, "16", {type: "Word16", value: "16"}, "16"], - [t.Word32, "32", {type: "Word32", value: "32"}, "32"], - [t.Word64, "64", {type: "Word64", value: "64"}, "64"], - [t.UFix64, "64", {type: "UFix64", value: "64"}, "64", true], - [t.Fix64, "64", {type: "Fix64", value: "64"}, "64", true], - [ - t.UFix64, - "64.000000001", - {type: "UFix64", value: "64.000000001"}, - "64.000000001", - true, - ], - [ - t.Fix64, - "64.000000001", - {type: "Fix64", value: "64.000000001"}, - "64.000000001", - true, - ], - [t.UFix64, "64.0", {type: "UFix64", value: "64.0"}, "64.0", false], - [t.Fix64, "64.0", {type: "Fix64", value: "64.0"}, "64.0", false], - [ - t.String, - "Go with the Flow", - {type: "String", value: "Go with the Flow"}, - "Go with the Flow", - ], - [t.Character, "c", {type: "Character", value: "c"}, "c"], - [t.Bool, true, {type: "Bool", value: true}, true], - [t.Address, "0x1", {type: "Address", value: "0x1"}, "0x1"], - [t.Void, null, {type: "Void"}, null], - [t.Optional(t.String), null, {type: "Optional", value: null}, null], - [ - t.Optional(t.String), - "test", - {type: "Optional", value: {type: "String", value: "test"}}, - "test", - ], - [ - t.Reference, - {address: "0x01", type: "0x01.CryptoKitty"}, - {type: "Reference", value: {address: "0x01", type: "0x01.CryptoKitty"}}, - {address: "0x01", type: "0x01.CryptoKitty"}, - ], - [ - t.Array(t.String), - ["test"], - {type: "Array", value: [{type: "String", value: "test"}]}, - ["test"], - ], - [ - t.Array([t.String, t.String]), - ["test1", "test2"], - { - type: "Array", - value: [ - {type: "String", value: "test1"}, - {type: "String", value: "test2"}, - ], - }, - ["test1", "test2"], - ], - [ - t.Dictionary([ - {key: t.Int, value: t.String}, - {key: t.Int, value: t.String}, - ]), + ;[ + [t.Identity, 0, 0, 0], + [t.Identity, "a", "a", "a"], + [t.Identity, null, null, null], + [t.Int, "1", { type: "Int", value: "1" }, "1"], + [t.UInt, "1", { type: "UInt", value: "1" }, "1"], + [t.Int8, "8", { type: "Int8", value: "8" }, "8"], + [t.UInt8, "8", { type: "UInt8", value: "8" }, "8"], + [t.Int16, "16", { type: "Int16", value: "16" }, "16"], + [t.UInt16, "16", { type: "UInt16", value: "16" }, "16"], + [t.Int32, "32", { type: "Int32", value: "32" }, "32"], + [t.UInt32, "32", { type: "UInt32", value: "32" }, "32"], + [t.Int64, "64", { type: "Int64", value: "64" }, "64"], + [t.UInt64, "64", { type: "UInt64", value: "64" }, "64"], + [t.Int128, "128", { type: "Int128", value: "128" }, "128"], + [t.UInt128, "128", { type: "UInt128", value: "128" }, "128"], + [t.Int256, "256", { type: "Int256", value: "256" }, "256"], + [t.UInt256, "256", { type: "UInt256", value: "256" }, "256"], + [t.Word8, "8", { type: "Word8", value: "8" }, "8"], + [t.Word16, "16", { type: "Word16", value: "16" }, "16"], + [t.Word32, "32", { type: "Word32", value: "32" }, "32"], + [t.Word64, "64", { type: "Word64", value: "64" }, "64"], + [t.UFix64, "64", { type: "UFix64", value: "64" }, "64", true], + [t.Fix64, "64", { type: "Fix64", value: "64" }, "64", true], [ - {key: "1", value: "one"}, - {key: "2", value: "two"}, + t.UFix64, + "64.000000001", + { type: "UFix64", value: "64.000000001" }, + "64.000000001", + true, ], - { - type: "Dictionary", - value: [ - {key: {type: "Int", value: "1"}, value: {type: "String", value: "one"}}, - {key: {type: "Int", value: "2"}, value: {type: "String", value: "two"}}, - ], - }, [ - {key: "1", value: "one"}, - {key: "2", value: "two"}, + t.Fix64, + "64.000000001", + { type: "Fix64", value: "64.000000001" }, + "64.000000001", + true, ], - ], - [ - t.Dictionary({key: t.Int, value: t.String}), - {key: "1", value: "one"}, - { - type: "Dictionary", - value: [ - {key: {type: "Int", value: "1"}, value: {type: "String", value: "one"}}, - ], - }, - {key: "1", value: "one"}, - ], - [ - t.Struct("0x01.Jeffysaur", [{value: t.String}]), - { - fields: [{name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur"}], - }, - { - type: "Struct", - value: { - id: "0x01.Jeffysaur", - fields: [ - { - name: "Jeffysaur_Name", - value: {type: "String", value: "Mr Jeff The Dinosaur"}, - }, + [t.UFix64, "64.0", { type: "UFix64", value: "64.0" }, "64.0", false], + [t.Fix64, "64.0", { type: "Fix64", value: "64.0" }, "64.0", false], + [ + t.String, + "Go with the Flow", + { type: "String", value: "Go with the Flow" }, + "Go with the Flow", + ], + [t.Character, "c", { type: "Character", value: "c" }, "c"], + [t.Bool, true, { type: "Bool", value: true }, true], + [t.Address, "0x1", { type: "Address", value: "0x1" }, "0x1"], + [t.Void, null, { type: "Void" }, null], + [t.Optional(t.String), null, { type: "Optional", value: null }, null], + [ + t.Optional(t.String), + "test", + { type: "Optional", value: { type: "String", value: "test" } }, + "test", + ], + [ + t.Reference, + { address: "0x01", type: "0x01.CryptoKitty" }, + { type: "Reference", value: { address: "0x01", type: "0x01.CryptoKitty" } }, + { address: "0x01", type: "0x01.CryptoKitty" }, + ], + [ + t.Array(t.String), + ["test"], + { type: "Array", value: [{ type: "String", value: "test" }] }, + ["test"], + ], + [ + t.Array([t.String, t.String]), + ["test1", "test2"], + { + type: "Array", + value: [ + { type: "String", value: "test1" }, + { type: "String", value: "test2" }, ], }, - }, - { - fields: [{name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur"}], - }, - ], - [ - t.Event("0x01.JeffWroteSomeJS", [{value: t.String}]), - { - fields: [{name: "wasTheCodeClean?", value: "absolutely"}], - }, - { - type: "Event", - value: { - id: "0x01.JeffWroteSomeJS", - fields: [ - { - name: "wasTheCodeClean?", - value: {type: "String", value: "absolutely"}, - }, + ["test1", "test2"], + ], + [ + t.Dictionary([ + { key: t.Int, value: t.String }, + { key: t.Int, value: t.String }, + ]), + [ + { key: "1", value: "one" }, + { key: "2", value: "two" }, + ], + { + type: "Dictionary", + value: [ + { key: { type: "Int", value: "1" }, value: { type: "String", value: "one" } }, + { key: { type: "Int", value: "2" }, value: { type: "String", value: "two" } }, ], }, - }, - { - fields: [{name: "wasTheCodeClean?", value: "absolutely"}], - }, - ], - [ - t.Resource("0x01.Jeffysaur", [{value: t.String}]), - { - fields: [{name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur"}], - }, - { - type: "Resource", - value: { - id: "0x01.Jeffysaur", - fields: [ - { - name: "Jeffysaur_Name", - value: {type: "String", value: "Mr Jeff The Dinosaur"}, - }, + [ + { key: "1", value: "one" }, + { key: "2", value: "two" }, + ], + ], + [ + t.Dictionary({ key: t.Int, value: t.String }), + { key: "1", value: "one" }, + { + type: "Dictionary", + value: [ + { key: { type: "Int", value: "1" }, value: { type: "String", value: "one" } }, ], }, - }, - { - fields: [{name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur"}], - }, - ], - [ - t.Path, - { - domain: "public", - identifier: "flowTokenVault", - }, - { - type: "Path", - value: { + { key: "1", value: "one" }, + ], + [ + t.Struct("0x01.Jeffysaur", [{ value: t.String }]), + { + fields: [{ name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur" }], + }, + { + type: "Struct", + value: { + id: "0x01.Jeffysaur", + fields: [ + { + name: "Jeffysaur_Name", + value: { type: "String", value: "Mr Jeff The Dinosaur" }, + }, + ], + }, + }, + { + fields: [{ name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur" }], + }, + ], + [ + t.Event("0x01.JeffWroteSomeJS", [{ value: t.String }]), + { + fields: [{ name: "wasTheCodeClean?", value: "absolutely" }], + }, + { + type: "Event", + value: { + id: "0x01.JeffWroteSomeJS", + fields: [ + { + name: "wasTheCodeClean?", + value: { type: "String", value: "absolutely" }, + }, + ], + }, + }, + { + fields: [{ name: "wasTheCodeClean?", value: "absolutely" }], + }, + ], + [ + t.Resource("0x01.Jeffysaur", [{ value: t.String }]), + { + fields: [{ name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur" }], + }, + { + type: "Resource", + value: { + id: "0x01.Jeffysaur", + fields: [ + { + name: "Jeffysaur_Name", + value: { type: "String", value: "Mr Jeff The Dinosaur" }, + }, + ], + }, + }, + { + fields: [{ name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur" }], + }, + ], + [ + t.Path, + { domain: "public", identifier: "flowTokenVault", }, - }, - { - domain: "public", - identifier: "flowTokenVault", - }, - ], - [ - t.Path, - { - domain: "notValidDomain", - identifier: "flowTokenVault", - }, - { - type: "Path", - value: { + { + type: "Path", + value: { + domain: "public", + identifier: "flowTokenVault", + }, + }, + { + domain: "public", + identifier: "flowTokenVault", + }, + ], + [ + t.Path, + { domain: "notValidDomain", identifier: "flowTokenVault", }, - }, - { - domain: "notValidDomain", - identifier: "flowTokenVault", - }, - true, - ], -].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { - describe(cast.label, () => { - test(`t.${cast.label}.asArgument(${input})`, () => { - if (shouldError) { - expect(() => cast.asArgument(input)).toThrow() - } else { - expect(cast.asArgument(input)).toStrictEqual(asArgument) - } - }) - test(`t.${cast.label}.asInjection(${input})`, () => { - expect(cast.asInjection(input)).toStrictEqual(asInjection) + { + type: "Path", + value: { + domain: "notValidDomain", + identifier: "flowTokenVault", + }, + }, + { + domain: "notValidDomain", + identifier: "flowTokenVault", + }, + true, + ], + [t.AnyStruct, "1", { type: "AnyStruct", value: "1" }, "1"], + [t.AnyStruct, "8", { type: "AnyStruct", value: "8" }, "8"], + [t.AnyStruct, "16", { type: "AnyStruct", value: "16" }, "16"], + [t.AnyStruct, "32", { type: "AnyStruct", value: "32" }, "32"], + [t.AnyStruct, "64", { type: "AnyStruct", value: "64" }, "64"], + [t.AnyStruct, "128", { type: "AnyStruct", value: "128" }, "128"], + [t.AnyStruct, "256", { type: "AnyStruct", value: "256" }, "256"] + ].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { + describe(cast.label, () => { + test(`t.${cast.label}.asArgument(${input})`, () => { + if (shouldError) { + expect(() => cast.asArgument(input)).toThrow() + } else { + expect(cast.asArgument(input)).toStrictEqual(asArgument) + } + }) + test(`t.${cast.label}.asInjection(${input})`, () => { + expect(cast.asInjection(input)).toStrictEqual(asInjection) + }) }) }) -}) From c521dcb874adfec60acf1acd65efa4f23282c503 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Wed, 8 Jun 2022 18:55:40 +0500 Subject: [PATCH 05/12] PKG -- [types] add test-cases for AnyStruct to check UFix64 & Fix64 type values --- packages/types/src/types.test.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index eda0b9e5f..8a9232930 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -218,7 +218,24 @@ import * as t from "./types.js" [t.AnyStruct, "32", { type: "AnyStruct", value: "32" }, "32"], [t.AnyStruct, "64", { type: "AnyStruct", value: "64" }, "64"], [t.AnyStruct, "128", { type: "AnyStruct", value: "128" }, "128"], - [t.AnyStruct, "256", { type: "AnyStruct", value: "256" }, "256"] + [t.AnyStruct, "256", { type: "AnyStruct", value: "256" }, "256"], + [ + t.AnyStruct, + "64.000000001", + { type: "AnyStruct", value: "64.000000001" }, + "64.000000001", + true, + ], + [ + t.AnyStruct, + "64.000000001", + { type: "AnyStruct", value: "64.000000001" }, + "64.000000001", + true, + ], + [t.AnyStruct, "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], + [t.AnyStruct, "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], + ].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { describe(cast.label, () => { test(`t.${cast.label}.asArgument(${input})`, () => { From 456a8309d3618107f1b7e9a405ab8ad2dbe64144 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Wed, 8 Jun 2022 19:07:26 +0500 Subject: [PATCH 06/12] PKG -- [types] add test-cases for AnyStruct to check String & charcter type values --- packages/types/src/types.test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index 8a9232930..d4bb4201e 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -235,7 +235,13 @@ import * as t from "./types.js" ], [t.AnyStruct, "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], [t.AnyStruct, "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], - + [ + t.AnyStruct, + "Go with the Flow", + { type: "AnyStruct", value: "Go with the Flow" }, + "Go with the Flow", + ], + [t.AnyStruct, "c", { type: "AnyStruct", value: "c" }, "c"], ].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { describe(cast.label, () => { test(`t.${cast.label}.asArgument(${input})`, () => { From 77e7399d8bfb14c74f2fa782e94e88b362faf1be Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Wed, 8 Jun 2022 19:24:05 +0500 Subject: [PATCH 07/12] PKG -- [types] add test-cases for AnyStruct to check Boolean & Address type values --- packages/types/src/types.js | 7 ++++++- packages/types/src/types.test.js | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index b5982f02f..684f196fd 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -780,8 +780,13 @@ export const AnyStruct = type( type: "AnyStruct", value: v, } - } + if (isBoolean(v)) + return { + type: "AnyStruct", + value: v, + } + throwTypeError("Expected AnyStruct") }, (v) => v diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index d4bb4201e..987c2977b 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -242,6 +242,9 @@ import * as t from "./types.js" "Go with the Flow", ], [t.AnyStruct, "c", { type: "AnyStruct", value: "c" }, "c"], + [t.AnyStruct, true, { type: "AnyStruct", value: true }, true], + [t.AnyStruct, "0x1", { type: "AnyStruct", value: "0x1" }, "0x1"], + ].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { describe(cast.label, () => { test(`t.${cast.label}.asArgument(${input})`, () => { From 46763d0b95406fd41d2336f72613799db7eda39c Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Mon, 13 Jun 2022 18:43:01 +0500 Subject: [PATCH 08/12] PKG -- [types] add test-cases for AnyStruct to check Array type values (string) --- packages/types/src/types.js | 12 +++++++- packages/types/src/types.test.js | 49 ++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index 684f196fd..898a18b6d 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -747,9 +747,10 @@ export const Path = type( v => v ) -export const AnyStruct = type( +export const AnyStruct = (children = []) => type( "AnyStruct", (v) => { + if (isNumber(v)) { if (isInteger(v)) numberValuesDeprecationNotice("Integer") @@ -787,6 +788,15 @@ export const AnyStruct = type( value: v, } + if (isArray(v)) { + return { + type: "AnyStruct", + value: isArray(children) + ? children.map((c, i) => c.asArgument(v[i])) + : v.map(x => children.asArgument(x)), + } + } + throwTypeError("Expected AnyStruct") }, (v) => v diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index 987c2977b..0fc0fa407 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -212,38 +212,57 @@ import * as t from "./types.js" }, true, ], - [t.AnyStruct, "1", { type: "AnyStruct", value: "1" }, "1"], - [t.AnyStruct, "8", { type: "AnyStruct", value: "8" }, "8"], - [t.AnyStruct, "16", { type: "AnyStruct", value: "16" }, "16"], - [t.AnyStruct, "32", { type: "AnyStruct", value: "32" }, "32"], - [t.AnyStruct, "64", { type: "AnyStruct", value: "64" }, "64"], - [t.AnyStruct, "128", { type: "AnyStruct", value: "128" }, "128"], - [t.AnyStruct, "256", { type: "AnyStruct", value: "256" }, "256"], + [t.AnyStruct(), "1", { type: "AnyStruct", value: "1" }, "1"], + [t.AnyStruct(), "8", { type: "AnyStruct", value: "8" }, "8"], + [t.AnyStruct(), "16", { type: "AnyStruct", value: "16" }, "16"], + [t.AnyStruct(), "32", { type: "AnyStruct", value: "32" }, "32"], + [t.AnyStruct(), "64", { type: "AnyStruct", value: "64" }, "64"], + [t.AnyStruct(), "128", { type: "AnyStruct", value: "128" }, "128"], + [t.AnyStruct(), "256", { type: "AnyStruct", value: "256" }, "256"], [ - t.AnyStruct, + t.AnyStruct(), "64.000000001", { type: "AnyStruct", value: "64.000000001" }, "64.000000001", true, ], [ - t.AnyStruct, + t.AnyStruct(), "64.000000001", { type: "AnyStruct", value: "64.000000001" }, "64.000000001", true, ], - [t.AnyStruct, "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], - [t.AnyStruct, "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], + [t.AnyStruct(), "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], + [t.AnyStruct(), "64.0", { type: "AnyStruct", value: "64.0" }, "64.0", false], [ - t.AnyStruct, + t.AnyStruct(), "Go with the Flow", { type: "AnyStruct", value: "Go with the Flow" }, "Go with the Flow", ], - [t.AnyStruct, "c", { type: "AnyStruct", value: "c" }, "c"], - [t.AnyStruct, true, { type: "AnyStruct", value: true }, true], - [t.AnyStruct, "0x1", { type: "AnyStruct", value: "0x1" }, "0x1"], + [t.AnyStruct(), "c", { type: "AnyStruct", value: "c" }, "c"], + [t.AnyStruct(), true, { type: "AnyStruct", value: true }, true], + [t.AnyStruct(), "0x1", { type: "AnyStruct", value: "0x1" }, "0x1"], + [ + t.AnyStruct(t.String), + ["test"], + { type: "AnyStruct", value: [{ type: "String", value: "test" }] }, + ["test"], + ], + [ + t.AnyStruct([t.String, t.String]), + ["test1", "test2"], + { + type: "AnyStruct", + value: [ + { type: "String", value: "test1" }, + { type: "String", value: "test2" }, + ], + }, + ["test1", "test2"], + ], + ].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { describe(cast.label, () => { From 524e329a8b7cfdd71704d7ba4ac0006483afc14a Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Tue, 14 Jun 2022 14:07:08 +0500 Subject: [PATCH 09/12] PKG -- [types] add test-cases for AnyStruct to check Dictionary type values --- packages/types/src/types.js | 33 +++++++++++++++++--- packages/types/src/types.test.js | 52 ++++++++++++++++++++++++++------ 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index 898a18b6d..3209fa97c 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -788,14 +788,37 @@ export const AnyStruct = (children = []) => type( value: v, } - if (isArray(v)) { + if (isObj(v)) { return { type: "AnyStruct", value: isArray(children) - ? children.map((c, i) => c.asArgument(v[i])) - : v.map(x => children.asArgument(x)), - } - } + ? + children.map((c, i) => ({ + key: c.key.asArgument(v[i].key), + value: c.value.asArgument(v[i].value), + })) + : isArray(v) + ? v.map(x => ({ + key: children.key.asArgument(x.key), + value: children.value.asArgument(x.value), + })) + : [ + { + key: children.key.asArgument(v.key), + value: children.value.asArgument(v.value), + }, + ], + } + } + + // if (isArray(v)) { + // return { + // type: "AnyStruct", + // value: isArray(children) + // ? children.map((c, i) => c.asArgument(v[i])) + // : v.map(x => children.asArgument(x)), + // } + // } throwTypeError("Expected AnyStruct") }, diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index 0fc0fa407..2df8a7510 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -244,26 +244,58 @@ import * as t from "./types.js" [t.AnyStruct(), "c", { type: "AnyStruct", value: "c" }, "c"], [t.AnyStruct(), true, { type: "AnyStruct", value: true }, true], [t.AnyStruct(), "0x1", { type: "AnyStruct", value: "0x1" }, "0x1"], + // [ + // t.AnyStruct(t.String), + // ["test"], + // { type: "AnyStruct", value: [{ type: "String", value: "test" }] }, + // ["test"], + // ], + // [ + // t.AnyStruct([t.String, t.UFix64]), + // ["test1", "64.00000001"], + // { + // type: "AnyStruct", + // value: [ + // { type: "String", value: "test1" }, + // { type: "UFix64", value: "64.00000001" }, + // ], + // }, + // ["test1", "64.00000001"], + // ], + [ - t.AnyStruct(t.String), - ["test"], - { type: "AnyStruct", value: [{ type: "String", value: "test" }] }, - ["test"], + t.AnyStruct([ + { key: t.Int, value: t.String }, + { key: t.Int, value: t.String }, + ]), + [ + { key: "1", value: "one" }, + { key: "2", value: "two" }, + ], + { + type: "AnyStruct", + value: [ + { key: { type: "Int", value: "1" }, value: { type: "String", value: "one" } }, + { key: { type: "Int", value: "2" }, value: { type: "String", value: "two" } }, + ], + }, + [ + { key: "1", value: "one" }, + { key: "2", value: "two" }, + ], ], [ - t.AnyStruct([t.String, t.String]), - ["test1", "test2"], + t.AnyStruct({ key: t.Int, value: t.String }), + { key: "1", value: "one" }, { type: "AnyStruct", value: [ - { type: "String", value: "test1" }, - { type: "String", value: "test2" }, + { key: { type: "Int", value: "1" }, value: { type: "String", value: "one" } }, ], }, - ["test1", "test2"], + { key: "1", value: "one" }, ], - ].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { describe(cast.label, () => { test(`t.${cast.label}.asArgument(${input})`, () => { From 113429dc7b20ef340aeff22fc7ec3f7a46df1914 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Tue, 14 Jun 2022 18:33:36 +0500 Subject: [PATCH 10/12] PKG -- [types] add test-cases for AnyStruct to check Array and Dictionary type values --- packages/types/src/types.js | 52 +++++++++++++++++++++++++++----- packages/types/src/types.test.js | 37 +++++++++++------------ 2 files changed, 62 insertions(+), 27 deletions(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index 3209fa97c..c32e28eef 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -793,15 +793,24 @@ export const AnyStruct = (children = []) => type( type: "AnyStruct", value: isArray(children) ? - children.map((c, i) => ({ - key: c.key.asArgument(v[i].key), - value: c.value.asArgument(v[i].value), - })) + children.map((c, i) => ( + + isObj(v[i]) ? + { + key: c.key.asArgument(v[i].key), + value: c.value.asArgument(v[i].value), + } : + c.asArgument(v[i]) + )) : isArray(v) - ? v.map(x => ({ - key: children.key.asArgument(x.key), - value: children.value.asArgument(x.value), - })) + ? v.map(x => ( + isObj(x) && console.log("x = ", x) ? + { + key: children.key.asArgument(x.key), + value: children.value.asArgument(x.value), + } : + children.asArgument(x) + )) : [ { key: children.key.asArgument(v.key), @@ -811,7 +820,34 @@ export const AnyStruct = (children = []) => type( } } + + // if (isObj(v)) { + // console.log("I am dictionary = ", v) + // return { + // type: "AnyStruct", + // value: isArray(children) + // ? + // children.map((c, i) => ({ + // key: c.key.asArgument(v[i].key), + // value: c.value.asArgument(v[i].value), + // })) + // : isArray(v) + // ? v.map(x => ({ + // key: children.key.asArgument(x.key), + // value: children.value.asArgument(x.value), + // })) + // : [ + // { + // key: children.key.asArgument(v.key), + // value: children.value.asArgument(v.value), + // }, + // ], + // } + // } + // if (isArray(v)) { + // console.log("I am an Array = ", v) + // return { // type: "AnyStruct", // value: isArray(children) diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index 2df8a7510..03671d7ac 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -244,25 +244,24 @@ import * as t from "./types.js" [t.AnyStruct(), "c", { type: "AnyStruct", value: "c" }, "c"], [t.AnyStruct(), true, { type: "AnyStruct", value: true }, true], [t.AnyStruct(), "0x1", { type: "AnyStruct", value: "0x1" }, "0x1"], - // [ - // t.AnyStruct(t.String), - // ["test"], - // { type: "AnyStruct", value: [{ type: "String", value: "test" }] }, - // ["test"], - // ], - // [ - // t.AnyStruct([t.String, t.UFix64]), - // ["test1", "64.00000001"], - // { - // type: "AnyStruct", - // value: [ - // { type: "String", value: "test1" }, - // { type: "UFix64", value: "64.00000001" }, - // ], - // }, - // ["test1", "64.00000001"], - // ], - + [ + t.AnyStruct(t.String), + ["test"], + { type: "AnyStruct", value: [{ type: "String", value: "test" }] }, + ["test"], + ], + [ + t.AnyStruct([t.String, t.UFix64]), + ["test1", "64.00000001"], + { + type: "AnyStruct", + value: [ + { type: "String", value: "test1" }, + { type: "UFix64", value: "64.00000001" }, + ], + }, + ["test1", "64.00000001"], + ], [ t.AnyStruct([ { key: t.Int, value: t.String }, From 4c42b0dcf366a440ff72e7b4e034a4b8bffb3cc3 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Tue, 14 Jun 2022 20:09:23 +0500 Subject: [PATCH 11/12] PKG -- [types] add test-cases for AnyStruct to check Path types --- packages/types/src/types.js | 91 +++++++++++++++++++------------- packages/types/src/types.test.js | 59 ++++++++++++++++++++- 2 files changed, 113 insertions(+), 37 deletions(-) diff --git a/packages/types/src/types.js b/packages/types/src/types.js index c32e28eef..ad71d9ed5 100644 --- a/packages/types/src/types.js +++ b/packages/types/src/types.js @@ -747,7 +747,7 @@ export const Path = type( v => v ) -export const AnyStruct = (children = []) => type( +export const AnyStruct = (children = [], id = null, fields = []) => type( "AnyStruct", (v) => { @@ -789,6 +789,59 @@ export const AnyStruct = (children = []) => type( } if (isObj(v)) { + + if (v.domain && v.identifier) { + if (!isString(v.domain)) { + throwTypeError( + `Expected a string for the Path domain but found ${v.domain}. Find out more about the Path type here: https://docs.onflow.org/cadence/json-cadence-spec/#path` + ) + } + + if ( + !( + v.domain === "storage" || + v.domain === "private" || + v.domain === "public" + ) + ) { + throwTypeError( + `Expected either "storage", "private" or "public" as the Path domain but found ${v.domain}. Find out more about the Path type here: https://docs.onflow.org/cadence/json-cadence-spec/#path` + ) + } + + if (!isString(v.identifier)) { + throwTypeError( + `Expected a string for the Path identifier but found ${v.identifier}. Find out more about the Path type here: https://docs.onflow.org/cadence/json-cadence-spec/#path` + ) + } + + return { + type: "AnyStruct", + value: { + domain: v.domain, + identifier: v.identifier, + }, + } + + } + + if (id) + return { + type: "AnyStruct", + value: { + id: id, + fields: isArray(fields) + ? fields.map((c, i) => ({ + name: v.fields[i].name, + value: c.value.asArgument(v.fields[i].value), + })) + : v.fields.map(x => ({ + name: x.name, + value: fields.value.asArgument(x.value), + })), + }, + } + return { type: "AnyStruct", value: isArray(children) @@ -804,7 +857,7 @@ export const AnyStruct = (children = []) => type( )) : isArray(v) ? v.map(x => ( - isObj(x) && console.log("x = ", x) ? + isObj(x) ? { key: children.key.asArgument(x.key), value: children.value.asArgument(x.value), @@ -821,40 +874,6 @@ export const AnyStruct = (children = []) => type( } - // if (isObj(v)) { - // console.log("I am dictionary = ", v) - // return { - // type: "AnyStruct", - // value: isArray(children) - // ? - // children.map((c, i) => ({ - // key: c.key.asArgument(v[i].key), - // value: c.value.asArgument(v[i].value), - // })) - // : isArray(v) - // ? v.map(x => ({ - // key: children.key.asArgument(x.key), - // value: children.value.asArgument(x.value), - // })) - // : [ - // { - // key: children.key.asArgument(v.key), - // value: children.value.asArgument(v.value), - // }, - // ], - // } - // } - - // if (isArray(v)) { - // console.log("I am an Array = ", v) - - // return { - // type: "AnyStruct", - // value: isArray(children) - // ? children.map((c, i) => c.asArgument(v[i])) - // : v.map(x => children.asArgument(x)), - // } - // } throwTypeError("Expected AnyStruct") }, diff --git a/packages/types/src/types.test.js b/packages/types/src/types.test.js index 03671d7ac..04a1abd54 100644 --- a/packages/types/src/types.test.js +++ b/packages/types/src/types.test.js @@ -294,7 +294,64 @@ import * as t from "./types.js" }, { key: "1", value: "one" }, ], - + [ + t.AnyStruct([], "0x01.Jeffysaur", [{ value: t.String }]), + { + fields: [{ name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur" }], + }, + { + type: "AnyStruct", + value: { + id: "0x01.Jeffysaur", + fields: [ + { + name: "Jeffysaur_Name", + value: { type: "String", value: "Mr Jeff The Dinosaur" }, + }, + ], + }, + }, + { + fields: [{ name: "Jeffysaur_Name", value: "Mr Jeff The Dinosaur" }], + }, + ], + [ + t.AnyStruct(), + { + domain: "public", + identifier: "flowTokenVault", + }, + { + type: "AnyStruct", + value: { + domain: "public", + identifier: "flowTokenVault", + }, + }, + { + domain: "public", + identifier: "flowTokenVault", + }, + ], + [ + t.AnyStruct(), + { + domain: "notValidDomain", + identifier: "flowTokenVault", + }, + { + type: "AnyStruct", + value: { + domain: "notValidDomain", + identifier: "flowTokenVault", + }, + }, + { + domain: "notValidDomain", + identifier: "flowTokenVault", + }, + true, + ], ].forEach(([cast, input, asArgument, asInjection, shouldError = false]) => { describe(cast.label, () => { test(`t.${cast.label}.asArgument(${input})`, () => { From 4e07a8658b66d7a8057cd4064ede3cdfd535b947 Mon Sep 17 00:00:00 2001 From: Sham Gir Date: Thu, 16 Jun 2022 16:53:51 +0500 Subject: [PATCH 12/12] PKG -- [types] add changeset --- .changeset/serious-planets-begin.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/serious-planets-begin.md diff --git a/.changeset/serious-planets-begin.md b/.changeset/serious-planets-begin.md new file mode 100644 index 000000000..20b16fbc2 --- /dev/null +++ b/.changeset/serious-planets-begin.md @@ -0,0 +1,5 @@ +--- +"@onflow/types": minor +--- + +AnyStruct type support added in @onflow/types