Skip to content

Commit 83323de

Browse files
authored
Merge pull request #3 from raphasampaio/feature/plain
Add plain instance
2 parents 2b22d87 + d084797 commit 83323de

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "SharedPreferences"
22
uuid = "a5a92e11-d0bb-45ff-9fc3-c070cd77387a"
3-
version = "0.1.1"
3+
version = "0.2.0"
44

55
[deps]
66
Nettle = "49dea1ee-f6fa-5aa6-9a11-8816cee7d4b9"

src/instance.jl

+38-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
struct Instance
1+
abstract type AbstractInstance end
2+
3+
struct Instance <: AbstractInstance
4+
path::String
5+
6+
function Instance(file::AbstractString)
7+
directory = tempdir()
8+
path = joinpath(directory, "$file.toml")
9+
10+
return new(
11+
path,
12+
)
13+
end
14+
end
15+
16+
struct InstanceEncrypted <: AbstractInstance
217
encryptor::Nettle.Encryptor
318
decryptor::Nettle.Decryptor
419
path::String
520

6-
function Instance(key::AbstractString)
21+
function InstanceEncrypted(key::AbstractString)
722
@assert length(key) == 32
823

924
directory = tempdir()
@@ -23,6 +38,14 @@ function load(instance::Instance)
2338
return Dict{String, Any}()
2439
end
2540

41+
return TOML.parsefile(instance.path)
42+
end
43+
44+
function load(instance::InstanceEncrypted)
45+
if !isfile(instance.path)
46+
return Dict{String, Any}()
47+
end
48+
2649
file = open(instance.path, "r")
2750
encrypted = read(file, String)
2851
close(file)
@@ -33,6 +56,14 @@ function load(instance::Instance)
3356
end
3457

3558
function save(instance::Instance, content::Dict)
59+
open(instance.path, "w") do io
60+
TOML.print(io, content)
61+
end
62+
63+
return nothing
64+
end
65+
66+
function save(instance::InstanceEncrypted, content::Dict)
3667
serialized = sprint(io -> TOML.print(io, content))
3768
encrypted = encrypt(instance.encryptor, add_padding_PKCS5(Vector{UInt8}(serialized), 16))
3869

@@ -43,31 +74,31 @@ function save(instance::Instance, content::Dict)
4374
return nothing
4475
end
4576

46-
function set!(instance::Instance, key::AbstractString, value::Any)
77+
function set!(instance::AbstractInstance, key::AbstractString, value::Any)
4778
dict = load(instance)
4879
dict[key] = value
4980
save(instance, dict)
5081
return nothing
5182
end
5283

53-
function Base.get(instance::Instance, key::AbstractString)
84+
function Base.get(instance::AbstractInstance, key::AbstractString)
5485
dict = load(instance)
5586
return dict[key]
5687
end
5788

58-
function Base.get(instance::Instance, key::AbstractString, default::Any)
89+
function Base.get(instance::AbstractInstance, key::AbstractString, default::Any)
5990
dict = load(instance)
6091
return get(dict, key, default)
6192
end
6293

63-
function remove!(instance::Instance, key::AbstractString)
94+
function remove!(instance::AbstractInstance, key::AbstractString)
6495
dict = load(instance)
6596
delete!(dict, key)
6697
save(instance, dict)
6798
return nothing
6899
end
69100

70-
function clean!(instance::Instance)
101+
function clean!(instance::AbstractInstance)
71102
rm(instance.path, force = true)
72103
return nothing
73104
end

test/runtests.jl

+19-13
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,57 @@ using SharedPreferences
33
using Aqua
44
using Test
55

6-
function test_all()
7-
@testset "Aqua.jl" begin
8-
@testset "Ambiguities" begin
9-
Aqua.test_ambiguities(SharedPreferences, recursive = false)
10-
end
11-
Aqua.test_all(SharedPreferences, ambiguities = false)
6+
function test_aqua()
7+
@testset "Ambiguities" begin
8+
Aqua.test_ambiguities(SharedPreferences, recursive = false)
129
end
10+
Aqua.test_all(SharedPreferences, ambiguities = false)
1311

14-
key = "iZS1EDuzE5dEf3NmN8m3SbwOJEpu4b0i"
12+
return nothing
13+
end
1514

16-
instance = SharedPreferences.Instance(key)
15+
function test_shared_preferences(instance::SharedPreferences.AbstractInstance)
1716
clean!(instance)
1817

19-
instance = SharedPreferences.Instance(key)
2018
set!(instance, "string", "value")
2119
set!(instance, "integer", 42)
2220
set!(instance, "float", 3.14)
2321
set!(instance, "boolean", true)
2422
set!(instance, "array", [1, 2, 3])
2523

26-
instance = SharedPreferences.Instance(key)
2724
@test get(instance, "string") == "value"
2825
@test get(instance, "integer") == 42
2926
@test get(instance, "float") == 3.14
3027
@test get(instance, "boolean") == true
3128
@test get(instance, "array") == [1, 2, 3]
3229

33-
instance = SharedPreferences.Instance(key)
3430
remove!(instance, "string")
3531
remove!(instance, "integer")
3632
remove!(instance, "float")
3733
remove!(instance, "boolean")
3834
remove!(instance, "array")
3935

40-
instance = SharedPreferences.Instance(key)
4136
@test_throws KeyError get(instance, "string")
4237
@test_throws KeyError get(instance, "integer")
4338
@test_throws KeyError get(instance, "float")
4439
@test_throws KeyError get(instance, "boolean")
4540
@test_throws KeyError get(instance, "array")
4641

47-
instance = SharedPreferences.Instance(key)
4842
clean!(instance)
4943

5044
return nothing
5145
end
5246

47+
function test_all()
48+
@testset "Aqua.jl" begin test_aqua() end
49+
50+
instance = SharedPreferences.Instance("vG1Z4i52FR")
51+
@testset "instance" begin test_shared_preferences(instance) end
52+
53+
instance = SharedPreferences.InstanceEncrypted("gIU2iw4p10KnP2HsU5TBJ5Uuj5Khj2R0")
54+
@testset "instance encrypted" begin test_shared_preferences(instance) end
55+
56+
return nothing
57+
end
58+
5359
test_all()

0 commit comments

Comments
 (0)