@@ -88,7 +88,6 @@ public ElmClientCodegen() {
88
88
super ();
89
89
outputFolder = "generated-code/elm" ;
90
90
modelTemplateFiles .put ("model.mustache" , ".elm" );
91
- apiTemplateFiles .put ("api.mustache" , ".elm" );
92
91
templateDir = "elm" ;
93
92
94
93
supportsInheritance = true ;
@@ -198,6 +197,7 @@ public void processOpts() {
198
197
case ELM_018 :
199
198
LOGGER .info ("Elm version: 0.18" );
200
199
additionalProperties .put ("isElm018" , true );
200
+ apiTemplateFiles .put ("api018.mustache" , ".elm" );
201
201
supportingFiles .add (new SupportingFile ("DateOnly018.mustache" , "src" , "DateOnly.elm" ));
202
202
supportingFiles .add (new SupportingFile ("DateTime018.mustache" , "src" , "DateTime.elm" ));
203
203
supportingFiles .add (new SupportingFile ("elm-package018.mustache" , "" , "elm-package.json" ));
@@ -206,6 +206,7 @@ public void processOpts() {
206
206
case ELM_019 :
207
207
LOGGER .info ("Elm version: 0.19" );
208
208
additionalProperties .put ("isElm019" , true );
209
+ apiTemplateFiles .put ("api.mustache" , ".elm" );
209
210
supportingFiles .add (new SupportingFile ("DateOnly.mustache" , "src" , "DateOnly.elm" ));
210
211
supportingFiles .add (new SupportingFile ("DateTime.mustache" , "src" , "DateTime.elm" ));
211
212
supportingFiles .add (new SupportingFile ("elm.mustache" , "" , "elm.json" ));
@@ -423,14 +424,33 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
423
424
final Map <String , Set <String >> dependencies = new HashMap <>();
424
425
425
426
for (CodegenOperation op : ops ) {
426
- String path = op .path ;
427
- for (CodegenParameter param : op .pathParams ) {
428
- final String var = paramToString (param );
429
- path = path .replace ("{" + param .paramName + "}" , "\" ++ " + var + " ++ \" " );
430
- hasDateTime = hasDateTime || param .isDateTime ;
431
- hasDate = hasDate || param .isDate ;
427
+ if (ElmVersion .ELM_018 .equals (elmVersion )) {
428
+ String path = op .path ;
429
+ for (CodegenParameter param : op .pathParams ) {
430
+ final String var = paramToString (param , false , null );
431
+ path = path .replace ("{" + param .paramName + "}" , "\" ++ " + var + " ++ \" " );
432
+ hasDateTime = hasDateTime || param .isDateTime ;
433
+ hasDate = hasDate || param .isDate ;
434
+ }
435
+ op .path = ("\" " + path + "\" " ).replaceAll (" \\ +\\ + \" \" " , "" );
436
+ } else {
437
+ final List <String > paths = Arrays .asList (op .path .substring (1 ).split ("/" ));
438
+ String path = paths .stream ().map (str -> str .charAt (0 ) == '{' ? str : "\" " + str + "\" " ).collect (Collectors .joining (", " ));
439
+ for (CodegenParameter param : op .pathParams ) {
440
+ String str = paramToString (param , false , null );
441
+ path = path .replace ("{" + param .paramName + "}" , str );
442
+ hasDateTime = hasDateTime || param .isDateTime ;
443
+ hasDate = hasDate || param .isDate ;
444
+ }
445
+ op .path = path ;
446
+
447
+ final String query = op .queryParams .stream ()
448
+ .map (param -> paramToString (param , true , "Url.string \" " + param .paramName + "\" " ))
449
+ .collect (Collectors .joining (", " ));
450
+ op .vendorExtensions .put ("query" , query );
451
+ // TODO headers
452
+ // TODO forms
432
453
}
433
- op .path = ("\" " + path + "\" " ).replaceAll (" \\ +\\ + \" \" " , "" );
434
454
435
455
if (op .bodyParam != null && !op .bodyParam .isPrimitiveType && !op .bodyParam .isMapContainer ) {
436
456
final String encoder = (String ) op .bodyParam .vendorExtensions .get (ENCODER );
@@ -523,26 +543,49 @@ private String toOptionalValue(String value) {
523
543
return "(Just " + value + ")" ;
524
544
}
525
545
526
- private String paramToString (final CodegenParameter param ) {
527
- final String paramName = param .paramName ;
546
+ private String paramToString (final CodegenParameter param , final boolean useMaybe , final String maybeMapResult ) {
547
+ final String paramName = (ElmVersion .ELM_018 .equals (elmVersion ) ? "" : "params." ) + param .paramName ;
548
+ if (!useMaybe ) {
549
+ param .required = true ;
550
+ }
528
551
552
+ String mapFn = null ;
529
553
if (param .isString || param .isUuid || param .isBinary || param .isByteArray ) {
530
- return paramName ;
554
+ mapFn = "" ;
531
555
} else if (param .isBoolean ) {
532
- return "if " + paramName + " then \" true\" else \" false\" " ;
556
+ mapFn = "( \\ val -> if val then \" true\" else \" false\" ) " ;
533
557
} else if (param .isDateTime ) {
534
- return "DateTime.toString " + paramName ;
558
+ mapFn = "DateTime.toString" ;
535
559
} else if (param .isDate ) {
536
- return "DateOnly.toString " + paramName ;
560
+ mapFn = "DateOnly.toString" ;
537
561
} else if (ElmVersion .ELM_018 .equals (elmVersion )) {
538
- return "toString " + paramName ;
562
+ mapFn = "toString" ;
539
563
} else if (param .isInteger || param .isLong ) {
540
- return "String.fromInt " + paramName ;
564
+ mapFn = "String.fromInt" ;
541
565
} else if (param .isFloat || param .isDouble ) {
542
- return "String.fromFloat " + paramName ;
566
+ mapFn = "String.fromFloat" ;
567
+ } else if (param .isListContainer ) {
568
+ // TODO duplicate ALL types from parameter to property...
569
+ if (param .items .isString || param .items .isUuid || param .items .isBinary || param .items .isByteArray ) {
570
+ mapFn = "String.join \" ,\" " ;
571
+ }
543
572
}
544
-
545
- throw new RuntimeException ("Parameter '" + paramName + "' cannot be converted to a string. Please report the issue." );
573
+ if (mapFn == null ) {
574
+ throw new RuntimeException ("Parameter '" + param .paramName + "' cannot be converted to a string. Please report the issue." );
575
+ }
576
+
577
+ if (param .isListContainer ) {
578
+ if (!param .required ) {
579
+ mapFn = "(" + mapFn + ")" ;
580
+ }
581
+ }
582
+ String mapResult = "" ;
583
+ if (maybeMapResult != null ) {
584
+ mapResult = maybeMapResult + (param .required ? " <|" : " <<" );
585
+ }
586
+ final String just = useMaybe ? "Just (" : "" ;
587
+ final String justEnd = useMaybe ? ")" : "" ;
588
+ return (param .required ? just : "Maybe.map" ) + mapResult + " " + mapFn + " " + paramName + (param .required ? justEnd : "" );
546
589
}
547
590
548
591
@ Override
0 commit comments