Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate 'Codable' bounds through MoatData tyvars #75

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golden/kotlinGenericNewtypeSpec/golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@Parcelize
@Serializable
value class LTree<A : Parcelable>(val value: List<A>) : Parcelable
4 changes: 4 additions & 0 deletions .golden/kotlinGenericStructSpec/golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data class Tree<A : Parcelable>(
val rootLabel: A,
val subForest: List<Tree<A>>,
) : Parcelable
4 changes: 4 additions & 0 deletions .golden/swiftGenericNewtypeSpec/golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct LTree<A: Codable>: CaseIterable, Hashable, Codable {
typealias LTreeTag = Tagged<LTree, [A]>
let value: LTreeTag
}
4 changes: 4 additions & 0 deletions .golden/swiftGenericStructSpec/golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct Tree<A: Codable>: Codable {
var rootLabel: A
var subForest: [Tree<A>]
}
2 changes: 1 addition & 1 deletion .golden/swiftMultipleTypeVariableSpec/golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct Data<A, B>: CaseIterable, Hashable, Codable {
struct Data<A: Codable, B: Codable>: CaseIterable, Hashable, Codable {
var field0: A
var field1: B
}
2 changes: 1 addition & 1 deletion .golden/swiftTypeVariableSpec/golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
struct Data<A>: CaseIterable, Hashable, Codable {
struct Data<A: Codable>: CaseIterable, Hashable, Codable {
var field0: A
}
2 changes: 2 additions & 0 deletions moat.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ test-suite spec
DuplicateRecordFieldSpec
EnumValueClassDocSpec
EnumValueClassSpec
GenericNewtypeSpec
GenericStructSpec
MultipleTypeVariableSpec
StrictEnumsSpec
StrictFieldsSpec
Expand Down
13 changes: 9 additions & 4 deletions src/Moat/Pretty/Swift.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ prettySwiftDataWith indent = \case
MoatEnum {..} ->
prettyTypeDoc "" enumDoc []
++ "enum "
++ prettyMoatTypeHeader enumName enumTyVars
++ prettyMoatTypeHeader enumName (addTyVarBounds enumTyVars enumProtocols)
++ prettyRawValueAndProtocols enumRawValue enumProtocols
++ " {"
++ newlineNonEmpty enumCases
Expand All @@ -41,7 +41,7 @@ prettySwiftDataWith indent = \case
MoatStruct {..} ->
prettyTypeDoc "" structDoc []
++ "struct "
++ prettyMoatTypeHeader structName structTyVars
++ prettyMoatTypeHeader structName (addTyVarBounds structTyVars structProtocols)
++ prettyRawValueAndProtocols Nothing structProtocols
++ " {"
++ newlineNonEmpty structFields
Expand All @@ -54,13 +54,13 @@ prettySwiftDataWith indent = \case
MoatAlias {..} ->
prettyTypeDoc "" aliasDoc []
++ "typealias "
++ prettyMoatTypeHeader aliasName aliasTyVars
++ prettyMoatTypeHeader aliasName (addTyVarBounds aliasTyVars [])
++ " = "
++ prettyMoatType aliasTyp
MoatNewtype {..} ->
prettyTypeDoc "" newtypeDoc []
++ "struct "
++ prettyMoatTypeHeader newtypeName newtypeTyVars
++ prettyMoatTypeHeader newtypeName (addTyVarBounds newtypeTyVars newtypeProtocols)
++ prettyRawValueAndProtocols Nothing newtypeProtocols
++ " {\n"
++ indents
Expand Down Expand Up @@ -268,3 +268,8 @@ prettyPrivateTypes indents = go
onLast :: (a -> a) -> [a] -> [a]
onLast _ [] = []
onLast f (x : xs) = x : map f xs

addTyVarBounds :: [String] -> [Protocol] -> [String]
addTyVarBounds tyVars protos
| Codable `elem` protos = map (++ ": Codable") tyVars
| otherwise = tyVars
33 changes: 33 additions & 0 deletions test/GenericNewtypeSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{-# LANGUAGE DerivingStrategies #-}

module GenericNewtypeSpec where

import Common
import Data.List.NonEmpty (NonEmpty)
import Moat
import Test.Hspec
import Test.Hspec.Golden
import Prelude

newtype LTree a = LTree (NonEmpty a)
deriving stock (Show, Eq)

mobileGenWith
( defaultOptions
{ dataAnnotations = [Parcelize, Serializable]
, dataInterfaces = [Parcelable]
, dataProtocols = [OtherProtocol "CaseIterable", Hashable, Codable]
, dataRawValue = Just Str
, generateDocComments = False
}
)
''LTree

spec :: Spec
spec =
describe "stays golden" $ do
let moduleName = "GenericNewtypeSpec"
it "swift" $
defaultGolden ("swift" <> moduleName) (showSwift @(LTree _))
it "kotlin" $
defaultGolden ("kotlin" <> moduleName) (showKotlin @(LTree _))
28 changes: 28 additions & 0 deletions test/GenericStructSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# OPTIONS_GHC -Wno-orphans #-}

module GenericStructSpec where

import Common
import Data.Tree (Tree)
import Moat
import Test.Hspec
import Test.Hspec.Golden
import Prelude

mobileGenWith
( defaultOptions
{ dataInterfaces = [Parcelable]
, dataProtocols = [Codable]
, generateDocComments = False
}
)
''Tree

spec :: Spec
spec =
describe "stays golden" $ do
let moduleName = "GenericStructSpec"
it "swift" $
defaultGolden ("swift" <> moduleName) (showSwift @(Tree _))
it "kotlin" $
defaultGolden ("kotlin" <> moduleName) (showKotlin @(Tree _))
Loading