diff --git a/frontend/src/Core/Decoder.elm b/frontend/src/Core/Decoder.elm index ca8bdbe..8b269ba 100644 --- a/frontend/src/Core/Decoder.elm +++ b/frontend/src/Core/Decoder.elm @@ -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 diff --git a/frontend/src/Core/Encoder.elm b/frontend/src/Core/Encoder.elm index ace7983..dddf3a0 100644 --- a/frontend/src/Core/Encoder.elm +++ b/frontend/src/Core/Encoder.elm @@ -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) + ] diff --git a/frontend/src/Core/Types.elm b/frontend/src/Core/Types.elm index 5ba8f5c..54373cc 100644 --- a/frontend/src/Core/Types.elm +++ b/frontend/src/Core/Types.elm @@ -119,3 +119,8 @@ type alias OneType = , userRequest : UserRequest , nonEmpty : (MyUnit, List MyUnit) } + +type alias CustomCodeGen = + { customFunTestString : String + , customFunTestInt : Int + } diff --git a/frontend/tests/Tests/Golden.elm b/frontend/tests/Tests/Golden.elm index 2267b0d..4ee1d53 100644 --- a/frontend/tests/Tests/Golden.elm +++ b/frontend/tests/Tests/Golden.elm @@ -36,7 +36,7 @@ goldenOneTypeJson = "bool": true, "unit": [], "nonEmpty": [1], - "value" : { + "value": { "boolField": true, "numberField": 1, "stringField": "hi", @@ -67,9 +67,9 @@ goldenOneTypeJson = "limit": 123 }, "age": 18, - "newtype": 666, + "newtype": 666, "newtypeList": [123], - "oneConstructor": "OneConstructor", + "oneConstructor": "OneConstructor", "user": { "status": "Approved", "tag": "User", diff --git a/types/Types.hs b/types/Types.hs index 890e4a1..ebf33dc 100644 --- a/types/Types.hs +++ b/types/Types.hs @@ -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) @@ -86,20 +86,14 @@ 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) @@ -107,10 +101,7 @@ data User = User , 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 @@ -118,20 +109,14 @@ data Guest | 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) @@ -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 @@ -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 @@ -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} @@ -207,6 +185,7 @@ type Types = , Guest , UserRequest , OneType + , CustomCodeGen ]