Skip to content

Commit

Permalink
Fix param conversion to string
Browse files Browse the repository at this point in the history
  • Loading branch information
miniBill committed Nov 13, 2024
1 parent ce48d9a commit bd62889
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
19 changes: 14 additions & 5 deletions src/Cli.elm
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,9 @@ defaultFormats =

dateTimeFormat : CliMonad.Format
dateTimeFormat =
{ basicType = Common.String
, format = "date-time"
, annotation = Gen.Time.annotation_.posix
, encode =
\instant ->
let
toString : Elm.Expression -> Elm.Expression
toString instant =
Gen.Rfc3339.make_.dateTimeOffset
(Elm.record
[ ( "instant", instant )
Expand All @@ -709,6 +707,14 @@ dateTimeFormat =
]
)
|> Gen.Rfc3339.toString
in
{ basicType = Common.String
, format = "date-time"
, annotation = Gen.Time.annotation_.posix
, encode =
\instant ->
instant
|> toString
|> Gen.Json.Encode.call_.string
, decoder =
Gen.Json.Decode.string
Expand All @@ -722,6 +728,7 @@ dateTimeFormat =
, err = \_ -> Gen.Json.Decode.fail "Invalid RFC-3339 date-time"
}
)
, toParamString = toString
, sharedDeclarations = []
, requiresPackages = []
}
Expand All @@ -737,6 +744,7 @@ dateFormat =
date
|> Gen.Date.toIsoString
|> Gen.Json.Encode.call_.string
, toParamString = Gen.Date.toIsoString
, decoder =
Gen.Json.Decode.string
|> Gen.Json.Decode.andThen
Expand All @@ -759,6 +767,7 @@ defaultStringFormat format =
, annotation = Elm.Annotation.string
, encode = Gen.Json.Encode.call_.string
, decoder = Gen.Json.Decode.string
, toParamString = identity
, sharedDeclarations = []
, requiresPackages = []
}
Expand Down
4 changes: 4 additions & 0 deletions src/CliMonad.elm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type alias Format =
, format : String
, encode : Elm.Expression -> Elm.Expression
, decoder : Elm.Expression
, toParamString : Elm.Expression -> Elm.Expression
, annotation : Elm.Annotation.Annotation
, sharedDeclarations : List Elm.Declaration
, requiresPackages : List String
Expand All @@ -67,6 +68,7 @@ type alias Format =
type alias InternalFormat =
{ encode : Elm.Expression -> Elm.Expression
, decoder : Elm.Expression
, toParamString : Elm.Expression -> Elm.Expression
, annotation : Elm.Annotation.Annotation
, sharedDeclarations : List Elm.Declaration
, requiresPackages : List String
Expand Down Expand Up @@ -265,6 +267,7 @@ toInternalFormat format =
( ( Common.basicTypeToString format.basicType, format.format )
, { encode = format.encode
, decoder = format.decoder
, toParamString = format.toParamString
, annotation = format.annotation
, sharedDeclarations = format.sharedDeclarations
, requiresPackages = format.requiresPackages
Expand Down Expand Up @@ -360,6 +363,7 @@ withFormat :
->
({ encode : Elm.Expression -> Elm.Expression
, decoder : Elm.Expression
, toParamString : Elm.Expression -> Elm.Expression
, annotation : Elm.Annotation.Annotation
, sharedDeclarations : List Elm.Declaration
, requiresPackages : List String
Expand Down
32 changes: 18 additions & 14 deletions src/OpenApi/Generate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2078,25 +2078,29 @@ paramToString qualify type_ =
(Elm.string "false")
in
case type_ of
Common.Basic basicType _ ->
CliMonad.succeed
{ inputToString = basicTypeToString basicType
, alwaysJust = True
, isMaybe = False
}

Common.Nullable (Common.Basic basicType _) ->
{ inputToString = basicTypeToString basicType
, alwaysJust = False
, isMaybe = True
}
|> CliMonad.succeed
Common.Basic basicType basic ->
CliMonad.withFormat basicType
basic.format
.toParamString
(basicTypeToString basicType)
|> CliMonad.map
(\inputToString ->
{ inputToString = inputToString
, alwaysJust = True
, isMaybe = False
}
)

Common.Nullable p ->
recursive p True <|
\{ inputToString, alwaysJust } val ->
if alwaysJust then
Gen.Maybe.call_.map inputToString val
case p of
Common.Basic Common.String _ ->
val

_ ->
Gen.Maybe.call_.map inputToString val

else
Gen.Maybe.call_.andThen inputToString val
Expand Down

0 comments on commit bd62889

Please sign in to comment.