Skip to content

Commit c85a3b1

Browse files
committed
Make dirs upon json file specified
1 parent 1c7e84b commit c85a3b1

File tree

2 files changed

+91
-84
lines changed

2 files changed

+91
-84
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

simple_json_db.go

Lines changed: 90 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,117 +10,123 @@ import (
1010
)
1111

1212
type SimpleDB struct {
13-
data map[string]interface{}
14-
filePath string
15-
debugEnabled bool
16-
timeout *time.Timer
13+
data map[string]interface{}
14+
filePath string
15+
debugEnabled bool
16+
timeout *time.Timer
1717
}
1818

1919
func NewSimpleDB(options map[string]interface{}) *SimpleDB {
20-
db := &SimpleDB{
21-
data: make(map[string]interface{}),
22-
filePath: "./db.json",
23-
debugEnabled: false,
24-
}
20+
db := &SimpleDB{
21+
data: make(map[string]interface{}),
22+
filePath: "./db.json",
23+
debugEnabled: false,
24+
}
2525

26-
if file, ok := options["file"].(string); ok {
27-
db.filePath = file
28-
}
29-
if debug, ok := options["debug"].(bool); ok {
30-
db.debugEnabled = debug
31-
}
26+
if file, ok := options["file"].(string); ok {
27+
db.filePath = file
28+
}
29+
if debug, ok := options["debug"].(bool); ok {
30+
db.debugEnabled = debug
31+
}
3232

33-
db.init()
34-
return db
33+
db.init()
34+
return db
3535
}
3636

3737
func (db *SimpleDB) init() {
38-
if db.debugEnabled {
39-
fmt.Println("[SimpleDB] starting...")
40-
}
41-
if !strings.HasSuffix(db.filePath, ".json") {
42-
db.filePath += ".json"
43-
}
44-
if !filepath.IsAbs(db.filePath) {
45-
wd, _ := os.Getwd()
46-
db.filePath = filepath.Join(wd, db.filePath)
47-
}
48-
if db.debugEnabled {
49-
fmt.Println("[SimpleDB] File path set")
50-
fmt.Printf("[SimpleDB] File path is: %s\n", db.filePath)
51-
}
38+
if db.debugEnabled {
39+
fmt.Println("[SimpleDB] starting...")
40+
}
41+
if !strings.HasSuffix(db.filePath, ".json") {
42+
db.filePath += ".json"
43+
}
44+
if !filepath.IsAbs(db.filePath) {
45+
wd, _ := os.Getwd()
46+
db.filePath = filepath.Join(wd, db.filePath)
47+
}
48+
if err := os.MkdirAll(filepath.Dir(db.filePath), os.ModePerm); err != nil {
49+
if db.debugEnabled {
50+
fmt.Printf("[SimpleDB] Error creating directories: %v\n", err)
51+
}
52+
}
53+
if db.debugEnabled {
54+
fmt.Println("[SimpleDB] File path set")
55+
fmt.Printf("[SimpleDB] File path is: %s\n", db.filePath)
56+
}
57+
58+
fileContent, err := os.ReadFile(db.filePath)
59+
if err == nil {
60+
json.Unmarshal(fileContent, &db.data)
61+
}
62+
if db.debugEnabled {
63+
fmt.Println("[SimpleDB] Database ready")
64+
}
5265

53-
fileContent, err := os.ReadFile(db.filePath)
54-
if err == nil {
55-
json.Unmarshal(fileContent, &db.data)
56-
}
57-
if db.debugEnabled {
58-
fmt.Println("[SimpleDB] Database ready")
59-
}
6066
}
6167

6268
func (db *SimpleDB) writeDb() {
63-
if db.debugEnabled {
64-
fmt.Println("[SimpleDB] Writing database to file")
65-
}
66-
fileContent, _ := json.Marshal(db.data)
67-
os.WriteFile(db.filePath, fileContent, 0644)
68-
db.timeoutRemove()
69+
if db.debugEnabled {
70+
fmt.Println("[SimpleDB] Writing database to file")
71+
}
72+
fileContent, _ := json.Marshal(db.data)
73+
os.WriteFile(db.filePath, fileContent, 0644)
74+
db.timeoutRemove()
6975
}
7076

7177
func (db *SimpleDB) Set(key string, value interface{}) {
72-
if db.debugEnabled {
73-
fmt.Printf("[SimpleDB] Setting data for %s\n", key)
74-
}
75-
if db.timeout != nil {
76-
db.timeoutRemove()
77-
}
78-
db.timeoutSet()
79-
db.data[key] = value
78+
if db.debugEnabled {
79+
fmt.Printf("[SimpleDB] Setting data for %s\n", key)
80+
}
81+
if db.timeout != nil {
82+
db.timeoutRemove()
83+
}
84+
db.timeoutSet()
85+
db.data[key] = value
8086
}
8187

8288
func (db *SimpleDB) Get(key string) interface{} {
83-
if db.debugEnabled {
84-
fmt.Printf("[SimpleDB] Returning data for %s\n", key)
85-
}
86-
return db.data[key]
89+
if db.debugEnabled {
90+
fmt.Printf("[SimpleDB] Returning data for %s\n", key)
91+
}
92+
return db.data[key]
8793
}
8894

8995
func (db *SimpleDB) Delete(key string) {
90-
if db.debugEnabled {
91-
fmt.Printf("[SimpleDB] Deleting data for %s\n", key)
92-
}
93-
if db.timeout != nil {
94-
db.timeoutRemove()
95-
}
96-
db.timeoutSet()
97-
delete(db.data, key)
96+
if db.debugEnabled {
97+
fmt.Printf("[SimpleDB] Deleting data for %s\n", key)
98+
}
99+
if db.timeout != nil {
100+
db.timeoutRemove()
101+
}
102+
db.timeoutSet()
103+
delete(db.data, key)
98104
}
99105

100106
func (db *SimpleDB) Keys() []string {
101-
if db.debugEnabled {
102-
fmt.Println("[SimpleDB] returning keys")
103-
}
104-
keys := make([]string, 0, len(db.data))
105-
for k := range db.data {
106-
keys = append(keys, k)
107-
}
108-
return keys
107+
if db.debugEnabled {
108+
fmt.Println("[SimpleDB] returning keys")
109+
}
110+
keys := make([]string, 0, len(db.data))
111+
for k := range db.data {
112+
keys = append(keys, k)
113+
}
114+
return keys
109115
}
110116

111117
func (db *SimpleDB) timeoutSet() {
112-
if db.debugEnabled {
113-
fmt.Println("[SimpleDB] Setting timeout")
114-
}
115-
db.timeout = time.AfterFunc(500*time.Millisecond, db.writeDb)
118+
if db.debugEnabled {
119+
fmt.Println("[SimpleDB] Setting timeout")
120+
}
121+
db.timeout = time.AfterFunc(500*time.Millisecond, db.writeDb)
116122
}
117123

118124
func (db *SimpleDB) timeoutRemove() {
119-
if db.debugEnabled {
120-
fmt.Println("[SimpleDB] Removing timeout")
121-
}
122-
if db.timeout != nil {
123-
db.timeout.Stop()
124-
db.timeout = nil
125-
}
126-
}
125+
if db.debugEnabled {
126+
fmt.Println("[SimpleDB] Removing timeout")
127+
}
128+
if db.timeout != nil {
129+
db.timeout.Stop()
130+
db.timeout = nil
131+
}
132+
}

0 commit comments

Comments
 (0)