File tree Expand file tree Collapse file tree 2 files changed +30
-7
lines changed
Expand file tree Collapse file tree 2 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import type {
99 PdlModelInput ,
1010 LocalizedExpression ,
1111} from "./pdl_ast"
12+ import { match , P } from "ts-pattern"
1213
1314/** Re-export for convenience */
1415export type { PdlBlock } from "./pdl_ast"
@@ -325,10 +326,23 @@ function isExpr(e: unknown): e is LocalizedExpression {
325326 )
326327}
327328
328- export function extractModel ( { model } : ModelBlock ) : string {
329- return typeof model === "string"
330- ? model
331- : isExpr ( model )
332- ? String ( model . pdl__result )
333- : "unknown"
329+ function getValue < T > ( expr : ExpressionT < T > ) {
330+ if ( isExpr ( expr ) ) {
331+ if ( expr . pdl__result === undefined ) {
332+ return expr . pdl__expr
333+ } else {
334+ return expr . pdl__result
335+ }
336+ } else {
337+ return expr
338+ }
339+ }
340+
341+ export function extractModel ( block : ModelBlock ) : string {
342+ return match ( block )
343+ . with ( { model : P . _ } , ( block ) => stringify ( getValue ( block . model ) ) )
344+ . with ( { processor : { model : P . _ } } , ( block ) => stringify ( getValue ( block . processor . model ) ) )
345+ . with ( { processor : { type : P . _ } } , ( block ) => stringify ( getValue ( block . processor . type ) ) )
346+ . with ( { processor : P . _ } , ( block ) => stringify ( getValue ( block . processor ) ) )
347+ . exhaustive ( )
334348}
Original file line number Diff line number Diff line change @@ -97,7 +97,7 @@ export function map_block_children(
9797 }
9898 } ,
9999 )
100- . with ( { kind : "model" } , ( block ) => {
100+ . with ( { kind : "model" , model : P . _ } , ( block ) => {
101101 const model = f_expr ( block . model )
102102 const input = block . input ? f_block ( block . input ) : undefined
103103 const parameters = block . parameters ? f_expr ( block . parameters ) : undefined
@@ -109,6 +109,15 @@ export function map_block_children(
109109 parameters,
110110 }
111111 } )
112+ . with ( { kind : "model" , } , ( block ) => {
113+ const input = block . input ? f_block ( block . input ) : undefined
114+ const parameters = block . parameters ? f_expr ( block . parameters ) : undefined
115+ return {
116+ ...block ,
117+ input,
118+ parameters,
119+ }
120+ } )
112121 . with ( { kind : "code" } , ( block ) => {
113122 if ( isArgs ( block ) ) {
114123 const args = block . args . map ( ( arg ) => f_expr ( arg ) )
You can’t perform that action at this time.
0 commit comments