shwifty-0.0.1.0: Generate swift types from haskell types.

Safe HaskellNone
LanguageHaskell2010

Shwifty

Contents

Description

The Shwifty library allows generation of Swift types (structs and enums) from Haskell ADTs, using Template Haskell. The main entry point to the library should be the documentation and examples of getShwifty. See also getShwiftyWith and getShwiftyWithTags.

This library is in alpha and there are a number of known bugs which shouldn't affect most users. See the issue tracker to see what those are.

There are probably many bugs/some weird behaviour when it comes to data families. Please report any issues on the issue tracker.

Synopsis

Classes for conversion

class ToSwift a where Source #

The class for things which can be converted to a Swift type (Ty).

Typically the instance will be generated by getShwifty.

Methods

toSwift :: Proxy a -> Ty Source #

Instances
ToSwift Bool Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Bool -> Ty Source #

ToSwift Char Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Char -> Ty Source #

ToSwift Double Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Double -> Ty Source #

ToSwift Float Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Float -> Ty Source #

ToSwift Int Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Int -> Ty Source #

ToSwift Int8 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Int8 -> Ty Source #

ToSwift Int16 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Int16 -> Ty Source #

ToSwift Int32 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Int32 -> Ty Source #

ToSwift Int64 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Int64 -> Ty Source #

ToSwift Integer Source # 
Instance details

Defined in Shwifty

ToSwift Word Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Word -> Ty Source #

ToSwift Word8 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Word8 -> Ty Source #

ToSwift Word16 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Word16 -> Ty Source #

ToSwift Word32 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Word32 -> Ty Source #

ToSwift Word64 Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Word64 -> Ty Source #

ToSwift () Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy () -> Ty Source #

ToSwift Void Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Void -> Ty Source #

ToSwift ByteString Source # 
Instance details

Defined in Shwifty

ToSwift ByteString Source # 
Instance details

Defined in Shwifty

ToSwift Text Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Text -> Ty Source #

ToSwift ByteArray Source # 
Instance details

Defined in Shwifty

ToSwift Text Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy Text -> Ty Source #

ToSwift UTCTime Source # 
Instance details

Defined in Shwifty

ToSwift UUID Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy UUID -> Ty Source #

ToSwift [Char] Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy [Char] -> Ty Source #

ToSwift a => ToSwift ([a] :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy [a] -> Ty Source #

ToSwift a => ToSwift (Maybe a :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (Maybe a) -> Ty Source #

ToSwift (CI s :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (CI s) -> Ty Source #

ToSwift a => ToSwift (PrimArray a :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (PrimArray a) -> Ty Source #

ToSwift a => ToSwift (SmallArray a :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (SmallArray a) -> Ty Source #

ToSwift a => ToSwift (Array a :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (Array a) -> Ty Source #

ToSwift a => ToSwift (Vector a :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (Vector a) -> Ty Source #

(ToSwift a, ToSwift b) => ToSwift (a -> b :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (a -> b) -> Ty Source #

(ToSwift a, ToSwift b) => ToSwift (Either a b :: Type) Source #

Note: In Swift, the ordering of the type variables is flipped - Shwifty has made the design choice to flip them for you. If you take issue with this, please open an issue for discussion on GitHub.

Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (Either a b) -> Ty Source #

(ToSwift a, ToSwift b) => ToSwift ((a, b) :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (a, b) -> Ty Source #

(ToSwift k, ToSwift v) => ToSwift (Map k v :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (Map k v) -> Ty Source #

(ToSwift k, ToSwift v) => ToSwift (HashMap k v :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (HashMap k v) -> Ty Source #

(ToSwift a, ToSwift b, ToSwift c) => ToSwift ((a, b, c) :: Type) Source # 
Instance details

Defined in Shwifty

Methods

toSwift :: Proxy (a, b, c) -> Ty Source #

class ToSwiftData a where Source #

The class for things which can be converted to SwiftData.

Typically the instance will be generated by getShwifty.

Minimal complete definition

toSwiftData

Methods

toSwiftData :: Proxy a -> SwiftData Source #

Convert a type to SwiftData

prettySwiftData :: Proxy a -> String Source #

Pretty-print a type as its SwiftData.

prettySwiftData :: Proxy a -> String Source #

Pretty-print a type as its SwiftData.

Generating instances

getShwifty :: Name -> Q [Dec] Source #

Generate ToSwiftData and ToSwift instances for your type. ToSwift instances are typically used to build cases or fields, whereas ToSwiftData instances are for building structs and enums. Click the Examples button to see examples of what Swift gets generated in different scenarios. To get access to the generated code, you will have to use one of the pretty-printing functions provided.

Examples

Expand
-- A simple sum type
data SumType = Sum1 | Sum2 | Sum3
getShwifty ''SumType
enum SumType {
    case sum1
    case sum2
    case sum3
}
-- A simple product type
data ProductType = ProductType { x :: Int, y :: Int }
getShwifty ''ProductType
struct ProductType {
    let x: Int
    let y: Int
}
-- A sum type with type variables
data SumType a b = SumL a | SumR b
getShwifty ''SumType
enum SumType<A, B> {
    case sumL(A)
    case sumR(B)
}
-- A product type with type variables
data ProductType a b = ProductType { aField :: a, bField :: b }
getShwifty ''ProductType
struct ProductType<A, B> {
    let aField: A
    let bField: B
}
-- A newtype
newtype Newtype a = Newtype { getNewtype :: a }
getShwifty ''Newtype
struct Newtype<A> {
    let getNewtype: A
}
-- A type with a function field
newtype Endo a = Endo { appEndo :: a -> a }
getShwifty ''Endo
struct Endo<A> {
    let appEndo: ((A) -> A)
}
-- A type with a kookier function field
newtype Fun a = Fun { fun :: Int -> Char -> Bool -> String -> Maybe a }
getShwifty ''Fun
struct FunA {
    let fun: ((Int, Char, Bool, String) -> A?)
}
-- A weird type with nested fields. Also note the Result's types being flipped from that of the Either.
data YouveGotProblems a b = YouveGotProblems { field1 :: Maybe (Maybe (Maybe a)), field2 :: Either (Maybe a) (Maybe b) }
getShwifty ''YouveGotProblems
struct YouveGotProblems<A, B> {
    let field1: Option<Option<Option<A>>>
    let field2: Result<Option<B>,Option<A>>
}
-- A type with polykinded type variables
-- Also note that there is no newline because
-- of the absence of fields
data PolyKinded (a :: k) = PolyKinded
getShwifty ''PolyKinded
struct PolyKinded<A> { }
-- A sum type where constructors might be records
data SumType a b (c :: k) = Sum1 Int a (Maybe b) | Sum2 b | Sum3 { x :: Int, y :: Int }
getShwifty ''SumType
enum SumType<A, B, C> {
  case field1(Int, A, Optional<B>)
  case field2(B)
  case field3(_ x: Int, _ y: Int)
}
-- A type containing another type with instance generated by 'getShwifty'
newtype MyFirstType a = MyFirstType { getMyFirstType :: a }
getShwifty ''MyFirstType

data Contains a = Contains { x :: MyFirstType Int, y :: MyFirstType a }
getShwifty ''Contains
struct MyFirstType<A> {
  let getMyFirstType: A
}

struct Contains<A> {
  let x: MyFirstType<Int>
  let y: MyFirstType<A>
}

getShwiftyWith :: Options -> Name -> Q [Dec] Source #

Like getShwifty, but lets you supply your own Options. Click the examples for some clarification of what you can do.

Examples

Expand
data PrefixedFields = MkPrefixedFields { prefixedFieldsX :: Int, prefixedFieldsY :: Int }
$(getShwiftyWith (defaultOptions { fieldLabelModifier = drop (length "PrefixedFields") }) ''PrefixedFields)
struct PrefixedFields {
    let x: Int
    let y: Int
}
data PrefixedCons = MkPrefixedConsLeft | MkPrefixedConsRight
$(getShwiftyWith (defaultOptions { constructorModifier = drop (length "MkPrefixedCons"), dataProtocols = [Codable] }) ''PrefixedCons)
enum PrefixedCons: Codable {
    case left
    case right
}

getShwiftyWithTags :: Options -> [Name] -> Name -> Q [Dec] Source #

Like getShwiftyWith, but lets you supply tags. Tags are type-safe typealiases that are akin to newtypes in Haskell. The introduction of a struct around something which is, say, a UUID in Swift means that the default Codable instance will not work correctly. So we introduce a tag(s). See the examples to see how this looks. Also, see https://github.com/pointfreeco/swift-tagged, the library which these tags use. The library is not included in any generated code.

Examples

Expand
-- Example of using the swift-tagged library:
-- A type containing a database key
data User = User { id :: UserId, name :: Text }
-- the user key
newtype UserId = UserId UUID
$(getShwiftyWithTags defaultOptions [ ''UserId ] ''User)
-- A type that also contains the UserId
data UserDetails = UserDetails { id :: UserId, lastName :: Text }
getShwifty ''UserDetails
struct User {
  let id: UserId
  let name: String

  typealias UserId = Tagged<User,UUID>
}

struct UserDetails {
  let id: User.UserId
  let lastName: String
}
-- Example type with multiple tags
newtype Name = MkName String
newtype Email = MkEmail String
data Person = Person { name :: Name, email :: Email }
$(getShwiftyWithTags defaultOptions [ ''Name, ''Email ] ''Person)
struct Person {
    let name: Name
    let email: Email

    enum NameTag {}
    typealias Name = Tagged<NameTag, String>

    enum EmailTag {}
    typealias Email = Tagged<EmailTag, String>
}

Types

data Ty Source #

An AST representing a Swift type.

Constructors

Unit

Unit (called "Unit/Void" in swift). Empty struct type.

Bool

Bool

Character

Character

Str

String. Named Str to avoid conflicts with String.

I

signed machine integer

I8

signed 8-bit integer

I16

signed 16-bit integer

I32

signed 32-bit integer

I64

signed 64-bit integer

U

unsigned machine integer

U8

unsigned 8-bit integer

U16

unsigned 16-bit integer

U32

unsigned 32-bit integer

U64

unsigned 64-bit integer

F32

32-bit floating point

F64

64-bit floating point

Decimal

Increased-precision floating point

BigSInt32

32-bit big integer

BigSInt64

64-bit big integer

Tuple2 Ty Ty

2-tuple

Tuple3 Ty Ty Ty

3-tuple

Optional Ty

Maybe type

Result Ty Ty

Either type

Note: The error type in Swift must implement the Error protocol. This library currently does not enforce this.

Set Ty

Set type

Dictionary Ty Ty

Dictionary type

Array Ty

array type

App Ty Ty

function type

Poly String

polymorphic type variable

Concrete

a concrete type variable, and its type variables. Will typically be generated by getShwifty.

Fields

Tag

A Tagged typealias, for newtyping in a way that doesn't break Codable.

See getShwiftyWithTags for examples.

Fields

  • name :: String

    the name of the type

  • parent :: String

    the type constructor of the type to which this alias belongs

  • typ :: Ty

    the type that this represents

  • disambiguate :: Bool

    does the type need disambiguation?

    This will happen if there are multiple tags with the same type. This is needed to maintain safety.

Instances
Eq Ty Source # 
Instance details

Defined in Shwifty

Methods

(==) :: Ty -> Ty -> Bool #

(/=) :: Ty -> Ty -> Bool #

Read Ty Source # 
Instance details

Defined in Shwifty

Show Ty Source # 
Instance details

Defined in Shwifty

Methods

showsPrec :: Int -> Ty -> ShowS #

show :: Ty -> String #

showList :: [Ty] -> ShowS #

Generic Ty Source # 
Instance details

Defined in Shwifty

Associated Types

type Rep Ty :: Type -> Type #

Methods

from :: Ty -> Rep Ty x #

to :: Rep Ty x -> Ty #

Lift Ty Source # 
Instance details

Defined in Shwifty

Methods

lift :: Ty -> Q Exp #

type Rep Ty Source # 
Instance details

Defined in Shwifty

type Rep Ty = D1 (MetaData "Ty" "Shwifty" "shwifty-0.0.1.0-JIy6ib2g261G5ctzZU78Nv" False) ((((C1 (MetaCons "Unit" PrefixI False) (U1 :: Type -> Type) :+: (C1 (MetaCons "Bool" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "Character" PrefixI False) (U1 :: Type -> Type))) :+: ((C1 (MetaCons "Str" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "I" PrefixI False) (U1 :: Type -> Type)) :+: (C1 (MetaCons "I8" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "I16" PrefixI False) (U1 :: Type -> Type)))) :+: (((C1 (MetaCons "I32" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "I64" PrefixI False) (U1 :: Type -> Type)) :+: (C1 (MetaCons "U" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "U8" PrefixI False) (U1 :: Type -> Type))) :+: ((C1 (MetaCons "U16" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "U32" PrefixI False) (U1 :: Type -> Type)) :+: (C1 (MetaCons "U64" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "F32" PrefixI False) (U1 :: Type -> Type))))) :+: (((C1 (MetaCons "F64" PrefixI False) (U1 :: Type -> Type) :+: (C1 (MetaCons "Decimal" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "BigSInt32" PrefixI False) (U1 :: Type -> Type))) :+: ((C1 (MetaCons "BigSInt64" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "Tuple2" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty))) :+: (C1 (MetaCons "Tuple3" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty))) :+: C1 (MetaCons "Optional" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty))))) :+: (((C1 (MetaCons "Result" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty)) :+: C1 (MetaCons "Set" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty))) :+: (C1 (MetaCons "Dictionary" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty)) :+: C1 (MetaCons "Array" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty)))) :+: ((C1 (MetaCons "App" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty)) :+: C1 (MetaCons "Poly" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String))) :+: (C1 (MetaCons "Concrete" PrefixI True) (S1 (MetaSel (Just "name") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: S1 (MetaSel (Just "tyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Ty])) :+: C1 (MetaCons "Tag" PrefixI True) ((S1 (MetaSel (Just "name") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: S1 (MetaSel (Just "parent") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String)) :*: (S1 (MetaSel (Just "typ") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: S1 (MetaSel (Just "disambiguate") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Bool))))))))

data SwiftData Source #

A Swift datatype, either a struct (product type) or enum (sum type). Haskll types are sums-of-products, so the way we differentiate when doing codegen, is that types with a single constructor will be converted to a struct, and those with two or more constructors will be converted to an enum. Types with 0 constructors will be converted to an empty enum.

Note: There seems to be a haddock bug related to `-XDuplicateRecordFields` which causes the haddocks for the fields of SwiftEnum to refer to struct.

Constructors

SwiftStruct

A struct (product type)

Fields

SwiftEnum

An enum (sum type)

Fields

TypeAlias

A top-level type alias

Fields

Instances
Eq SwiftData Source # 
Instance details

Defined in Shwifty

Read SwiftData Source # 
Instance details

Defined in Shwifty

Show SwiftData Source # 
Instance details

Defined in Shwifty

Generic SwiftData Source # 
Instance details

Defined in Shwifty

Associated Types

type Rep SwiftData :: Type -> Type #

type Rep SwiftData Source # 
Instance details

Defined in Shwifty

type Rep SwiftData = D1 (MetaData "SwiftData" "Shwifty" "shwifty-0.0.1.0-JIy6ib2g261G5ctzZU78Nv" False) (C1 (MetaCons "SwiftStruct" PrefixI True) ((S1 (MetaSel (Just "name") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: S1 (MetaSel (Just "tyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [String])) :*: (S1 (MetaSel (Just "protocols") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Protocol]) :*: (S1 (MetaSel (Just "fields") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [(String, Ty)]) :*: S1 (MetaSel (Just "tags") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Ty])))) :+: (C1 (MetaCons "SwiftEnum" PrefixI True) ((S1 (MetaSel (Just "name") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: (S1 (MetaSel (Just "tyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [String]) :*: S1 (MetaSel (Just "protocols") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Protocol]))) :*: (S1 (MetaSel (Just "cases") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [(String, [(Maybe String, Ty)])]) :*: (S1 (MetaSel (Just "rawValue") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Maybe Ty)) :*: S1 (MetaSel (Just "tags") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Ty])))) :+: C1 (MetaCons "TypeAlias" PrefixI True) (S1 (MetaSel (Just "name") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: (S1 (MetaSel (Just "tyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [String]) :*: S1 (MetaSel (Just "typ") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty)))))

data Protocol Source #

Swift protocols. Only a few are supported right now.

Instances
Eq Protocol Source # 
Instance details

Defined in Shwifty

Read Protocol Source # 
Instance details

Defined in Shwifty

Show Protocol Source # 
Instance details

Defined in Shwifty

Generic Protocol Source # 
Instance details

Defined in Shwifty

Associated Types

type Rep Protocol :: Type -> Type #

Methods

from :: Protocol -> Rep Protocol x #

to :: Rep Protocol x -> Protocol #

Lift Protocol Source # 
Instance details

Defined in Shwifty

Methods

lift :: Protocol -> Q Exp #

type Rep Protocol Source # 
Instance details

Defined in Shwifty

type Rep Protocol = D1 (MetaData "Protocol" "Shwifty" "shwifty-0.0.1.0-JIy6ib2g261G5ctzZU78Nv" False) (C1 (MetaCons "Hashable" PrefixI False) (U1 :: Type -> Type) :+: (C1 (MetaCons "Codable" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "Equatable" PrefixI False) (U1 :: Type -> Type)))

Options for encoding types

Option type

data Options Source #

Options that specify how to encode your SwiftData to a swift type.

Options can be set using record syntax on defaultOptions with the fields below.

Actual Options

$sel:fieldLabelModifier:Options :: Options -> String -> String Source #

Function applied to field labels. Handy for removing common record prefixes, for example. The default (id) makes no changes.

$sel:constructorModifier:Options :: Options -> String -> String Source #

Function applied to value constructor names. The default (id) makes no changes.

$sel:optionalExpand:Options :: Options -> Bool Source #

Whether or not to truncate Optional types. Normally, an Optional (Maybe) is encoded as "A?", which is syntactic sugar for "Optional<A>". The default value (False) will keep it as sugar. A value of True will expand it to be desugared.

$sel:indent:Options :: Options -> Int Source #

Number of spaces to indent field names and cases. The default is 4.

$sel:generateToSwift:Options :: Options -> Bool Source #

Whether or not to generate a ToSwift instance. Sometime this can be desirable if you want to define the instance by hand, or the instance exists elsewhere. The default is True, i.e., to generate the instance.

$sel:generateToSwiftData:Options :: Options -> Bool Source #

Whether or not to generate a ToSwiftData instance. Sometime this can be desirable if you want to define the instance by hand, or the instance exists elsewhere. The default is True, i.e., to generate the instance.

$sel:dataProtocols:Options :: Options -> [Protocol] Source #

Protocols to add to a type. The default ([]) will add none.

$sel:dataRawValue:Options :: Options -> Maybe Ty Source #

The rawValue of an enum. See https://developer.apple.com/documentation/swift/rawrepresentable/1540698-rawvalue

The default (Nothing) will not include any rawValue.

Typically, if the type does have a $sel:rawValue:SwiftStruct, the Ty will be I or Str.

Note: Currently, nothing will prevent you from putting something nonsensical here.

$sel:typeAlias:Options :: Options -> Bool Source #

Whether or not to generate a newtype as a type alias. Consider if you want this or to use getShwiftyWithTags instead.

The default (False) will generate newtypes as their own structs.

$sel:newtypeTag:Options :: Options -> Bool Source #

Whether or not to generate a newtype as an empty enum with a tag. This is for type safety reasons, but with retaining the ability to have Codable conformance.

The default (False) will not do this.

Note: This takes priority over $sel:typeAlias:Options.

Note: This option is not currently supported for newtype instances.

Examples

Expand
newtype NonEmptyText = MkNonEmptyText String
$(getShwiftyWith (defaultOptions { newtypeTag = True }) ''NonEmpyText)
enum NonEmptyTextTag {
    typealias NonEmptyText = Tagged<NonEmptyTextTag, String>
}

$sel:lowerFirstCase:Options :: Options -> Bool Source #

Whether or not to lower-case the first character of a case after applying all modifiers to it.

The default (True) will do so.

$sel:lowerFirstField:Options :: Options -> Bool Source #

Whether or not to lower-case the first character of a field after applying all modifiers to it.

The default (True) will do so.

Default Options

defaultOptions :: Options Source #

The default Options.

defaultOptions :: Options
defaultOptions = Options
  { typeConstructorModifier = id
  , fieldLabelModifier = id
  , constructorModifier = id
  , optionalExpand= False
  , indent = 4
  , generateToSwift = True
  , generateToSwiftData = True
  , dataProtocols = []
  , dataRawValue = Nothing
  , typeAlias = False
  , newtypeTag = False
  , lowerFirstField = True
  , lowerFirstCase = True
  }

Pretty-printing

Functions

prettyTy :: Ty -> String Source #

Pretty-print a Ty.

prettySwiftData :: ToSwiftData a => Proxy a -> String Source #

Pretty-print a type as its SwiftData.

Re-exports

type X = Void Source #

A filler type to be used when pretty-printing. The codegen used by shwifty doesn't look at at what a type's type variables are instantiated to, but rather at the type's top-level definition. However, to make GHC happy, you will have to fill in type variables with unused types. To get around this, you could also use something like `-XQuantifiedConstraints`, or existential types, but we leave that to the user to handle.