Skip to content

Repository files navigation

Icon representing the OpenBytes c project.

c

Micro Composition

MIT LicenseCommunity Chat

What is c?

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.

Where can c be used?

c can be used anywhere to create transformations or interact with the cache.

Examples

BiDirectionalTransformation of String and Int

lettransformer= c.transformer(
from:{ string inInt(string)},
to:{ int in"\(String(describing: int))"})letstring= transformer.to(3)letint= transformer.from("3")try t.assert(transformer.to(int), isEqualTo: string)

Cache

letcache= c.Cache()
cache.set(value:Double.pi, forKey:"🥧")letpi:Double= cache.get("🥧")??0try t.assert(pi, isEqualTo:.pi)letresolvedValue:Double=try cache.resolve("🥧")try t.assert(resolvedValue, isEqualTo:.pi)
cache.remove("🥧")letnilValue:Double?= cache.get("🥧")try t.assert(isNil: nilValue)

Cache

enumCacheKey:Hashable{...}letcache=c.Cache<CacheKey,Any>()
cache.set(value:Double.pi, forKey:CacheKey.piKey)letpi:Double= cache.get(.piKey)??0try t.assert(pi, isEqualTo:.pi)letresolvedValue:Double=try cache.resolve(.piKey)try t.assert(resolvedValue, isEqualTo:.pi)
cache.remove(.piKey)letnilValue:Double?= cache.get(.piKey)try t.assert(isNil: nilValue)

JSON

enumMockJSONKey:String,Hashable{case name, number, bool, invalid_key
}structMockJSON:Codable{varname:Stringvarnumber:Intvarbool:Bool}letjsonData:Data=try!JSONEncoder().encode(MockJSON(name:"Twitch", number:5, bool:false))letjson:c.JSON<MockJSONKey>=.init(data: jsonData)XCTAssertEqual(try! json.resolve(.name),"Twitch")XCTAssertEqual(try! json.resolve(.number),5)XCTAssertEqual(try! json.resolve(.bool),false)letinvalid_key:Bool?= json.get(.invalid_key)XCTAssertNil(json.get(.invalid_key))XCTAssertNil(invalid_key)
json.set(value:"Leif", forKey:.name)XCTAssertEqual(try! json.resolve(.name),"Leif")

Global Cache

letsomeCache:Cache=...
// Set the value of a Cache with any hashable key
c.set(value: someCache, forKey:"someCache")
// Get an optional Cache using any hashable key
letanotherCache:Cache?= c.get(0)
// Require that a Cache exist using a `.get` with a force unwrap
letrequiredCache:Cache=try c.resolve(0)letkeyedCache:KeyedCache<String>=try c.resolve(...)

About

📦 Micro Composition using Transformations and Cache

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages