@@ -33,32 +33,20 @@ export const ByteArray: ByteArray = Data.BytesSchema.annotations({
3333// }
3434// );
3535
36- interface Integer extends Schema . transform < typeof Data . IntSchema , typeof Schema . BigIntFromSelf > { }
36+ interface Integer extends Schema . SchemaClass < bigint , bigint , never > { }
3737
3838/**
3939 * Creates a schema for integers using Plutus Data Integer transformation
4040 * The integer is represented as a BigInt
4141 *
4242 * @since 2.0.0
4343 */
44- // export const Integer: Integer = Schema.transform(
45- // Data.IntSchema,
46- // Schema.BigIntFromSelf,
47- // {
48- // strict: true,
49- // encode: (value) => value,
50- // decode: (value) => value,
51- // }
52- // ).annotations({
53- // identifier: "TSchema.Integer",
54- // });
55-
56- export const Integer = Data . IntSchema . annotations ( {
44+ export const Integer : Integer = Data . IntSchema . annotations ( {
5745 identifier : "TSchema.Integer"
5846} )
5947
6048interface Literal < Literals extends NonEmptyReadonlyArray < SchemaAST . LiteralValue > >
61- extends Schema . transform < typeof Data . Constr , Schema . Literal < [ ...Literals ] > > { }
49+ extends Schema . transform < Schema . SchemaClass < Data . Constr , Data . Constr , never > , Schema . Literal < [ ...Literals ] > > { }
6250
6351/**
6452 * Creates a schema for literal types with Plutus Data Constructor transformation
@@ -68,22 +56,22 @@ interface Literal<Literals extends NonEmptyReadonlyArray<SchemaAST.LiteralValue>
6856export const Literal = < Literals extends NonEmptyReadonlyArray < Exclude < SchemaAST . LiteralValue , null | bigint > > > (
6957 ...self : Literals
7058) : Literal < Literals > =>
71- Schema . transform ( Data . Constr , Schema . Literal ( ...self ) , {
59+ Schema . transform ( Schema . typeSchema ( Data . Constr ) , Schema . Literal ( ...self ) , {
7260 strict : true ,
73- encode : ( value ) => Data . constr ( BigInt ( self . indexOf ( value ) ) , [ ] ) ,
61+ encode : ( value ) => new Data . Constr ( { index : BigInt ( self . indexOf ( value ) ) , fields : [ ] } ) ,
7462 decode : ( value ) => self [ Number ( value . index ) ]
7563 } )
7664
7765interface OneLiteral < Single extends Exclude < SchemaAST . LiteralValue , null | bigint > >
78- extends Schema . transform < typeof Data . Constr , Schema . Literal < [ Single ] > > { }
66+ extends Schema . transform < Schema . SchemaClass < Data . Constr , Data . Constr , never > , Schema . Literal < [ Single ] > > { }
7967
8068export const OneLiteral = < Single extends Exclude < SchemaAST . LiteralValue , null | bigint > > (
8169 self : Single
8270) : OneLiteral < Single > =>
83- Schema . transform ( Data . Constr , Schema . Literal ( self ) , {
71+ Schema . transform ( Schema . typeSchema ( Data . Constr ) , Schema . Literal ( self ) , {
8472 strict : true ,
8573
86- encode : ( _value ) => Data . constr ( 0n , [ ] ) ,
74+ encode : ( _value ) => new Data . Constr ( { index : 0n , fields : [ ] } ) ,
8775
8876 decode : ( _value ) => self
8977 } )
@@ -116,48 +104,22 @@ interface Map<K extends Schema.Schema.Any, V extends Schema.Schema.Any>
116104 * @since 1.0.0
117105 */
118106export const Map = < K extends Schema . Schema . Any , V extends Schema . Schema . Any > ( key : K , value : V ) : Map < K , V > =>
119- Schema . transform (
120- Schema . typeSchema ( Data . MapSchema ) ,
121- Schema . MapFromSelf ( { key, value } ) . annotations ( {
122- identifier : "TSchema.Map"
123- } ) ,
124- {
125- strict : false ,
126- encode : ( tsMap ) => {
127- // Transform TypeScript Map<K_TS, V_TS> to Data Map<K_Data, V_Data>
128- // The individual key/value transformations are handled by the schema framework
129- return new globalThis . Map ( [ ...tsMap ] )
130- } ,
131- decode : ( dataMap ) => {
132- // Transform Data Map<K_Data, V_Data> to TypeScript Map<K_TS, V_TS>
133- // The individual key/value transformations are handled by the schema framework
134- return new globalThis . Map ( [ ...dataMap ] )
135- }
136- }
137- ) . annotations ( {
138- identifier : "TSchema.MapFromDataMap" ,
139- message : ( issue ) => {
140- const actual = issue . actual
141- const isMap =
142- typeof actual === "object" &&
143- actual !== null &&
144- typeof ( actual as Record < string , unknown > ) . size === "number" &&
145- typeof ( actual as Record < string , unknown > ) . set === "function"
146-
147- if ( ! isMap ) {
148- const actualType =
149- typeof actual === "bigint"
150- ? "bigint"
151- : typeof actual === "object" && actual !== null && globalThis . Array . isArray ( actual )
152- ? "array"
153- : typeof actual
154- return `Invalid value for Map: received ${ actualType } (${ JSON . stringify ( actual ) } ), expected Map`
155- }
156- return `Invalid Map: check that all keys and values match the expected types`
107+ Schema . transform ( Schema . typeSchema ( Data . MapSchema ) , Schema . MapFromSelf ( { key, value } ) , {
108+ strict : false ,
109+ encode : ( tsMap ) => {
110+ // Transform TypeScript Map<K_TS, V_TS> to Data Map<K_Data, V_Data>
111+ // The individual key/value transformations are handled by the schema framework
112+ return new globalThis . Map ( [ ...tsMap ] )
113+ } ,
114+ decode : ( dataMap ) => {
115+ // Transform Data Map<K_Data, V_Data> to TypeScript Map<K_TS, V_TS>
116+ // The individual key/value transformations are handled by the schema framework
117+ return new globalThis . Map ( [ ...dataMap ] )
157118 }
158119 } )
159120
160- interface NullOr < S extends Schema . Schema . All > extends Schema . transform < typeof Data . Constr , Schema . NullOr < S > > { }
121+ interface NullOr < S extends Schema . Schema . All >
122+ extends Schema . transform < Schema . SchemaClass < Data . Constr , Data . Constr , never > , Schema . NullOr < S > > { }
161123
162124/**
163125 * Creates a schema for nullable types that transforms to/from Plutus Data Constructor
@@ -168,13 +130,15 @@ interface NullOr<S extends Schema.Schema.All> extends Schema.transform<typeof Da
168130 * @since 2.0.0
169131 */
170132export const NullOr = < S extends Schema . Schema . All > ( self : S ) : NullOr < S > =>
171- Schema . transform ( Data . Constr , Schema . NullOr ( self ) , {
133+ Schema . transform ( Schema . typeSchema ( Data . Constr ) , Schema . NullOr ( self ) , {
172134 strict : true ,
173- encode : ( value ) => ( value === null ? Data . constr ( 1n , [ ] ) : Data . constr ( 0n , [ value ] ) ) ,
135+ encode : ( value ) =>
136+ value === null ? new Data . Constr ( { index : 1n , fields : [ ] } ) : new Data . Constr ( { index : 0n , fields : [ value ] } ) ,
174137 decode : ( value ) => ( value . index === 1n ? null : ( value . fields [ 0 ] as Schema . Schema . Type < S > ) )
175138 } )
176139
177- interface UndefineOr < S extends Schema . Schema . Any > extends Schema . transform < typeof Data . Constr , Schema . UndefinedOr < S > > { }
140+ interface UndefineOr < S extends Schema . Schema . Any >
141+ extends Schema . transform < Schema . SchemaClass < Data . Constr , Data . Constr , never > , Schema . UndefinedOr < S > > { }
178142
179143/**
180144 * Creates a schema for undefined types that transforms to/from Plutus Data Constructor
@@ -185,9 +149,12 @@ interface UndefineOr<S extends Schema.Schema.Any> extends Schema.transform<typeo
185149 * @since 2.0.0
186150 */
187151export const UndefinedOr = < S extends Schema . Schema . Any > ( self : S ) : UndefineOr < S > =>
188- Schema . transform ( Data . Constr , Schema . UndefinedOr ( self ) , {
152+ Schema . transform ( Schema . typeSchema ( Data . Constr ) , Schema . UndefinedOr ( self ) , {
189153 strict : true ,
190- encode : ( value ) => ( value === undefined ? Data . constr ( 1n , [ ] ) : Data . constr ( 0n , [ value ] ) ) ,
154+ encode : ( value ) =>
155+ value === undefined
156+ ? new Data . Constr ( { index : 1n , fields : [ ] } )
157+ : new Data . Constr ( { index : 0n , fields : [ value ] } ) ,
191158 decode : ( value ) => ( value . index === 1n ? undefined : ( value . fields [ 0 ] as Schema . Schema . Type < S > ) )
192159 } )
193160
@@ -225,7 +192,7 @@ export const Boolean: Boolean = Schema.transform(
225192} )
226193
227194interface Struct < Fields extends Schema . Struct . Fields >
228- extends Schema . transform < typeof Data . Constr , Schema . Struct < Fields > > { }
195+ extends Schema . transform < Schema . SchemaClass < Data . Constr , Data . Constr , never > , Schema . Struct < Fields > > { }
229196
230197/**
231198 * Creates a schema for struct types using Plutus Data Constructor
@@ -234,12 +201,22 @@ interface Struct<Fields extends Schema.Struct.Fields>
234201 * @since 2.0.0
235202 */
236203export const Struct = < Fields extends Schema . Struct . Fields > ( fields : Fields ) : Struct < Fields > =>
237- Schema . transform ( Data . Constr , Schema . Struct ( fields ) , {
204+ Schema . transform ( Schema . typeSchema ( Data . Constr ) , Schema . Struct ( fields ) , {
238205 strict : false ,
239- encode : ( obj ) => Data . constr ( 0n , Object . values ( obj ) as globalThis . Array < Data . Data > ) ,
240- decode : ( constr : { readonly index : bigint ; readonly fields : ReadonlyArray < any > } ) => {
206+ encode : ( encodedStruct ) => {
207+ // encodedStruct is the result of Schema.Struct(fields), which has already transformed all fields
208+ return new Data . Constr ( {
209+ index : 0n ,
210+ fields : Object . values ( encodedStruct )
211+ } )
212+ } ,
213+ decode : ( fromA ) => {
241214 const keys = Object . keys ( fields )
242- return Object . fromEntries ( keys . map ( ( key , index ) => [ key , constr . fields [ index ] ] ) )
215+ const result = { } as Record < string , Data . Data >
216+ keys . forEach ( ( key , index ) => {
217+ result [ key ] = fromA . fields [ index ]
218+ } )
219+ return result as { [ K in keyof Schema . Struct . Encoded < Fields > ] : Schema . Struct . Encoded < Fields > [ K ] }
243220 }
244221 } )
245222
0 commit comments