Generic model framework

Overview

Pistachio

Pistachio is a generic model framework. By leveraging lenses and value transformers, it allows you to create type safe adapters for any recursive data structure, be it JSON, YAML or XML.

If you are already familiar with Argo, take a look at Pistachiargo.

Installation

Carthage

Carthage is a simple, decentralized dependency manager for Cocoa.

  1. Add Pistachio to your Cartfile:
github "felixjendrusch/Pistachio" ~> 0.2
  1. Run carthage update to fetch and build Pistachio and its dependencies.

  2. Make sure your application's target links against Pistachio.framework and copies all relevant frameworks into its application bundle (iOS); or embeds the binaries of all relevant frameworks (Mac).

Usage

Let's start by defining a very simple model:

struct Origin {
  var city: String

  init(city: String = "") {
    self.city = city
  }
}
struct Person {
  var name: String
  var origin: Origin

  init(name: String = "", origin: Origin = Origin()) {
    self.name = name
    self.origin = origin
  }
}

A lens is basically just a combination of a getter and a setter, providing a view on your model:

struct OriginLenses {
  static let city = Lens(get: { $0.city }, set: { (inout origin: Origin, city) in
    origin.city = city
  })
}
struct PersonLenses {
  static let name = Lens(get: { $0.name }, set: { (inout person: Person, name) in
    person.name = name
  })

  static let origin = Lens(get: { $0.origin }, set: { (inout person: Person, origin) in
    person.origin = origin
  })
}

It can be used to access and modify your model:

var person = Person(name: "Felix", origin: Origin(city: "Berlin"))
person = set(PersonLenses.name, person, "Robb")
get(PersonLenses.name, person) // == "Robb"

And you can compose, lift, etc. them:

let composed = PersonLenses.origin >>> OriginLenses.city
person = set(composed, person, "New York")
get(composed, person) // == "New York"

With lenses and value transformers, you can create adapters for your models:

struct Adapters {
  static let origin = DictionaryAdapter(specification: [
    "city_name": map(OriginLenses.city, StringToAnyObjectValueTransformers)
  ], dictionaryTansformer: DictionaryToAnyObjectValueTransformers, value: Origin())

  static let person = DictionaryAdapter(specification: [
    "name": map(PersonLenses.name, StringToAnyObjectValueTransformers),
    "origin": map(PersonLenses.origin, origin)
  ], dictionaryTansformer: DictionaryToAnyObjectValueTransformers, value: Person())
}

Use fix to create adapters for recursive models:

let adapter: DictionaryAdapter<Key, Value, TransformedValue, Error> = fix { adapter in
  // use `adapter` to reference the currently created adapter
}

Adapters handle transforming and reverse transforming your models:

let adapter = Adapters.person

var person = Person(name: "Seb", origin: Origin(city: "Berlin"))
var data = adapter.transform(person)
// == .Success(Box([ "name": "Seb", "origin": [ "city_name": "Berlin" ] ]))

adapter.reverseTransform(data.value!)
// == .Success(Box(person))

Both transform and reverseTransform return a Result, which either holds the (reverse) transformed value or an error. This enables you to gracefully handle transformation errors.

Posts

You might also like...
The QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey.
The QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey.

QuoteKit The QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey. It uses the latest async/await syntax for

contoh pembuatan framework untuk ios swift

circularContohFramework Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Install

Useless tools for exploring Virtualization.framework

Tools for exploring the internals of Virtualization.framework's Mac virtualization support. I made this since I don't have an Apple Silicon Mac but st

How to develop an iOS 14 application with SwiftUI 2.0 framework. How to create an Onboarding Screen with Page Tab View

Ama-Fruits USER INTERFACE AND USER EXPERIENCE APP DESIGN How to develop an iOS 14 application with SwiftUI 2.0 framework. How to create an Onboarding

A simple composition framework to create transformations that are either unidirectional or bidirectional

c is a simple composition framework. You have the ability to create transformations that are either unidirectional or bidirectional. There is also a cache that values can be set and resolved.

A simple framework to output to a file, url, the console, or even register notification using UserNotifications

o is a simple framework to output to a file, url, the console, or even register notification using UserNotifications. o can also get input from a file, url, or console.

A Swift SPM framework for running and managing Lua code from Swift

LuaKit A Swift Package for running and managing Lua code from Swift. Documentation For documentation, add this package as Swift Package Dependency, an

IBSKit - an Xcode Fat Framework written in Swift 5
IBSKit - an Xcode Fat Framework written in Swift 5

IBSKit framework is designed to solve everyday tasks that any iOS developer faces when developing a new project.

A robust drag-and-drop framework for iOS.

# BetweenKit ###Overview BetweenKit is a robust framework, built on UIKit that allows you to build drag-and-drop functionallity into your iOS applicat

Comments
  • Eliminate a few Lens type annotations

    Eliminate a few Lens type annotations

    One of my initial reactions to Pistachio (besides, “fuck yeah lenses”) was a bit of disappointment at the number of type annotations required, since I hate to duplicate anything for the compiler.

    However, I didn't realize that the level of annotation given in the README is actually overkill, and that less will still compile perfectly fine.

    So, in short, this is an opinionated change intended to counteract the sort of reaction that I had, by reducing code/type duplication in the README and some of Pistacho's tests. Feel free to accept or reject it! :smile:

    opened by jspahrsummers 4
  • Swift 1.2

    Swift 1.2

    opened by felixvisee 0
  • Swift2

    Swift2

    Updated code, specs, dependencies so everything builds with swift2.

    Depends on robb/Monocle#9 which does the same for Monocle. 36b5903 assumes Monocle gets version bumped to 0.0.3. I'll update with whatever version number @robb decides on.

    opened by stigi 3
  • Upgrade Monocle?

    Upgrade Monocle?

    Is there any plan to upgrade the Monocle package? version 0.0.1 does not compile with swift 2 compiler.

    https://github.com/robb/Monocle/commit/2ef01aa0d3cc33f1a0aa6ba1e583c39a58aa1dbf

    opened by sendyhalim 2
Releases(0.2.2)
Owner
Felix Visée
Software engineer in the Wallet team at Apple.
Felix Visée
MacLookup - Lookup for all Mac names, colors, model identifiers and part numbers

MacLookup Lookup for all Mac names, colors, model identifiers and part numbers.

Voyager Software Inc. 2 Jan 4, 2022
MVVM - Model View ViewModel done on Swift

MVVM Model View ViewModel done on iOS Swift Model–view–viewmodel (MVVM) is a sof

null 0 Jan 19, 2022
Contacts is an iOS app based on MVP (Model View Presenter) software architectural pattern.

Contacts Description Contacts is an iOS app based on MVP (Model View Presenter) software architectural pattern. Run Requirements Xcode 10.2.1 Swift 5.

Tirupati Balan 4 Apr 26, 2022
Start your next Open-Source Swift Framework 📦

SwiftKit enables you to easily generate a cross platform Swift Framework from your command line. It is the best way to start your next Open-Source Swi

Sven Tiigi 821 Dec 28, 2022
Easily generate cross platform Swift framework projects from the command line

SwiftPlate Easily generate cross platform Swift framework projects from the command line. SwiftPlate will generate Xcode projects for you in seconds,

John Sundell 1.8k Dec 27, 2022
A Swift wrapper around the CoreSymbolication private framework on macOS.

CoreSymbolication provides a very powerful system for looking up and extracting symbolic information from mach-o executables, dyld shared caches, and dSYMs.

Stacksift 7 Nov 21, 2022
ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects.

ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects. The kit provides variety of customisations inorder to design a confetti which matches your project's UI. ConfettiKit makes your work of adding Confetti on your project with just one line of code.

Gokul Nair 14 Sep 27, 2022
A ARM macOS Virtual Machine, using macOS 12's new Virtualization framework.

macOS Virtual Machine A ARM macOS Virtual Machine, using macOS 12's new Virtualization framework. I copied KhaosT's code from here, all I did is chang

Ming Chang 127 Nov 30, 2022
Alchemy, an elegant, batteries included backend framework for Swift.

Elegant, batteries included web framework for Swift.

Alchemy 313 Jan 2, 2023
SandboxKit - Framework that makes it easy to launch a single Scene of your application

SandboxKit This framework makes debugging more efficient in your application. Sandbox is the name of a structure that improves the efficiency of debug

Aoi Okawa 10 Apr 24, 2022