Skip to content

Commit

Permalink
Export to elm, simplify Types.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrcek committed Aug 2, 2023
1 parent 5326d05 commit 0fdca95
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
5 changes: 5 additions & 0 deletions frontend/src/Core/Decoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ decodeOneType = D.succeed T.OneType
|> required "guests" (D.list decodeGuest)
|> required "userRequest" decodeUserRequest
|> required "nonEmpty" (elmStreetDecodeNonEmpty decodeMyUnit)

decodeCustomCodeGen : Decoder T.CustomCodeGen
decodeCustomCodeGen = D.succeed T.CustomCodeGen
|> required "customFunTestString" D.string
|> required "customFunTestInt" D.int
7 changes: 7 additions & 0 deletions frontend/src/Core/Encoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ encodeOneType x = E.object
, ("userRequest", encodeUserRequest x.userRequest)
, ("nonEmpty", (elmStreetEncodeNonEmpty encodeMyUnit) x.nonEmpty)
]

encodeCustomCodeGen : T.CustomCodeGen -> Value
encodeCustomCodeGen x = E.object
[ ("tag", E.string "CustomCodeGen")
, ("customFunTestString", E.string x.customFunTestString)
, ("customFunTestInt", E.int x.customFunTestInt)
]
5 changes: 5 additions & 0 deletions frontend/src/Core/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ type alias OneType =
, userRequest : UserRequest
, nonEmpty : (MyUnit, List MyUnit)
}

type alias CustomCodeGen =
{ customFunTestString : String
, customFunTestInt : Int
}
6 changes: 3 additions & 3 deletions frontend/tests/Tests/Golden.elm
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ goldenOneTypeJson =
"bool": true,
"unit": [],
"nonEmpty": [1],
"value" : {
"value": {
"boolField": true,
"numberField": 1,
"stringField": "hi",
Expand Down Expand Up @@ -67,9 +67,9 @@ goldenOneTypeJson =
"limit": 123
},
"age": 18,
"newtype": 666,
"newtype": 666,
"newtypeList": [123],
"oneConstructor": "OneConstructor",
"oneConstructor": "OneConstructor",
"user": {
"status": "Approved",
"tag": "User",
Expand Down
39 changes: 9 additions & 30 deletions types/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Data.Text (Text)
import Data.Time.Calendar (fromGregorian)
import Data.Time.Clock (UTCTime (..))
import Data.Word (Word32)
import Elm (Elm (..), ElmStreet (..), elmNewtype, elmStreetParseJson, elmStreetToJson)
import Elm (Elm (..), ElmStreet (..), elmNewtype)
import Elm.Generic (CodeGenOptions (..), ElmStreetGenericConstraints, GenericElmDefinition(..))
import Elm.Aeson (elmStreetParseJsonWith, elmStreetToJsonWith)
import GHC.Generics (Generic, Rep)
Expand Down Expand Up @@ -86,52 +86,37 @@ newtype NewtypeList = NewtypeList [Int]

data OneConstructor = OneConstructor
deriving stock (Generic, Eq, Show)
deriving anyclass (Elm)

instance ToJSON OneConstructor where toJSON = elmStreetToJson
instance FromJSON OneConstructor where parseJSON = elmStreetParseJson
deriving (Elm, FromJSON, ToJSON) via ElmStreet OneConstructor

data RequestStatus
= Approved
| Rejected
| Reviewing
deriving (Generic, Eq, Show)
deriving anyclass (Elm)

instance ToJSON RequestStatus where toJSON = elmStreetToJson
instance FromJSON RequestStatus where parseJSON = elmStreetParseJson
deriving (Elm, FromJSON, ToJSON) via ElmStreet RequestStatus

data User = User
{ userId :: !(Id User)
, userName :: !Text
, userAge :: !Age
, userStatus :: !RequestStatus
} deriving (Generic, Eq, Show)
deriving anyclass (Elm)

instance ToJSON User where toJSON = elmStreetToJson
instance FromJSON User where parseJSON = elmStreetParseJson
deriving (Elm, FromJSON, ToJSON) via ElmStreet User

data Guest
= Regular Text Int
| Visitor Text
| Special (Maybe [Int])
| Blocked
deriving (Generic, Eq, Show)
deriving anyclass (Elm)

instance ToJSON Guest where toJSON = elmStreetToJson
instance FromJSON Guest where parseJSON = elmStreetParseJson
deriving (Elm, FromJSON, ToJSON) via ElmStreet Guest

data UserRequest = UserRequest
{ userRequestIds :: ![Id User]
, userRequestLimit :: !Word32
, userRequestExample :: !(Maybe (Either User Guest))
} deriving (Generic, Eq, Show)
deriving anyclass (Elm)

instance ToJSON UserRequest where toJSON = elmStreetToJson
instance FromJSON UserRequest where parseJSON = elmStreetParseJson
deriving (Elm, FromJSON, ToJSON) via ElmStreet UserRequest

data MyUnit = MyUnit ()
deriving stock (Show, Eq, Ord, Generic)
Expand All @@ -142,10 +127,7 @@ data MyResult
= Ok
| Err Text
deriving (Generic, Eq, Show)
deriving anyclass (Elm)

instance ToJSON MyResult where toJSON = elmStreetToJson
instance FromJSON MyResult where parseJSON = elmStreetParseJson
deriving (Elm, FromJSON, ToJSON) via ElmStreet MyResult

-- | All test types together in one type to play with.
data OneType = OneType
Expand All @@ -163,10 +145,7 @@ data OneType = OneType
, oneTypeUserRequest :: !UserRequest
, oneTypeNonEmpty :: !(NonEmpty MyUnit)
} deriving (Generic, Eq, Show)
deriving anyclass (Elm)

instance ToJSON OneType where toJSON = elmStreetToJson
instance FromJSON OneType where parseJSON = elmStreetParseJson
deriving (Elm, FromJSON, ToJSON) via ElmStreet OneType

data CustomCodeGen = CustomCodeGen
{ customCodeGenString :: String
Expand All @@ -178,7 +157,6 @@ data CustomCodeGen = CustomCodeGen
customCodeGenOptions :: CodeGenOptions
customCodeGenOptions = CodeGenOptions (Text.replace "CodeGen" "FunTest")


-- Newtype whose Elm/ToJSON/FromJSON instance use custom CodeGenOptions
newtype CustomElm a = CustomElm {unCustomElm :: a}

Expand Down Expand Up @@ -207,6 +185,7 @@ type Types =
, Guest
, UserRequest
, OneType
, CustomCodeGen
]


Expand Down

0 comments on commit 0fdca95

Please sign in to comment.