@@ -1183,6 +1183,14 @@ func TestTypedFunctions(t *testing.T) {
11831183 def mixed_items(values: array<int | string>) -> array<int | string>
11841184 values
11851185 end
1186+
1187+ def player_payload(payload: { id: string, score: int, active: bool? }) -> { id: string, score: int, active: bool? }
1188+ payload
1189+ end
1190+
1191+ def shaped_rows(rows: array<{ id: string, stats: { wins: int } }>) -> array<{ id: string, stats: { wins: int } }>
1192+ rows
1193+ end
11861194 ` )
11871195
11881196 if fn , ok := script .Function ("bad_return" ); ! ok || fn .ReturnTy == nil {
@@ -1224,6 +1232,23 @@ func TestTypedFunctions(t *testing.T) {
12241232 if got := callFunc (t , script , "mixed_items" , []Value {NewArray ([]Value {NewInt (1 ), NewString ("two" ), NewInt (3 )})}); got .Kind () != KindArray {
12251233 t .Fatalf ("mixed_items expected array result, got %v" , got .Kind ())
12261234 }
1235+ if got := callFunc (t , script , "player_payload" , []Value {NewHash (map [string ]Value {
1236+ "id" : NewString ("p-1" ),
1237+ "score" : NewInt (42 ),
1238+ "active" : NewNil (),
1239+ })}); got .Kind () != KindHash {
1240+ t .Fatalf ("player_payload expected hash result, got %v" , got .Kind ())
1241+ }
1242+ if got := callFunc (t , script , "shaped_rows" , []Value {NewArray ([]Value {
1243+ NewHash (map [string ]Value {
1244+ "id" : NewString ("p-1" ),
1245+ "stats" : NewHash (map [string ]Value {
1246+ "wins" : NewInt (7 ),
1247+ }),
1248+ }),
1249+ })}); got .Kind () != KindArray {
1250+ t .Fatalf ("shaped_rows expected array result, got %v" , got .Kind ())
1251+ }
12271252 if got := callFunc (t , script , "nil_result" , nil ); ! got .Equal (NewNil ()) {
12281253 t .Fatalf ("nil_result mismatch: %v" , got )
12291254 }
@@ -1288,6 +1313,42 @@ func TestTypedFunctions(t *testing.T) {
12881313 if err == nil || ! strings .Contains (err .Error (), "expected array<int | string>" ) {
12891314 t .Fatalf ("expected typed union array arg error, got %v" , err )
12901315 }
1316+
1317+ _ , err = script .Call (context .Background (), "player_payload" , []Value {
1318+ NewHash (map [string ]Value {
1319+ "id" : NewString ("p-1" ),
1320+ "score" : NewInt (42 ),
1321+ "role" : NewString ("captain" ),
1322+ }),
1323+ }, CallOptions {})
1324+ if err == nil || ! strings .Contains (err .Error (), "expected { active: bool?, id: string, score: int }" ) {
1325+ t .Fatalf ("expected shape extra-field error, got %v" , err )
1326+ }
1327+
1328+ _ , err = script .Call (context .Background (), "player_payload" , []Value {
1329+ NewHash (map [string ]Value {
1330+ "id" : NewString ("p-1" ),
1331+ "score" : NewString ("wrong" ),
1332+ "active" : NewBool (true ),
1333+ }),
1334+ }, CallOptions {})
1335+ if err == nil || ! strings .Contains (err .Error (), "expected { active: bool?, id: string, score: int }" ) {
1336+ t .Fatalf ("expected shape field-type error, got %v" , err )
1337+ }
1338+
1339+ _ , err = script .Call (context .Background (), "shaped_rows" , []Value {
1340+ NewArray ([]Value {
1341+ NewHash (map [string ]Value {
1342+ "id" : NewString ("p-1" ),
1343+ "stats" : NewHash (map [string ]Value {
1344+ "wins" : NewString ("bad" ),
1345+ }),
1346+ }),
1347+ }),
1348+ }, CallOptions {})
1349+ if err == nil || ! strings .Contains (err .Error (), "expected array<{ id: string, stats: { wins: int } }>" ) {
1350+ t .Fatalf ("expected nested shape error, got %v" , err )
1351+ }
12911352}
12921353
12931354func TestArrayAndHashHelpers (t * testing.T ) {
0 commit comments