GithubHelp home page GithubHelp logo

brightdigit / options Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 0.0 155 KB

Sometimes there are situations where you want to use an Enum in an OptionSet or you want Enum backed by a RawType of Int but also have String labels as well

License: MIT License

Swift 84.65% Shell 15.35%
swift enum bitflags codable optionset

options's Introduction

Options

Options

More powerful options for Enum and OptionSet types.

SwiftPM Twitter GitHub GitHub issues GitHub Workflow Status

Codecov CodeFactor Grade codebeat badge Code Climate maintainability Code Climate technical debt Code Climate issues Reviewed by Hound

Table of Contents

Introduction

Options provides a powerful set of features for Enum and OptionSet types:

  • Providing additional representations for Enum types besides the RawType rawValue
  • Being able to interchange between Enum and OptionSet types
  • Using an additional value type for a Codable OptionSet

Requirements

Apple Platforms

  • Xcode 14.1 or later
  • Swift 5.7.1 or later
  • iOS 16 / watchOS 9 / tvOS 16 / macOS 12 or later deployment targets

Linux

  • Ubuntu 20.04 or later
  • Swift 5.7.1 or later

Installation

Use the Swift Package Manager to install this library via the repository url:

https://github.com/brightdigit/Options.git

Use version up to 1.0.

Usage

Versatile Options

Let's say we are using an Enum for a list of popular social media networks:

enum SocialNetwork : Int {
  case digg
  case aim
  case bebo
  case delicious
  case eworld
  case googleplus
  case itunesping
  case jaiku
  case miiverse
  case musically
  case orkut
  case posterous
  case stumbleupon
  case windowslive
  case yahoo
}

We'll be using this as a way to define a particular social handle:

struct SocialHandle {
  let name : String
  let network : SocialNetwork
}

However we also want to provide a way to have a unique set of social networks available:

struct SocialNetworkSet : Int, OptionSet {
...
}

let user : User
let networks : SocialNetworkSet = user.availableNetworks()

We can then simply use Options() macro to generate both these types:

@Options
enum SocialNetwork : Int {
  case digg
  case aim
  case bebo
  case delicious
  case eworld
  case googleplus
  case itunesping
  case jaiku
  case miiverse
  case musically
  case orkut
  case posterous
  case stumbleupon
  case windowslive
  case yahoo
}

Now we can use the newly create SocialNetworkSet type to store a set of values:

let networks : SocialNetworkSet
networks = [.aim, .delicious, .googleplus, .windowslive]

Multiple Value Types

With the Options() macro, we add the ability to encode and decode values not only from their raw value but also from a another type such as a string. This is useful for when you want to store the values in JSON format.

For instance, with a type like SocialNetwork we need need to store the value as an Integer:

5

However by adding the Options() macro we can also decode from a String:

"googleplus"

Creating an OptionSet

We can also have a new OptionSet type created. Options() create a new OptionSet type with the suffix -Set. This new OptionSet will automatically work with your enum to create a distinct set of values. Additionally it will decode and encode your values as an Array of String. This means the value:

[.aim, .delicious, .googleplus, .windowslive]

is encoded as:

["aim", "delicious", "googleplus", "windowslive"]

Further Code Documentation

Documentation Here

License

This code is distributed under the MIT license. See the LICENSE file for more info.

options's People

Contributors

leogdion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

options's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.