Pact's documentation says you can type objects with schemas. I have been unable to work out how to do this.
When creating an object, eg. in the REPL, with eg. {"a":1.0,"b":2.0}, it is of type object:*.
I have a schema of:
(defschema data
a : decimal
b : decimal
)
And a table schema that uses this:
(defschema table-schema
v : {data}
)
(deftable my-table:{table-schema})
As a note: the above does not type-check (see REPL output below)
I can find no way to insert into this table, as it produces a type error:
Type error: expected (defschema data [a:decimal, b:<a>]), found object:* at : (insert (deftable my-table:(defschema table-schema [v:(de... "123" {"v": {"value": 100.0,"rate": 0.5}}
Many thanks in advance.
Full Code:
(define-keyset 'admin-keyset (read-keyset "admin-keyset"))
(module complex-schema 'admin-keyset
(defschema data
a : decimal
b
)
(defschema table-schema
v : {data}
)
(deftable my-table:{table-schema})
(defun new-thing (id:string)
(insert my-table id {
"v" : {"value":100.0,"rate":0.5}
})
)
)
REPL Output/Interaction
pact> (load "complex-schema.repl")
"Loading complex-schema.repl..."
"Setting transaction keys"
"Setting transaction data"
"Begin Tx 0"
"Loading complex-schema.pact..."
"Keyset defined"
"Loaded module complex-schema, hash tGl1sYgL1VWh8zBJCU3KmMGbofkzK0gBEyXwbRnp7sI"
"Typecheck complex-schema: Unable to resolve all types"
:OutputFailure: Unable to unify field type: (v, {complex-schema.data}, (object:*, Object object5::object:* {"rate": Prim decimal6::decimal = LDecimal {_lDecimal = 0.5},"value": Prim decimal7::decimal = LDecimal {_lDecimal = 100.0}}))
"Verification of complex-schema failed"
:OutputFailure: Unable to unify field type: (v, {complex-schema.data}, (object:*, Object object4::object:* {"rate": Prim decimal5::decimal = LDecimal {_lDecimal = 0.5},"value": Prim decimal6::decimal = LDecimal {_lDecimal = 100.0}}))
"Commit Tx 0"
"Begin Tx 1"
"Using complex-schema"
"TableCreated"
"Commit Tx 1"
pact> (use complex-schema)
"Using complex-schema"
pact> (new-thing "123")
<interactive>:1:0: Type error: expected (defschema data [a:decimal, b:<a>]), found object:*
at : (insert (deftable my-table:(defschema table-schema [v:(de... "123" {"v": {"value": 100.0,"rate": 0.5}})
at <interactive>:0:0: (new-thing "123")
I'm not sure if it's exactly what you want since I left out the explicit type definition on the table. But it allowed me to load your module and call it's function to insert the record in
my-table:You can apply the type definition upon reading the object:
and then use the properties of the object with
(at 'property objectname)