Giter Club home page Giter Club logo

storage's Introduction

Storage

Every type of storage:

  • Has the same interface.
  • Extendable.
  • Thread safe.

Interface:

Any storage provided via managers is accessible as key-value pairs. You may use strings, but you'll be getting warnings, because I deprecated this approach. Natively the framework advices you to use string based enums.

In exaples I'll be using this:

enum Auth: String {
    case username // .rawValue == "username"
    case isAdmin  // .rawValue == "isAdmin"
}

Avalible Managers:

  • Keychain

    Shortcut: KC

  • UserDefaults

    Shortcut: UD

  • Cache [temporary / persistent]

    Shortcut: CH

You can easily extend Avalible managers with your own by writinfg an implementation and subscribing your managers to a StorageManagerProtocol

You should access any storage via providers. You may use them as static or singleton's properties.

  • [Manager].[Provider]

Out-of-the-box Providers:

  • .data
  • .bool
  • .string

Providers allow you to access storage:

  • Via subscripts:
    • provider[key]
  • Via methods:
    • provider.get(for: key)
    • provider.set(value, for: key)
  • Via deprecated subscripts and methods:
    • provider[stringKey]
    • provider.get(forKey: stringKey)
    • provider.set(value, forKey: stringKey)

For convenience it would be nice to extend framework's Storage.Provider with your custom adapter subscripts:

extension Storage.Provider {
    subscript(key: Auth) -> Value? {    // Value is a Provider's assosiated type.
        get { get(for: key) }           // Just pass key to build-in getter,
        set { set(newValue, for: key) } // or key-value to build in setter.
    }
}

This will allow you to access storage not just like this:

  • Storage.[Manager].[Provider][Auth.username]

but also like this:

  • Storage.[Manager].[Provider][.username]

Keychain as an example:

  • Storage.Keychain.data.set(nil, forKey: "deletedItem")
  • Storage.Keychain.data.get(forKey: "deletedItem") // nil
  • Storage.Keychain.data["StringKeys"] = "Sucks c:".data(using: .utf8)
  • Storage.Keychain.default.string[.username] = "Root"
  • Storage.Keychain.default.bool[.isAdmin] = true
  • Storage.KC.bool[.isAdmin] // true

storage's People

Contributors

maximkrouk avatar

Stargazers

 avatar

Watchers

 avatar

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.