[elm] Major refactoring; add discriminator support (#1104)

* Use the same name for all exposed encoder & decoders;
* Improve imports (limit exposures);
* Add support for OAS3 discriminators;
* Distinct between 0.18 & latest mustache files.
This commit is contained in:
Erik Timmers 2018-09-29 12:13:04 +02:00 committed by GitHub
parent cbddb08468
commit 345b7ec7e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 315 additions and 354 deletions

View File

@ -53,6 +53,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(ElmClientCodegen.class);
@ -62,7 +63,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final String ELM_VERSION = "elmVersion";
private static final String ENCODER = "elmEncoder";
private static final String DECODER = "elmDecoder";
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
private static final String DISCRIMINATOR_NAME = "discriminatorName";
private static final String UNION_TYPE = "elmUnionType";
protected String packageName = "openapi";
@ -187,10 +188,18 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
case ELM_018:
LOGGER.info("Elm version = 0.18");
additionalProperties.put("isElm018", true);
supportingFiles.add(new SupportingFile("DateOnly018.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime018.mustache", "src", "DateTime.elm"));
supportingFiles.add(new SupportingFile("elm-package018.mustache", "", "elm-package.json"));
supportingFiles.add(new SupportingFile("Main018.mustache", "src", "Main.elm"));
break;
case ELM_019:
LOGGER.info("Elm version = 0.19");
additionalProperties.put("isElm019", true);
supportingFiles.add(new SupportingFile("DateOnly.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime.mustache", "src", "DateTime.elm"));
supportingFiles.add(new SupportingFile("elm.mustache", "", "elm.json"));
supportingFiles.add(new SupportingFile("Main.mustache", "src", "Main.elm"));
break;
default:
throw new RuntimeException("Undefined Elm version");
@ -199,19 +208,6 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("Byte.mustache", "src", "Byte.elm"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
if (ElmVersion.ELM_018.equals(elmVersion)) {
supportingFiles.add(new SupportingFile("DateOnly018.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime018.mustache", "src", "DateTime.elm"));
supportingFiles.add(new SupportingFile("elm-package.mustache", "", "elm-package.json"));
supportingFiles.add(new SupportingFile("Main018.mustache", "src", "Main.elm"));
}
if (ElmVersion.ELM_019.equals(elmVersion)) {
supportingFiles.add(new SupportingFile("DateOnly019.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime019.mustache", "src", "DateTime.elm"));
supportingFiles.add(new SupportingFile("elm.mustache", "", "elm.json"));
supportingFiles.add(new SupportingFile("Main019.mustache", "src", "Main.elm"));
}
}
@Override
@ -339,55 +335,40 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
for (Map<String, Object> mo : models) {
CodegenModel cm = (CodegenModel) mo.get("model");
if (cm.isEnum) {
this.addEncoderAndDecoder(cm.vendorExtensions, cm.classname, false, false);
addEncoderAndDecoder(cm.vendorExtensions, cm.classname, DataTypeExposure.EXPOSED);
cm.vendorExtensions.put(UNION_TYPE, cm.classname);
} else if (cm.isAlias) {
this.addEncoderAndDecoder(cm.vendorExtensions, cm.dataType, false, true);
addEncoderAndDecoder(cm.vendorExtensions, cm.dataType, DataTypeExposure.EXPOSED);
}
List<ElmImport> elmImports = new ArrayList<>();
for (CodegenProperty property : cm.allVars) {
if (property.complexType != null) {
elmImports.add(createPropertyImport(property));
final ElmImport elmImport = createImport(property.complexType);
elmImports.add(elmImport);
}
}
if (cm.isArrayModel) {
if (cm.arrayModelType != null) {
// add type imports
final ElmImport elmImport = new ElmImport();
final String modulePrefix = customPrimitives.contains(cm.arrayModelType) ? "" : "Data.";
elmImport.moduleName = modulePrefix + cm.arrayModelType;
elmImport.exposures = new TreeSet<>();
elmImport.exposures.add(cm.arrayModelType);
elmImport.exposures.add(org.openapitools.codegen.utils.StringUtils.camelize(cm.arrayModelType, true) + "Decoder");
elmImport.exposures.add(org.openapitools.codegen.utils.StringUtils.camelize(cm.arrayModelType, true) + "Encoder");
elmImport.hasExposures = true;
final ElmImport elmImport = createImport(cm.arrayModelType);
elmImports.add(elmImport);
}
}
if (cm.discriminator != null) {
for (CodegenModel child : cm.children) {
// add child imports
final ElmImport elmImport = new ElmImport();
final String modulePrefix = customPrimitives.contains(child.classname) ? "" : "Data.";
elmImport.moduleName = modulePrefix + child.classname;
elmImport.exposures = new TreeSet<>();
elmImport.exposures.add(child.classname);
elmImport.exposures.add(child.classVarName + "Decoder");
elmImport.exposures.add(child.classVarName + "Encoder");
elmImport.hasExposures = true;
final ElmImport elmImport = createImport(child.classname);
elmImports.add(elmImport);
// set discriminator value to all children (recursively)
this.setDiscriminatorValue(child, cm.getDiscriminatorName(), this.getDiscriminatorValue(child));
final String propertyName = cm.discriminator.getPropertyName();
final List<CodegenProperty> allVars = child.allVars.stream()
.filter(var -> !var.baseName.equals(propertyName))
.collect(Collectors.toList());
child.allVars.clear();
child.allVars.addAll(allVars);
// add all non-discriminator vars
int index = 0;
for (CodegenProperty property : cm.vars) {
if (!cm.discriminator.equals(property.baseName)) {
child.vars.add(index++, property);
}
}
child.vendorExtensions.put(DISCRIMINATOR_NAME, propertyName);
}
}
inner.put("elmImports", elmImports);
@ -396,42 +377,16 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
return objs;
}
private void setDiscriminatorValue(CodegenModel model, String baseName, String value) {
for (CodegenProperty prop : model.vars) {
if (prop.baseName.equals(baseName)) {
prop.discriminatorValue = value;
}
}
for (CodegenProperty prop : model.allVars) {
if (prop.baseName.equals(baseName)) {
prop.discriminatorValue = value;
}
}
if (model.children != null) {
final boolean newDiscriminator = model.discriminator != null;
for (CodegenModel child : model.children) {
this.setDiscriminatorValue(child, baseName, newDiscriminator ? value : this.getDiscriminatorValue(child));
}
}
}
private String getDiscriminatorValue(CodegenModel model) {
return model.vendorExtensions.containsKey(X_DISCRIMINATOR_TYPE) ?
(String) model.vendorExtensions.get(X_DISCRIMINATOR_TYPE) : model.classname;
}
private ElmImport createPropertyImport(final CodegenProperty property) {
private ElmImport createImport(final String name) {
final ElmImport elmImport = new ElmImport();
final String modulePrefix = customPrimitives.contains(property.complexType) ? "" : "Data.";
elmImport.moduleName = modulePrefix + property.complexType;
final boolean isData = !customPrimitives.contains(name);
final String modulePrefix = isData ? "Data." : "";
elmImport.moduleName = modulePrefix + name;
if (isData) {
elmImport.as = name;
}
elmImport.exposures = new TreeSet<>();
elmImport.exposures.add(property.complexType);
if (property.vendorExtensions.containsKey(DECODER)) {
elmImport.exposures.add((String) property.vendorExtensions.get(DECODER));
}
if (property.vendorExtensions.containsKey(ENCODER)) {
elmImport.exposures.add((String) property.vendorExtensions.get(ENCODER));
}
elmImport.exposures.add(name);
elmImport.hasExposures = true;
return elmImport;
}
@ -463,7 +418,6 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
if (!dependencies.containsKey(op.bodyParam.dataType)) {
dependencies.put(op.bodyParam.dataType, new TreeSet<String>());
}
dependencies.get(op.bodyParam.dataType).add(encoder);
}
}
for (CodegenResponse resp : op.responses) {
@ -475,7 +429,6 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
if (!dependencies.containsKey(resp.dataType)) {
dependencies.put(resp.dataType, new TreeSet<String>());
}
dependencies.get(resp.dataType).add(decoder);
}
}
}
@ -485,6 +438,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
final ElmImport elmImport = new ElmImport();
final String key = entry.getKey();
elmImport.moduleName = "Data." + key;
elmImport.as = key;
elmImport.exposures = entry.getValue();
elmImport.exposures.add(key);
elmImport.hasExposures = true;
@ -582,10 +536,12 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
public CodegenProperty fromProperty(String name, Schema p) {
final CodegenProperty property = super.fromProperty(name, p);
final String dataType = property.isEnum ? property.baseName : property.dataType;
addEncoderAndDecoder(property.vendorExtensions, dataType, property.isMapContainer, property.isPrimitiveType && !property.isEnum);
if (property.isEnum) {
addEncoderAndDecoder(property.vendorExtensions, property.baseName, DataTypeExposure.INTERNAL);
property.vendorExtensions.put(UNION_TYPE, property.datatypeWithEnum);
} else {
final boolean isPrimitiveType = property.isMapContainer ? isPrimitiveDataType(property.dataType) : property.isPrimitiveType;
addEncoderAndDecoder(property.vendorExtensions, property.dataType, isPrimitiveType ? DataTypeExposure.PRIMITIVE : DataTypeExposure.EXTERNAL);
}
return property;
@ -595,33 +551,46 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
public CodegenResponse fromResponse(OpenAPI openAPI, String responseCode, ApiResponse resp) {
final CodegenResponse response = super.fromResponse(openAPI, responseCode, resp);
if (response.dataType != null) {
addEncoderAndDecoder(response.vendorExtensions, response.dataType, response.isMapContainer, response.primitiveType);
final boolean isPrimitiveType = response.isMapContainer ? isPrimitiveDataType(response.dataType) : response.primitiveType;
addEncoderAndDecoder(response.vendorExtensions, response.dataType, isPrimitiveType ? DataTypeExposure.PRIMITIVE : DataTypeExposure.EXTERNAL);
}
return response;
}
@Override
public void postProcessParameter(CodegenParameter parameter) {
addEncoderAndDecoder(parameter.vendorExtensions, parameter.dataType, parameter.isMapContainer, parameter.isPrimitiveType);
final boolean isPrimitiveType = parameter.isMapContainer ? isPrimitiveDataType(parameter.dataType) : parameter.isPrimitiveType;
addEncoderAndDecoder(parameter.vendorExtensions, parameter.dataType, isPrimitiveType ? DataTypeExposure.PRIMITIVE : DataTypeExposure.EXTERNAL);
}
private boolean isPrimitiveDataType(String dataType) {
return languageSpecificPrimitives.contains(dataType);
}
private void addEncoderAndDecoder(Map<String, Object> vendorExtensions, String dataType, Boolean isMapContainer, Boolean isPrimitiveType) {
if (isMapContainer) {
isPrimitiveType = isPrimitiveDataType(dataType);
}
private void addEncoderAndDecoder(final Map<String, Object> vendorExtensions, final String dataType, final DataTypeExposure dataTypeExposure) {
final String baseName = org.openapitools.codegen.utils.StringUtils.camelize(dataType, true);
String encoderName;
String decoderName;
if (isPrimitiveType) {
encoderName = "Encode." + baseName;
decoderName = "Decode." + baseName;
} else {
encoderName = baseName + "Encoder";
decoderName = baseName + "Decoder";
switch (dataTypeExposure) {
case EXPOSED:
decoderName = "decoder";
encoderName = "encoder";
break;
case INTERNAL:
encoderName = baseName + "Encoder";
decoderName = baseName + "Decoder";
break;
case EXTERNAL:
encoderName = dataType + ".encoder";
decoderName = dataType + ".decoder";
break;
case PRIMITIVE:
encoderName = "Encode." + baseName;
decoderName = "Decode." + baseName;
break;
default:
encoderName = "";
decoderName = "";
}
if (!vendorExtensions.containsKey(ENCODER)) {
vendorExtensions.put(ENCODER, encoderName);
@ -631,6 +600,13 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
}
}
private enum DataTypeExposure {
EXPOSED,
INTERNAL,
EXTERNAL,
PRIMITIVE
}
private static class ElmImport {
public String moduleName;
public String as;

View File

@ -1,4 +1,4 @@
module DateOnly exposing (DateOnly, dateOnlyDecoder, dateOnlyEncoder)
module DateOnly exposing (DateOnly, decoder, encoder)
import Iso8601
import Json.Decode as Decode exposing (Decoder)
@ -11,14 +11,14 @@ type alias DateOnly =
Time.Posix
dateOnlyDecoder : Decoder DateOnly
dateOnlyDecoder =
decoder : Decoder DateOnly
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateOnlyEncoder : DateOnly -> Encode.Value
dateOnlyEncoder model =
encoder : DateOnly -> Encode.Value
encoder model =
Iso8601.fromTime model
|> String.left 10
|> Encode.string

View File

@ -1,4 +1,4 @@
module DateOnly exposing (DateOnly, dateOnlyDecoder, dateOnlyEncoder)
module DateOnly exposing (DateOnly, decoder, encoder)
import Date
import Date.Extra exposing (fromIsoString, toFormattedString)
@ -11,14 +11,14 @@ type alias DateOnly =
Date.Date
dateOnlyDecoder : Decoder DateOnly
dateOnlyDecoder =
decoder : Decoder DateOnly
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateOnlyEncoder : DateOnly -> Encode.Value
dateOnlyEncoder model =
encoder : DateOnly -> Encode.Value
encoder model =
Encode.string <| toFormattedString "yyyy-MM-dd" model

View File

@ -1,4 +1,4 @@
module DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
module DateTime exposing (DateTime, decoder, encoder)
import Iso8601
import Json.Decode as Decode exposing (Decoder)
@ -11,14 +11,14 @@ type alias DateTime =
Time.Posix
dateTimeDecoder : Decoder DateTime
dateTimeDecoder =
decoder : Decoder DateTime
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateTimeEncoder : DateTime -> Encode.Value
dateTimeEncoder model =
encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| Iso8601.fromTime model

View File

@ -1,4 +1,4 @@
module DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
module DateTime exposing (DateTime, decoder, encoder)
import Date
import Date.Extra exposing (fromIsoString, toIsoString)
@ -11,14 +11,14 @@ type alias DateTime =
Date.Date
dateTimeDecoder : Decoder DateTime
dateTimeDecoder =
decoder : Decoder DateTime
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateTimeEncoder : DateTime -> Encode.Value
dateTimeEncoder model =
encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| toIsoString model

View File

@ -1,7 +1,6 @@
{{classVarName}}Decoder : Decoder {{classname}}
{{classVarName}}Decoder =
decoder : Decoder {{classname}}
decoder =
{{#isElm018}}decode{{/isElm018}}{{#isElm019}}Decode.succeed{{/isElm019}} {{classname}}
{{#allVars}}
{{^discriminatorValue}} |> {{>fieldDecoder}}
{{/discriminatorValue}}
|> {{>fieldDecoder}}
{{/allVars}}

View File

@ -1,7 +1,7 @@
{{classVarName}}Encoder : {{classname}} -> Encode.Value
{{classVarName}}Encoder model =
encoder : {{#vendorExtensions.discriminatorName}}String -> {{/vendorExtensions.discriminatorName}}{{classname}} -> Encode.Value
encoder {{#vendorExtensions.discriminatorName}}tag {{/vendorExtensions.discriminatorName}}model =
Encode.object
{{#allVars}}
{{#-first}}[{{/-first}}{{^-first}},{{/-first}} {{>fieldEncoder}}
{{/allVars}}
]
{{/allVars}}{{#vendorExtensions.discriminatorName}} , ( "{{{vendorExtensions.discriminatorName}}}", Encode.string tag ){{/vendorExtensions.discriminatorName}}
]

View File

@ -1 +1 @@
( "{{baseName}}", {{#discriminatorValue}}Encode.string "{{discriminatorValue}}"{{/discriminatorValue}}{{^discriminatorValue}}{{^required}}withDefault Encode.null (map {{/required}}{{#isContainer}}(Encode.list {{#isElm018}}<< List.map {{/isElm018}}{{/isContainer}}{{vendorExtensions.elmEncoder}}{{#isContainer}}){{/isContainer}} model.{{name}}{{^required}}){{/required}}{{/discriminatorValue}} )
( "{{baseName}}", {{#discriminatorValue}}Encode.string "{{discriminatorValue}}"{{/discriminatorValue}}{{^discriminatorValue}}{{^required}}Maybe.withDefault Encode.null (Maybe.map {{/required}}{{#isContainer}}(Encode.list {{#isElm018}}<< List.map {{/isElm018}}{{/isContainer}}{{vendorExtensions.elmEncoder}}{{#isContainer}}){{/isContainer}} model.{{name}}{{^required}}){{/required}}{{/discriminatorValue}} )

View File

@ -1,11 +1,10 @@
{{>licenseInfo}}
module Data.{{classname}} exposing ({{#models}}{{#model}}{{classname}}{{#hasChildren}}(..){{/hasChildren}}{{#isEnum}}(..){{/isEnum}}{{^isEnum}}{{#vars}}{{#isEnum}}, {{vendorExtensions.elmUnionType}}(..){{/isEnum}}{{/vars}}{{/isEnum}}, {{classVarName}}Decoder, {{classVarName}}Encoder{{/model}}{{/models}})
module Data.{{classname}} exposing ({{#models}}{{#model}}{{classname}}{{#hasChildren}}(..){{/hasChildren}}{{#isEnum}}(..){{/isEnum}}{{^isEnum}}{{#vars}}{{#isEnum}}, {{vendorExtensions.elmUnionType}}(..){{/isEnum}}{{/vars}}{{/isEnum}}, decoder, encoder{{/model}}{{/models}})
{{>imports}}import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing ({{#isElm018}}decode, {{/isElm018}}optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{{#models}}
{{#model}}
@ -14,6 +13,6 @@ import Maybe exposing (map, withDefault)
{-| {{{description}}}
-}
{{/description}}
{{#isEnum}}{{>modelTypeUnion}}{{/isEnum}}{{^isEnum}}{{#hasChildren}}{{>modelTypeDiscriminator}}{{/hasChildren}}{{^hasChildren}}{{#isAlias}}{{>modelTypePrimitive}}{{/isAlias}}{{^isAlias}}{{#isArrayModel}}{{>modelTypeArray}}{{/isArrayModel}}{{^isArrayModel}}{{>modelTypeAlias}}{{/isArrayModel}}{{/isAlias}}{{/hasChildren}}{{/isEnum}}
{{#isEnum}}{{>modelTypeUnion}}{{/isEnum}}{{^isEnum}}{{#discriminator}}{{>modelTypeDiscriminator}}{{/discriminator}}{{^discriminator}}{{#isAlias}}{{>modelTypePrimitive}}{{/isAlias}}{{^isAlias}}{{#isArrayModel}}{{>modelTypeArray}}{{/isArrayModel}}{{^isArrayModel}}{{>modelTypeAlias}}{{/isArrayModel}}{{/isAlias}}{{/discriminator}}{{/isEnum}}
{{/model}}
{{/models}}

View File

@ -1,6 +1,6 @@
type alias {{classname}} =
{ {{#vars}}{{^-first}} , {{/-first}}{{name}} : {{^required}}Maybe {{#isContainer}}({{/isContainer}}{{/required}}{{#isContainer}}List {{/isContainer}}{{#isEnum}}{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{dataType}}{{/isEnum}}{{^required}}{{#isContainer}}){{/isContainer}}{{/required}}
{{/vars}} }
{ {{#allVars}}{{^-first}} , {{/-first}}{{name}} : {{^required}}Maybe {{#isContainer}}({{/isContainer}}{{/required}}{{#isContainer}}List {{/isContainer}}{{#isEnum}}{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{dataType}}{{/isEnum}}{{^required}}{{#isContainer}}){{/isContainer}}{{/required}}
{{/allVars}} }
{{#vars}}
{{#isEnum}}

View File

@ -2,11 +2,11 @@ type alias {{classname}} =
{{parent}}
{{classVarName}}Decoder : Decoder {{classname}}
{{classVarName}}Decoder =
decoder : Decoder {{classname}}
decoder =
Decode.list {{vendorExtensions.elmDecoder}}
{{classVarName}}Encoder : {{classname}} -> Encode.Value
{{classVarName}}Encoder items =
encoder : {{classname}} -> Encode.Value
encoder items =
Encode.list {{#isElm018}}(List.map {{/isElm018}}{{vendorExtensions.elmEncoder}} items{{#isElm018}}){{/isElm018}}

View File

@ -1,34 +1,32 @@
type {{classname}}
{{#children}}
{{#-first}}={{/-first}}{{^-first}}|{{/-first}} {{classname}}Type {{classname}}
{{/children}}
{{#mappedModels}}
{{#-first}}={{/-first}}{{^-first}}|{{/-first}} {{modelName}}Type {{modelName}}
{{/mappedModels}}
{{classVarName}}Decoder : Decoder {{classname}}
{{classVarName}}Decoder =
Decode.field "{{{discriminatorName}}}" Decode.string
|> Decode.andThen {{classVarName}}TypeDecoder
decoder : Decoder {{classname}}
decoder =
Decode.field "{{{discriminator.propertyName}}}" Decode.string
|> Decode.andThen {{classVarName}}Decoder
{{classVarName}}TypeDecoder : String -> Decoder {{classname}}
{{classVarName}}TypeDecoder {{{discriminatorName}}} =
case {{{discriminatorName}}} of
{{#children}}
"{{vendorExtensions.x-discriminator-value}}" ->
Decode.map {{classname}}Type {{classVarName}}Decoder
{{classVarName}}Decoder : String -> Decoder {{classname}}
{{classVarName}}Decoder tag =
case tag of
{{#mappedModels}}
"{{mappingName}}" ->
Decode.map {{modelName}}Type {{modelName}}.decoder
{{/children}}
{{/mappedModels}}
_ ->
Decode.fail <|
"Trying to decode {{classname}}, but {{{discriminatorName}}} "
++ {{{discriminatorName}}} ++ " is not supported."
Decode.fail <| "Trying to decode {{classname}}, but {{{discriminatorName}}} " ++ tag ++ " is not supported."
{{classVarName}}Encoder : {{classname}} -> Encode.Value
{{classVarName}}Encoder model =
encoder : {{classname}} -> Encode.Value
encoder model =
case model of
{{#children}}
{{classname}}Type subModel ->
{{classVarName}}Encoder subModel
{{#mappedModels}}
{{modelName}}Type subModel ->
{{modelName}}.encoder "{{mappingName}}" subModel
{{/children}}
{{/mappedModels}}

View File

@ -2,11 +2,11 @@ type alias {{classname}}
= {{dataType}}
{{classVarName}}Decoder : Decoder {{classname}}
{{classVarName}}Decoder =
decoder : Decoder {{classname}}
decoder =
{{vendorExtensions.elmDecoder}}
{{classVarName}}Encoder : {{classname}} -> Encode.Value
{{classVarName}}Encoder =
encoder : {{classname}} -> Encode.Value
encoder =
{{vendorExtensions.elmEncoder}}

View File

@ -1,15 +1,16 @@
{{vendorExtensions.elmDecoder}} : Decoder {{vendorExtensions.elmUnionType}}
{{vendorExtensions.elmDecoder}} =
Decode.string
|> Decode.andThen (\str ->
case str of
|> Decode.andThen
(\str ->
case str of
{{#allowableValues}}
{{#enumVars}}
{{{value}}} ->
Decode.succeed {{name}}
{{{value}}} ->
Decode.succeed {{name}}
{{/enumVars}}
{{/allowableValues}}
other ->
Decode.fail <| "Unknown type: " ++ other
)
other ->
Decode.fail <| "Unknown type: " ++ other
)

View File

@ -10,12 +10,11 @@
-}
module Data.ApiResponse exposing (ApiResponse, apiResponseDecoder, apiResponseEncoder)
module Data.ApiResponse exposing (ApiResponse, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (decode, optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| Describes the result of uploading an image resource
@ -27,18 +26,18 @@ type alias ApiResponse =
}
apiResponseDecoder : Decoder ApiResponse
apiResponseDecoder =
decoder : Decoder ApiResponse
decoder =
decode ApiResponse
|> optional "code" (Decode.nullable Decode.int) Nothing
|> optional "type" (Decode.nullable Decode.string) Nothing
|> optional "message" (Decode.nullable Decode.string) Nothing
apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model =
encoder : ApiResponse -> Encode.Value
encoder model =
Encode.object
[ ( "code", withDefault Encode.null (map Encode.int model.code) )
, ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) )
[ ( "code", Maybe.withDefault Encode.null (Maybe.map Encode.int model.code) )
, ( "type", Maybe.withDefault Encode.null (Maybe.map Encode.string model.type_) )
, ( "message", Maybe.withDefault Encode.null (Maybe.map Encode.string model.message) )
]

View File

@ -10,12 +10,11 @@
-}
module Data.Category exposing (Category, categoryDecoder, categoryEncoder)
module Data.Category exposing (Category, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (decode, optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A category for a pet
@ -26,16 +25,16 @@ type alias Category =
}
categoryDecoder : Decoder Category
categoryDecoder =
decoder : Decoder Category
decoder =
decode Category
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "name" (Decode.nullable Decode.string) Nothing
categoryEncoder : Category -> Encode.Value
categoryEncoder model =
encoder : Category -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "name", Maybe.withDefault Encode.null (Maybe.map Encode.string model.name) )
]

View File

@ -10,13 +10,12 @@
-}
module Data.Order_ exposing (Order_, Status(..), orderDecoder, orderEncoder)
module Data.Order_ exposing (Order_, Status(..), decoder, encoder)
import DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
import DateTime exposing (DateTime)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (decode, optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| An order for a pets from the pet store
@ -37,26 +36,26 @@ type Status
| Delivered
orderDecoder : Decoder Order_
orderDecoder =
decoder : Decoder Order_
decoder =
decode Order_
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "petId" (Decode.nullable Decode.int) Nothing
|> optional "quantity" (Decode.nullable Decode.int) Nothing
|> optional "shipDate" (Decode.nullable dateTimeDecoder) Nothing
|> optional "shipDate" (Decode.nullable DateTime.decoder) Nothing
|> optional "status" (Decode.nullable statusDecoder) Nothing
|> optional "complete" (Decode.nullable Decode.bool) (Just False)
orderEncoder : Order_ -> Encode.Value
orderEncoder model =
encoder : Order_ -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "petId", withDefault Encode.null (map Encode.int model.petId) )
, ( "quantity", withDefault Encode.null (map Encode.int model.quantity) )
, ( "shipDate", withDefault Encode.null (map dateTimeEncoder model.shipDate) )
, ( "status", withDefault Encode.null (map statusEncoder model.status) )
, ( "complete", withDefault Encode.null (map Encode.bool model.complete) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "petId", Maybe.withDefault Encode.null (Maybe.map Encode.int model.petId) )
, ( "quantity", Maybe.withDefault Encode.null (Maybe.map Encode.int model.quantity) )
, ( "shipDate", Maybe.withDefault Encode.null (Maybe.map DateTime.encoder model.shipDate) )
, ( "status", Maybe.withDefault Encode.null (Maybe.map statusEncoder model.status) )
, ( "complete", Maybe.withDefault Encode.null (Maybe.map Encode.bool model.complete) )
]

View File

@ -10,14 +10,13 @@
-}
module Data.Pet exposing (Pet, Status(..), petDecoder, petEncoder)
module Data.Pet exposing (Pet, Status(..), decoder, encoder)
import Data.Category exposing (Category, categoryDecoder, categoryEncoder)
import Data.Tag exposing (Tag, tagDecoder, tagEncoder)
import Data.Category as Category exposing (Category)
import Data.Tag as Tag exposing (Tag)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (decode, optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A pet for sale in the pet store
@ -38,26 +37,26 @@ type Status
| Sold
petDecoder : Decoder Pet
petDecoder =
decoder : Decoder Pet
decoder =
decode Pet
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "category" (Decode.nullable categoryDecoder) Nothing
|> optional "category" (Decode.nullable Category.decoder) Nothing
|> required "name" Decode.string
|> required "photoUrls" (Decode.list Decode.string)
|> optional "tags" (Decode.nullable (Decode.list tagDecoder)) Nothing
|> optional "tags" (Decode.nullable (Decode.list Tag.decoder)) Nothing
|> optional "status" (Decode.nullable statusDecoder) Nothing
petEncoder : Pet -> Encode.Value
petEncoder model =
encoder : Pet -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "category", withDefault Encode.null (map categoryEncoder model.category) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "category", Maybe.withDefault Encode.null (Maybe.map Category.encoder model.category) )
, ( "name", Encode.string model.name )
, ( "photoUrls", (Encode.list << List.map Encode.string) model.photoUrls )
, ( "tags", withDefault Encode.null (map (Encode.list << List.map tagEncoder) model.tags) )
, ( "status", withDefault Encode.null (map statusEncoder model.status) )
, ( "tags", Maybe.withDefault Encode.null (Maybe.map (Encode.list << List.map Tag.encoder) model.tags) )
, ( "status", Maybe.withDefault Encode.null (Maybe.map statusEncoder model.status) )
]

View File

@ -10,12 +10,11 @@
-}
module Data.Tag exposing (Tag, tagDecoder, tagEncoder)
module Data.Tag exposing (Tag, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (decode, optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A tag for a pet
@ -26,16 +25,16 @@ type alias Tag =
}
tagDecoder : Decoder Tag
tagDecoder =
decoder : Decoder Tag
decoder =
decode Tag
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "name" (Decode.nullable Decode.string) Nothing
tagEncoder : Tag -> Encode.Value
tagEncoder model =
encoder : Tag -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "name", Maybe.withDefault Encode.null (Maybe.map Encode.string model.name) )
]

View File

@ -10,12 +10,11 @@
-}
module Data.User exposing (User, userDecoder, userEncoder)
module Data.User exposing (User, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (decode, optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A User who is purchasing from the pet store
@ -32,8 +31,8 @@ type alias User =
}
userDecoder : Decoder User
userDecoder =
decoder : Decoder User
decoder =
decode User
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "username" (Decode.nullable Decode.string) Nothing
@ -45,15 +44,15 @@ userDecoder =
|> optional "userStatus" (Decode.nullable Decode.int) Nothing
userEncoder : User -> Encode.Value
userEncoder model =
encoder : User -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "username", withDefault Encode.null (map Encode.string model.username) )
, ( "firstName", withDefault Encode.null (map Encode.string model.firstName) )
, ( "lastName", withDefault Encode.null (map Encode.string model.lastName) )
, ( "email", withDefault Encode.null (map Encode.string model.email) )
, ( "password", withDefault Encode.null (map Encode.string model.password) )
, ( "phone", withDefault Encode.null (map Encode.string model.phone) )
, ( "userStatus", withDefault Encode.null (map Encode.int model.userStatus) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "username", Maybe.withDefault Encode.null (Maybe.map Encode.string model.username) )
, ( "firstName", Maybe.withDefault Encode.null (Maybe.map Encode.string model.firstName) )
, ( "lastName", Maybe.withDefault Encode.null (Maybe.map Encode.string model.lastName) )
, ( "email", Maybe.withDefault Encode.null (Maybe.map Encode.string model.email) )
, ( "password", Maybe.withDefault Encode.null (Maybe.map Encode.string model.password) )
, ( "phone", Maybe.withDefault Encode.null (Maybe.map Encode.string model.phone) )
, ( "userStatus", Maybe.withDefault Encode.null (Maybe.map Encode.int model.userStatus) )
]

View File

@ -1,4 +1,4 @@
module DateOnly exposing (DateOnly, dateOnlyDecoder, dateOnlyEncoder)
module DateOnly exposing (DateOnly, decoder, encoder)
import Date
import Date.Extra exposing (fromIsoString, toFormattedString)
@ -11,14 +11,14 @@ type alias DateOnly =
Date.Date
dateOnlyDecoder : Decoder DateOnly
dateOnlyDecoder =
decoder : Decoder DateOnly
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateOnlyEncoder : DateOnly -> Encode.Value
dateOnlyEncoder model =
encoder : DateOnly -> Encode.Value
encoder model =
Encode.string <| toFormattedString "yyyy-MM-dd" model

View File

@ -1,4 +1,4 @@
module DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
module DateTime exposing (DateTime, decoder, encoder)
import Date
import Date.Extra exposing (fromIsoString, toIsoString)
@ -11,14 +11,14 @@ type alias DateTime =
Date.Date
dateTimeDecoder : Decoder DateTime
dateTimeDecoder =
decoder : Decoder DateTime
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateTimeEncoder : DateTime -> Encode.Value
dateTimeEncoder model =
encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| toIsoString model

View File

@ -12,8 +12,8 @@
module Request.Pet exposing (addPet, deletePet, findPetsByStatus, findPetsByTags, getPetById, updatePet, updatePetWithForm, uploadFile)
import Data.ApiResponse exposing (ApiResponse, apiResponseDecoder)
import Data.Pet exposing (Pet, petDecoder, petEncoder)
import Data.ApiResponse as ApiResponse exposing (ApiResponse)
import Data.Pet as Pet exposing (Pet)
import Dict
import Http
import Json.Decode as Decode
@ -29,7 +29,7 @@ addPet model =
{ method = "POST"
, url = basePath ++ "/pet"
, headers = []
, body = Http.jsonBody <| petEncoder model
, body = Http.jsonBody <| Pet.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -58,7 +58,7 @@ findPetsByStatus =
, url = basePath ++ "/pet/findByStatus"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson (Decode.list petDecoder)
, expect = Http.expectJson (Decode.list Pet.decoder)
, timeout = Just 30000
, withCredentials = False
}
@ -73,7 +73,7 @@ findPetsByTags =
, url = basePath ++ "/pet/findByTags"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson (Decode.list petDecoder)
, expect = Http.expectJson (Decode.list Pet.decoder)
, timeout = Just 30000
, withCredentials = False
}
@ -88,7 +88,7 @@ getPetById petId =
, url = basePath ++ "/pet/" ++ toString petId
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson petDecoder
, expect = Http.expectJson Pet.decoder
, timeout = Just 30000
, withCredentials = False
}
@ -100,7 +100,7 @@ updatePet model =
{ method = "PUT"
, url = basePath ++ "/pet"
, headers = []
, body = Http.jsonBody <| petEncoder model
, body = Http.jsonBody <| Pet.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -127,7 +127,7 @@ uploadFile petId =
, url = basePath ++ "/pet/" ++ toString petId ++ "/uploadImage"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson apiResponseDecoder
, expect = Http.expectJson ApiResponse.decoder
, timeout = Just 30000
, withCredentials = False
}

View File

@ -12,7 +12,7 @@
module Request.Store exposing (deleteOrder, getInventory, getOrderById, placeOrder)
import Data.Order_ exposing (Order_, orderDecoder, orderEncoder)
import Data.Order_ as Order_ exposing (Order_)
import Dict
import Http
import Json.Decode as Decode
@ -61,7 +61,7 @@ getOrderById orderId =
, url = basePath ++ "/store/order/" ++ toString orderId
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson orderDecoder
, expect = Http.expectJson Order_.decoder
, timeout = Just 30000
, withCredentials = False
}
@ -73,8 +73,8 @@ placeOrder model =
{ method = "POST"
, url = basePath ++ "/store/order"
, headers = []
, body = Http.jsonBody <| orderEncoder model
, expect = Http.expectJson orderDecoder
, body = Http.jsonBody <| Order_.encoder model
, expect = Http.expectJson Order_.decoder
, timeout = Just 30000
, withCredentials = False
}

View File

@ -12,7 +12,7 @@
module Request.User exposing (createUser, createUsersWithArrayInput, createUsersWithListInput, deleteUser, getUserByName, loginUser, logoutUser, updateUser)
import Data.User exposing (User, userDecoder, userEncoder)
import Data.User as User exposing (User)
import Dict
import Http
import Json.Decode as Decode
@ -30,7 +30,7 @@ createUser model =
{ method = "POST"
, url = basePath ++ "/user"
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -43,7 +43,7 @@ createUsersWithArrayInput model =
{ method = "POST"
, url = basePath ++ "/user/createWithArray"
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -56,7 +56,7 @@ createUsersWithListInput model =
{ method = "POST"
, url = basePath ++ "/user/createWithList"
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -85,7 +85,7 @@ getUserByName username =
, url = basePath ++ "/user/" ++ username
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson userDecoder
, expect = Http.expectJson User.decoder
, timeout = Just 30000
, withCredentials = False
}
@ -125,7 +125,7 @@ updateUser username model =
{ method = "PUT"
, url = basePath ++ "/user/" ++ username
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False

View File

@ -10,12 +10,11 @@
-}
module Data.ApiResponse exposing (ApiResponse, apiResponseDecoder, apiResponseEncoder)
module Data.ApiResponse exposing (ApiResponse, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| Describes the result of uploading an image resource
@ -27,18 +26,18 @@ type alias ApiResponse =
}
apiResponseDecoder : Decoder ApiResponse
apiResponseDecoder =
decoder : Decoder ApiResponse
decoder =
Decode.succeed ApiResponse
|> optional "code" (Decode.nullable Decode.int) Nothing
|> optional "type" (Decode.nullable Decode.string) Nothing
|> optional "message" (Decode.nullable Decode.string) Nothing
apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model =
encoder : ApiResponse -> Encode.Value
encoder model =
Encode.object
[ ( "code", withDefault Encode.null (map Encode.int model.code) )
, ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) )
[ ( "code", Maybe.withDefault Encode.null (Maybe.map Encode.int model.code) )
, ( "type", Maybe.withDefault Encode.null (Maybe.map Encode.string model.type_) )
, ( "message", Maybe.withDefault Encode.null (Maybe.map Encode.string model.message) )
]

View File

@ -10,12 +10,11 @@
-}
module Data.Category exposing (Category, categoryDecoder, categoryEncoder)
module Data.Category exposing (Category, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A category for a pet
@ -26,16 +25,16 @@ type alias Category =
}
categoryDecoder : Decoder Category
categoryDecoder =
decoder : Decoder Category
decoder =
Decode.succeed Category
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "name" (Decode.nullable Decode.string) Nothing
categoryEncoder : Category -> Encode.Value
categoryEncoder model =
encoder : Category -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "name", Maybe.withDefault Encode.null (Maybe.map Encode.string model.name) )
]

View File

@ -10,13 +10,12 @@
-}
module Data.Order_ exposing (Order_, Status(..), orderDecoder, orderEncoder)
module Data.Order_ exposing (Order_, Status(..), decoder, encoder)
import DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
import DateTime exposing (DateTime)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| An order for a pets from the pet store
@ -37,26 +36,26 @@ type Status
| Delivered
orderDecoder : Decoder Order_
orderDecoder =
decoder : Decoder Order_
decoder =
Decode.succeed Order_
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "petId" (Decode.nullable Decode.int) Nothing
|> optional "quantity" (Decode.nullable Decode.int) Nothing
|> optional "shipDate" (Decode.nullable dateTimeDecoder) Nothing
|> optional "shipDate" (Decode.nullable DateTime.decoder) Nothing
|> optional "status" (Decode.nullable statusDecoder) Nothing
|> optional "complete" (Decode.nullable Decode.bool) (Just False)
orderEncoder : Order_ -> Encode.Value
orderEncoder model =
encoder : Order_ -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "petId", withDefault Encode.null (map Encode.int model.petId) )
, ( "quantity", withDefault Encode.null (map Encode.int model.quantity) )
, ( "shipDate", withDefault Encode.null (map dateTimeEncoder model.shipDate) )
, ( "status", withDefault Encode.null (map statusEncoder model.status) )
, ( "complete", withDefault Encode.null (map Encode.bool model.complete) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "petId", Maybe.withDefault Encode.null (Maybe.map Encode.int model.petId) )
, ( "quantity", Maybe.withDefault Encode.null (Maybe.map Encode.int model.quantity) )
, ( "shipDate", Maybe.withDefault Encode.null (Maybe.map DateTime.encoder model.shipDate) )
, ( "status", Maybe.withDefault Encode.null (Maybe.map statusEncoder model.status) )
, ( "complete", Maybe.withDefault Encode.null (Maybe.map Encode.bool model.complete) )
]

View File

@ -10,14 +10,13 @@
-}
module Data.Pet exposing (Pet, Status(..), petDecoder, petEncoder)
module Data.Pet exposing (Pet, Status(..), decoder, encoder)
import Data.Category exposing (Category, categoryDecoder, categoryEncoder)
import Data.Tag exposing (Tag, tagDecoder, tagEncoder)
import Data.Category as Category exposing (Category)
import Data.Tag as Tag exposing (Tag)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A pet for sale in the pet store
@ -38,26 +37,26 @@ type Status
| Sold
petDecoder : Decoder Pet
petDecoder =
decoder : Decoder Pet
decoder =
Decode.succeed Pet
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "category" (Decode.nullable categoryDecoder) Nothing
|> optional "category" (Decode.nullable Category.decoder) Nothing
|> required "name" Decode.string
|> required "photoUrls" (Decode.list Decode.string)
|> optional "tags" (Decode.nullable (Decode.list tagDecoder)) Nothing
|> optional "tags" (Decode.nullable (Decode.list Tag.decoder)) Nothing
|> optional "status" (Decode.nullable statusDecoder) Nothing
petEncoder : Pet -> Encode.Value
petEncoder model =
encoder : Pet -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "category", withDefault Encode.null (map categoryEncoder model.category) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "category", Maybe.withDefault Encode.null (Maybe.map Category.encoder model.category) )
, ( "name", Encode.string model.name )
, ( "photoUrls", Encode.list Encode.string model.photoUrls )
, ( "tags", withDefault Encode.null (map (Encode.list tagEncoder) model.tags) )
, ( "status", withDefault Encode.null (map statusEncoder model.status) )
, ( "tags", Maybe.withDefault Encode.null (Maybe.map (Encode.list Tag.encoder) model.tags) )
, ( "status", Maybe.withDefault Encode.null (Maybe.map statusEncoder model.status) )
]

View File

@ -10,12 +10,11 @@
-}
module Data.Tag exposing (Tag, tagDecoder, tagEncoder)
module Data.Tag exposing (Tag, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A tag for a pet
@ -26,16 +25,16 @@ type alias Tag =
}
tagDecoder : Decoder Tag
tagDecoder =
decoder : Decoder Tag
decoder =
Decode.succeed Tag
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "name" (Decode.nullable Decode.string) Nothing
tagEncoder : Tag -> Encode.Value
tagEncoder model =
encoder : Tag -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "name", Maybe.withDefault Encode.null (Maybe.map Encode.string model.name) )
]

View File

@ -10,12 +10,11 @@
-}
module Data.User exposing (User, userDecoder, userEncoder)
module Data.User exposing (User, decoder, encoder)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)
{-| A User who is purchasing from the pet store
@ -32,8 +31,8 @@ type alias User =
}
userDecoder : Decoder User
userDecoder =
decoder : Decoder User
decoder =
Decode.succeed User
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "username" (Decode.nullable Decode.string) Nothing
@ -45,15 +44,15 @@ userDecoder =
|> optional "userStatus" (Decode.nullable Decode.int) Nothing
userEncoder : User -> Encode.Value
userEncoder model =
encoder : User -> Encode.Value
encoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "username", withDefault Encode.null (map Encode.string model.username) )
, ( "firstName", withDefault Encode.null (map Encode.string model.firstName) )
, ( "lastName", withDefault Encode.null (map Encode.string model.lastName) )
, ( "email", withDefault Encode.null (map Encode.string model.email) )
, ( "password", withDefault Encode.null (map Encode.string model.password) )
, ( "phone", withDefault Encode.null (map Encode.string model.phone) )
, ( "userStatus", withDefault Encode.null (map Encode.int model.userStatus) )
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "username", Maybe.withDefault Encode.null (Maybe.map Encode.string model.username) )
, ( "firstName", Maybe.withDefault Encode.null (Maybe.map Encode.string model.firstName) )
, ( "lastName", Maybe.withDefault Encode.null (Maybe.map Encode.string model.lastName) )
, ( "email", Maybe.withDefault Encode.null (Maybe.map Encode.string model.email) )
, ( "password", Maybe.withDefault Encode.null (Maybe.map Encode.string model.password) )
, ( "phone", Maybe.withDefault Encode.null (Maybe.map Encode.string model.phone) )
, ( "userStatus", Maybe.withDefault Encode.null (Maybe.map Encode.int model.userStatus) )
]

View File

@ -1,4 +1,4 @@
module DateOnly exposing (DateOnly, dateOnlyDecoder, dateOnlyEncoder)
module DateOnly exposing (DateOnly, decoder, encoder)
import Iso8601
import Json.Decode as Decode exposing (Decoder)
@ -11,14 +11,14 @@ type alias DateOnly =
Time.Posix
dateOnlyDecoder : Decoder DateOnly
dateOnlyDecoder =
decoder : Decoder DateOnly
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateOnlyEncoder : DateOnly -> Encode.Value
dateOnlyEncoder model =
encoder : DateOnly -> Encode.Value
encoder model =
Iso8601.fromTime model
|> String.left 10
|> Encode.string

View File

@ -1,4 +1,4 @@
module DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
module DateTime exposing (DateTime, decoder, encoder)
import Iso8601
import Json.Decode as Decode exposing (Decoder)
@ -11,14 +11,14 @@ type alias DateTime =
Time.Posix
dateTimeDecoder : Decoder DateTime
dateTimeDecoder =
decoder : Decoder DateTime
decoder =
Decode.string
|> Decode.andThen decodeIsoString
dateTimeEncoder : DateTime -> Encode.Value
dateTimeEncoder model =
encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| Iso8601.fromTime model

View File

@ -12,8 +12,8 @@
module Request.Pet exposing (addPet, deletePet, findPetsByStatus, findPetsByTags, getPetById, updatePet, updatePetWithForm, uploadFile)
import Data.ApiResponse exposing (ApiResponse, apiResponseDecoder)
import Data.Pet exposing (Pet, petDecoder, petEncoder)
import Data.ApiResponse as ApiResponse exposing (ApiResponse)
import Data.Pet as Pet exposing (Pet)
import Dict
import Http
import Json.Decode as Decode
@ -29,7 +29,7 @@ addPet model =
{ method = "POST"
, url = basePath ++ "/pet"
, headers = []
, body = Http.jsonBody <| petEncoder model
, body = Http.jsonBody <| Pet.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -58,7 +58,7 @@ findPetsByStatus =
, url = basePath ++ "/pet/findByStatus"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson (Decode.list petDecoder)
, expect = Http.expectJson (Decode.list Pet.decoder)
, timeout = Just 30000
, withCredentials = False
}
@ -73,7 +73,7 @@ findPetsByTags =
, url = basePath ++ "/pet/findByTags"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson (Decode.list petDecoder)
, expect = Http.expectJson (Decode.list Pet.decoder)
, timeout = Just 30000
, withCredentials = False
}
@ -88,7 +88,7 @@ getPetById petId =
, url = basePath ++ "/pet/" ++ String.fromInt petId
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson petDecoder
, expect = Http.expectJson Pet.decoder
, timeout = Just 30000
, withCredentials = False
}
@ -100,7 +100,7 @@ updatePet model =
{ method = "PUT"
, url = basePath ++ "/pet"
, headers = []
, body = Http.jsonBody <| petEncoder model
, body = Http.jsonBody <| Pet.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -127,7 +127,7 @@ uploadFile petId =
, url = basePath ++ "/pet/" ++ String.fromInt petId ++ "/uploadImage"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson apiResponseDecoder
, expect = Http.expectJson ApiResponse.decoder
, timeout = Just 30000
, withCredentials = False
}

View File

@ -12,7 +12,7 @@
module Request.Store exposing (deleteOrder, getInventory, getOrderById, placeOrder)
import Data.Order_ exposing (Order_, orderDecoder, orderEncoder)
import Data.Order_ as Order_ exposing (Order_)
import Dict
import Http
import Json.Decode as Decode
@ -61,7 +61,7 @@ getOrderById orderId =
, url = basePath ++ "/store/order/" ++ String.fromInt orderId
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson orderDecoder
, expect = Http.expectJson Order_.decoder
, timeout = Just 30000
, withCredentials = False
}
@ -73,8 +73,8 @@ placeOrder model =
{ method = "POST"
, url = basePath ++ "/store/order"
, headers = []
, body = Http.jsonBody <| orderEncoder model
, expect = Http.expectJson orderDecoder
, body = Http.jsonBody <| Order_.encoder model
, expect = Http.expectJson Order_.decoder
, timeout = Just 30000
, withCredentials = False
}

View File

@ -12,7 +12,7 @@
module Request.User exposing (createUser, createUsersWithArrayInput, createUsersWithListInput, deleteUser, getUserByName, loginUser, logoutUser, updateUser)
import Data.User exposing (User, userDecoder, userEncoder)
import Data.User as User exposing (User)
import Dict
import Http
import Json.Decode as Decode
@ -30,7 +30,7 @@ createUser model =
{ method = "POST"
, url = basePath ++ "/user"
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -43,7 +43,7 @@ createUsersWithArrayInput model =
{ method = "POST"
, url = basePath ++ "/user/createWithArray"
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -56,7 +56,7 @@ createUsersWithListInput model =
{ method = "POST"
, url = basePath ++ "/user/createWithList"
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False
@ -85,7 +85,7 @@ getUserByName username =
, url = basePath ++ "/user/" ++ username
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson userDecoder
, expect = Http.expectJson User.decoder
, timeout = Just 30000
, withCredentials = False
}
@ -125,7 +125,7 @@ updateUser username model =
{ method = "PUT"
, url = basePath ++ "/user/" ++ username
, headers = []
, body = Http.jsonBody <| userEncoder model
, body = Http.jsonBody <| User.encoder model
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Just 30000
, withCredentials = False