Write values with []=
and read with []
.
import linux::data_store # Needed for testing only
class A
serialize
var b = true
var f = 1.234
end
var data_store = new DataStore
data_store["one"] = 1
data_store["str"] = "Some string"
data_store["a"] = new A
assert data_store["one"] == 1
assert data_store["str"] == "Some string"
assert data_store["a"].as(A).b
assert data_store["a"].as(A).f == 1.234
assert data_store["other"] == null
Set to null
to clear a value.
data_store["one"] = null
assert data_store["one"] == null
app :: DataStore :: defaultinit
app :: DataStore :: user_defaults
TheNSUserDefaults
used to implement DataStore
app :: DataStore :: user_defaults=
TheNSUserDefaults
used to implement DataStore
linux :: data_store $ DataStore :: []=
Storevalue
at key
ios :: data_store $ DataStore :: []=
Storevalue
at key
android :: data_store $ DataStore :: []=
Storevalue
at key
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Object :: defaultinit
app :: DataStore :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).app :: DataStore :: user_defaults
TheNSUserDefaults
used to implement DataStore
app :: DataStore :: user_defaults=
TheNSUserDefaults
used to implement DataStore
# Simple data storage facility
#
# Write values with `[]=` and read with `[]`.
# ~~~
# import linux::data_store # Needed for testing only
#
# class A
# serialize
#
# var b = true
# var f = 1.234
# end
#
# var data_store = new DataStore
# data_store["one"] = 1
# data_store["str"] = "Some string"
# data_store["a"] = new A
#
# assert data_store["one"] == 1
# assert data_store["str"] == "Some string"
# assert data_store["a"].as(A).b
# assert data_store["a"].as(A).f == 1.234
# assert data_store["other"] == null
# ~~~
#
# Set to `null` to clear a value.
# ~~~
# data_store["one"] = null
# assert data_store["one"] == null
# ~~~
class DataStore
# Get the object stored at `key`, or null if nothing is available
fun [](key: String): nullable Object is abstract
# Store `value` at `key`
fun []=(key: String, value: nullable Serializable) is abstract
end
lib/app/data_store.nit:56,1--93,3
redef class DataStore
# The `NSUserDefaults` used to implement `DataStore`
var user_defaults = new NSUserDefaults.standard_user_defaults is lazy
redef fun [](key)
do
var nsstr = user_defaults.string_for_key(key.to_nsstring)
if nsstr.address_is_null then return null
# TODO report errors
var deserializer = new JsonDeserializer(nsstr.to_s)
var deserialized = deserializer.deserialize
var errors = deserializer.errors
if errors.not_empty then
# An update may have broken the versioning compatibility
print_error "{class_name} error at deserialization: {errors.join(", ")}"
return null # Let's be safe
end
return deserialized
end
redef fun []=(key, value)
do
var nsobject: NSString
if value == null then
nsobject = new NSString.nil
else
var serialized_string = new StringWriter
var serializer = new JsonSerializer(serialized_string)
serializer.serialize(value)
# TODO report errors
nsobject = serialized_string.to_s.to_nsstring
end
user_defaults.set_object(nsobject, key.to_nsstring)
end
end
lib/ios/data_store.nit:22,1--64,3
redef class DataStore
# File path of the Sqlite3 DB file
fun db_file: String do return "data_store.db"
# Sqlite3 table used
fun db_table: String do return "data_store"
private var db_cache: nullable Sqlite3DB = null
# Database to use to implement the `DataStore`
fun db: nullable Sqlite3DB
do
var db = db_cache
if db != null then return db
# Find DB path
var config_home = xdg_basedir.config_home.to_s
var config_dir = config_home.join_path(sys.program_name.basename)
if not config_dir.file_exists then config_dir.mkdir
var db_path = config_dir.join_path(db_file)
# Open DB connection
db = new Sqlite3DB.open(db_path)
if not db.is_open then
print_error "Data store unavaible, cannot load/save data. (at '{db_path}' with '{db.error or else "unknown"}')"
return null
end
# Create DB table
db.create_table "IF NOT EXISTS {db_table} (key TEXT PRIMARY KEY, value TEXT)"
db_cache = db
return db
end
redef fun [](key)
do
# Get DB
var db = self.db
if db == null then return null
# Prepare SELECT statement
var stmt = db.select("* FROM {db_table} WHERE key == {key.to_sql_string}")
if stmt == null then return null
# Execute statment
for row in stmt do
# Get from statement
var serialized = row[1].to_s
stmt.close
# Deserialize
var deserializer = new JsonDeserializer(serialized)
var deserialized = deserializer.deserialize
var errors = deserializer.errors
if errors.not_empty then
# An update may have broken the versioning compatibility
print_error "{class_name} error at deserialization: {errors.join(", ")}"
return null # Let's be safe
end
return deserialized
end
stmt.close
return null
end
redef fun []=(key, value)
do
# Get DB
var db = self.db
if db == null then return
# Serialize
var stream = new StringWriter
var serializer = new JsonSerializer(stream)
serializer.serialize value
var serialized = stream.to_s
# Save in DB
db.execute "BEGIN TRANSACTION"
db.insert "OR REPLACE INTO {db_table} VALUES({key.to_sql_string}, {serialized.to_sql_string})"
db.execute "COMMIT"
end
end
lib/linux/data_store.nit:25,1--112,3
redef class DataStore
redef fun [](key) do return shared_preferences[key]
redef fun []=(key, value) do shared_preferences[key] = value
end
lib/android/data_store.nit:25,1--33,3