shwifty-0.0.3.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 #

Convert a type to its Swift Ty.

Instances
ToSwift Bool Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Bool -> Ty Source #

ToSwift Char Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Char -> Ty Source #

ToSwift Double Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Double -> Ty Source #

ToSwift Float Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Float -> Ty Source #

ToSwift Int Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Int -> Ty Source #

ToSwift Int8 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Int8 -> Ty Source #

ToSwift Int16 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Int16 -> Ty Source #

ToSwift Int32 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Int32 -> Ty Source #

ToSwift Int64 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Int64 -> Ty Source #

ToSwift Integer Source # 
Instance details

Defined in Shwifty.Class

ToSwift Word Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Word -> Ty Source #

ToSwift Word8 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Word8 -> Ty Source #

ToSwift Word16 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Word16 -> Ty Source #

ToSwift Word32 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Word32 -> Ty Source #

ToSwift Word64 Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Word64 -> Ty Source #

ToSwift () Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy () -> Ty Source #

ToSwift ByteString Source # 
Instance details

Defined in Shwifty.Class

ToSwift ByteString Source # 
Instance details

Defined in Shwifty.Class

ToSwift Text Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Text -> Ty Source #

ToSwift ByteArray Source # 
Instance details

Defined in Shwifty.Class

ToSwift Text Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy Text -> Ty Source #

ToSwift UTCTime Source # 
Instance details

Defined in Shwifty.Class

ToSwift UUID Source # 
Instance details

Defined in Shwifty.Class

Methods

toSwift :: Proxy UUID -> Ty Source #

ToSwift [Char] Source # 
Instance details

Defined in Shwifty.Class

Methods

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

ToSwift a => ToSwift [a] Source # 
Instance details

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

ToSwift (CI s) Source # 
Instance details

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

(ToSwift a, ToSwift b) => ToSwift (Either a b) 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.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

Methods

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

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

Defined in Shwifty.Class

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.

Methods

toSwiftData :: Proxy a -> SwiftData Source #

Convert a type to 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 Fun<A> {
    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>
}

getShwiftyCodec :: forall tag. ModifyOptions tag => Codec tag -> Name -> Q [Dec] Source #

Like getShwiftyWith, but with a Codec instead of Options.

getShwiftyCodecTags :: forall tag. ModifyOptions tag => Codec tag -> [Name] -> Name -> Q [Dec] Source #

Like getShwiftyWithTags, but with a Codec instead of Options.

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

  • tagName :: String

    the name of the type

  • tagParent :: String

    the type constructor of the type to which this alias belongs

  • tagTyp :: Ty

    the type that this represents

  • tagDisambiguate :: 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.Types

Methods

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

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

Read Ty Source # 
Instance details

Defined in Shwifty.Types

Show Ty Source # 
Instance details

Defined in Shwifty.Types

Methods

showsPrec :: Int -> Ty -> ShowS #

show :: Ty -> String #

showList :: [Ty] -> ShowS #

Generic Ty Source # 
Instance details

Defined in Shwifty.Types

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.Types

Methods

lift :: Ty -> Q Exp #

ProtocolList protocols => ModifyOptions (MakeBase (Nothing :: Maybe Ty) protocols :: Type) Source # 
Instance details

Defined in Shwifty.Codec

(CanBeRawValue ty, ProtocolList protocols) => ModifyOptions (MakeBase (Just ty) protocols :: Type) Source # 
Instance details

Defined in Shwifty.Codec

type Rep Ty Source # 
Instance details

Defined in Shwifty.Types

type Rep Ty = D1 (MetaData "Ty" "Shwifty.Types" "shwifty-0.0.3.0-B9LT10ccU5n8RnziLkuDGN" 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 "concreteName") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: S1 (MetaSel (Just "concreteTyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Ty])) :+: C1 (MetaCons "Tag" PrefixI True) ((S1 (MetaSel (Just "tagName") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: S1 (MetaSel (Just "tagParent") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String)) :*: (S1 (MetaSel (Just "tagTyp") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Ty) :*: S1 (MetaSel (Just "tagDisambiguate") 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.

Constructors

SwiftStruct

A struct (product type)

Fields

SwiftEnum

An enum (sum type)

Fields

SwiftAlias

A top-level type alias

Fields

Instances
Eq SwiftData Source # 
Instance details

Defined in Shwifty.Types

Read SwiftData Source # 
Instance details

Defined in Shwifty.Types

Show SwiftData Source # 
Instance details

Defined in Shwifty.Types

Generic SwiftData Source # 
Instance details

Defined in Shwifty.Types

Associated Types

type Rep SwiftData :: Type -> Type #

type Rep SwiftData Source # 
Instance details

Defined in Shwifty.Types

type Rep SwiftData = D1 (MetaData "SwiftData" "Shwifty.Types" "shwifty-0.0.3.0-B9LT10ccU5n8RnziLkuDGN" False) (C1 (MetaCons "SwiftStruct" PrefixI True) ((S1 (MetaSel (Just "structName") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: (S1 (MetaSel (Just "structTyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [String]) :*: S1 (MetaSel (Just "structProtocols") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Protocol]))) :*: (S1 (MetaSel (Just "structFields") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [(String, Ty)]) :*: (S1 (MetaSel (Just "structPrivateTypes") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [SwiftData]) :*: S1 (MetaSel (Just "structTags") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Ty])))) :+: (C1 (MetaCons "SwiftEnum" PrefixI True) ((S1 (MetaSel (Just "enumName") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: (S1 (MetaSel (Just "enumTyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [String]) :*: S1 (MetaSel (Just "enumProtocols") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Protocol]))) :*: ((S1 (MetaSel (Just "enumCases") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [(String, [(Maybe String, Ty)])]) :*: S1 (MetaSel (Just "enumRawValue") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Maybe Ty))) :*: (S1 (MetaSel (Just "enumPrivateTypes") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [SwiftData]) :*: S1 (MetaSel (Just "enumTags") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Ty])))) :+: C1 (MetaCons "SwiftAlias" PrefixI True) (S1 (MetaSel (Just "aliasName") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 String) :*: (S1 (MetaSel (Just "aliasTyVars") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [String]) :*: S1 (MetaSel (Just "aliasTyp") 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.Types

Read Protocol Source # 
Instance details

Defined in Shwifty.Types

Show Protocol Source # 
Instance details

Defined in Shwifty.Types

Generic Protocol Source # 
Instance details

Defined in Shwifty.Types

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.Types

Methods

lift :: Protocol -> Q Exp #

type Rep Protocol Source # 
Instance details

Defined in Shwifty.Types

type Rep Protocol = D1 (MetaData "Protocol" "Shwifty.Types" "shwifty-0.0.3.0-B9LT10ccU5n8RnziLkuDGN" 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

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

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

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

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

optionalExpand :: 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.

generateToSwift :: 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.

generateToSwiftData :: 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.

dataProtocols :: Options -> [Protocol] Source #

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

dataRawValue :: 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 rawValue, the Ty will be I or Str.

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

typeAlias :: 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.

newtypeTag :: 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 typeAlias.

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>
}

lowerFirstCase :: 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.

lowerFirstField :: 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.

omitFields :: Options -> [String] Source #

Fields to omit from a struct when generating types.

The default ([]) will omit nothing.

omitCases :: Options -> [String] Source #

Cases to omit from an enum when generating types.

The default ([]) will omit nothing.

makeBase :: Options -> (Bool, Maybe Ty, [Protocol]) Source #

Whether or not to make a base type, its raw value, and its protocols.

Here, "base type" refers to a version of the type without any fields. This can be useful for doing Codable conversions.

The default (False, Nothing, []) will not create the base type.

Default Options

defaultOptions :: Options Source #

The default Options.

defaultOptions :: Options
defaultOptions = Options
  { typeConstructorModifier = id
  , fieldLabelModifier = id
  , constructorModifier = id
  , optionalExpand= False
  , generateToSwift = True
  , generateToSwiftData = True
  , dataProtocols = []
  , dataRawValue = Nothing
  , typeAlias = False
  , newtypeTag = False
  , lowerFirstField = True
  , lowerFirstCase = True
  , omitFields = []
  , omitCases = []
  , makeBase = (False, Nothing, [])
  }

Codec options

data Codec tag Source #

A carrier for modifiers.

Constructors

Codec 
Instances
ModifyOptions tag => ModifyOptions (Codec tag :: Type) Source # 
Instance details

Defined in Shwifty.Codec

class ModifyOptions tag where Source #

Modify options.

Instances
ModifyOptions NewtypeTag Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions TypeAlias Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions AsIs Source # 
Instance details

Defined in Shwifty.Codec

KnownSymbol cas => ModifyOptions (OmitCase cas :: Type) Source # 
Instance details

Defined in Shwifty.Codec

KnownSymbol field => ModifyOptions (OmitField field :: Type) Source # 
Instance details

Defined in Shwifty.Codec

(TypeError (Text "Cannot apply DontLowercase to TyCon") :: Constraint) => ModifyOptions (DontLowercase TyCon) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (DontLowercase DataCon) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (DontLowercase Field) Source # 
Instance details

Defined in Shwifty.Codec

CanBeRawValue ty => ModifyOptions (RawValue ty :: Type) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (Implement Hashable) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (Implement Codable) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (Implement Equatable) Source # 
Instance details

Defined in Shwifty.Codec

GenerateClass c => ModifyOptions (DontGenerate c :: Type) Source # 
Instance details

Defined in Shwifty.Codec

ProtocolList protocols => ModifyOptions (MakeBase (Nothing :: Maybe Ty) protocols :: Type) Source # 
Instance details

Defined in Shwifty.Codec

(CanBeRawValue ty, ProtocolList protocols) => ModifyOptions (MakeBase (Just ty) protocols :: Type) Source # 
Instance details

Defined in Shwifty.Codec

KnownSymbol string => ModifyOptions (Drop TyCon string :: Type) Source # 
Instance details

Defined in Shwifty.Codec

KnownSymbol string => ModifyOptions (Drop DataCon string :: Type) Source # 
Instance details

Defined in Shwifty.Codec

KnownSymbol string => ModifyOptions (Drop Field string :: Type) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions tag => ModifyOptions (Codec tag :: Type) Source # 
Instance details

Defined in Shwifty.Codec

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

Defined in Shwifty.Codec

type AsIs = () Source #

No modifications

data a & b infixr 6 Source #

Combine modifications.

Instances
(ModifyOptions a, ModifyOptions b) => ModifyOptions (a & b :: Type) Source # 
Instance details

Defined in Shwifty.Codec

data Label Source #

Label modifiers.

Constructors

TyCon

Type constructor modifier

DataCon

Data constructor modifiers

Field

Field label modifiers

data Drop (label :: Label) (string :: Symbol) Source #

Modify a label by dropping a string

Instances
KnownSymbol string => ModifyOptions (Drop TyCon string :: Type) Source # 
Instance details

Defined in Shwifty.Codec

KnownSymbol string => ModifyOptions (Drop DataCon string :: Type) Source # 
Instance details

Defined in Shwifty.Codec

KnownSymbol string => ModifyOptions (Drop Field string :: Type) Source # 
Instance details

Defined in Shwifty.Codec

data DontGenerate (cls :: * -> Constraint) Source #

Don't generate a specific class.

Instances
GenerateClass c => ModifyOptions (DontGenerate c :: Type) Source # 
Instance details

Defined in Shwifty.Codec

data Implement (protocol :: Protocol) Source #

Add protocols

Instances
ModifyOptions (Implement Hashable) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (Implement Codable) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (Implement Equatable) Source # 
Instance details

Defined in Shwifty.Codec

data RawValue (ty :: Ty) Source #

Add a rawValue

Instances
CanBeRawValue ty => ModifyOptions (RawValue ty :: Type) Source # 
Instance details

Defined in Shwifty.Codec

class CanBeRawValue (ty :: Ty) Source #

A Class that indicates that this swift type can be a rawValue. The value of getRawValue will be its actual rawValue.

Minimal complete definition

getRawValue

Instances
CanBeRawValue Str Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue I Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue I8 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue I16 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue I32 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue I64 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue U Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue U8 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue U16 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue U32 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

CanBeRawValue U64 Source # 
Instance details

Defined in Shwifty.Codec

Methods

getRawValue :: Ty

data TypeAlias Source #

Make it a type alias (only applies to newtypes)

Instances
ModifyOptions TypeAlias Source # 
Instance details

Defined in Shwifty.Codec

data NewtypeTag Source #

Make it a newtype tag (only applies to newtype tags)

Instances
ModifyOptions NewtypeTag Source # 
Instance details

Defined in Shwifty.Codec

data DontLowercase (someKind :: Label) Source #

Dont lower-case fields/cases

Instances
(TypeError (Text "Cannot apply DontLowercase to TyCon") :: Constraint) => ModifyOptions (DontLowercase TyCon) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (DontLowercase DataCon) Source # 
Instance details

Defined in Shwifty.Codec

ModifyOptions (DontLowercase Field) Source # 
Instance details

Defined in Shwifty.Codec

data OmitField (field :: Symbol) Source #

Omit a field

Instances
KnownSymbol field => ModifyOptions (OmitField field :: Type) Source # 
Instance details

Defined in Shwifty.Codec

data OmitCase (cas :: Symbol) Source #

Omit a case

Instances
KnownSymbol cas => ModifyOptions (OmitCase cas :: Type) Source # 
Instance details

Defined in Shwifty.Codec

data MakeBase (rawValue :: Maybe Ty) (protocols :: [Protocol]) Source #

Make a base type

Instances
ProtocolList protocols => ModifyOptions (MakeBase (Nothing :: Maybe Ty) protocols :: Type) Source # 
Instance details

Defined in Shwifty.Codec

(CanBeRawValue ty, ProtocolList protocols) => ModifyOptions (MakeBase (Just ty) protocols :: Type) Source # 
Instance details

Defined in Shwifty.Codec

Pretty-printing

Functions

prettyTy :: Ty -> String Source #

Pretty-print a Ty.

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.