Skip to content

Commit

Permalink
Merge pull request #14 from MuDrone/master
Browse files Browse the repository at this point in the history
replace deprecated io/util
  • Loading branch information
shubhexists authored Nov 8, 2023
2 parents e159837 + 17c738d commit 96951e4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {

// //Writing into the database Example
for _, items := range employees {
db.Write("trials", User{
db.Write("users", User{
Name: items.Name,
Age: items.Age,
Contact: items.Contact,
Expand Down
16 changes: 7 additions & 9 deletions models/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package models
import (
"sync"
// io/util is deprecated but I found no alternative on ChatGPT :), To be updated soon with newer one
"io/ioutil"
//Maybe later on shift to a faster library for json encoding and decoding
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/jcelliott/lumber"
"github.com/patrickmn/go-cache"
"github.com/shubhexists/go-json-db/utils"
Expand Down Expand Up @@ -86,12 +84,12 @@ func (driver *Driver) Write(collection string, v interface{}) error {
defer mutex.Unlock()

dir := filepath.Join(driver.dir, collection)
tempdir := filepath.Join(dir, data)
tempDir := filepath.Join(dir, data)
fnlPath := filepath.Join(dir, data+".json")
tmpPath := fnlPath + ".tmp"

// Check if the file already exists
if _, err := utils.Stat(tempdir); err == nil {
if _, err := utils.Stat(tempDir); err == nil {
fmt.Println("Record already exists!")
return fmt.Errorf("record already exists")
}
Expand All @@ -106,7 +104,7 @@ func (driver *Driver) Write(collection string, v interface{}) error {
}

b = append(b, byte('\n'))
if err := ioutil.WriteFile(tmpPath, b, 0644); err != nil {
if err := os.WriteFile(tmpPath, b, 0644); err != nil {
return err
}

Expand Down Expand Up @@ -138,7 +136,7 @@ func (driver *Driver) Read(collection string, data string, c *cache.Cache, wantC
}
}

b, err := ioutil.ReadFile(record + ".json")
b, err := os.ReadFile(record + ".json")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -168,11 +166,11 @@ func (driver *Driver) ReadAll(collection string, c *cache.Cache, wantCache bool)
return records.([]string), nil
}
}
files, _ := ioutil.ReadDir(dir)
files, _ := os.ReadDir(dir)
var records []string

for _, file := range files {
b, err := ioutil.ReadFile(filepath.Join(dir, file.Name()))
b, err := os.ReadFile(filepath.Join(dir, file.Name()))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -262,7 +260,7 @@ func (driver *Driver) UpdateRecord(collection string, data string, v interface{}
}

b = append(b, byte('\n'))
if err := ioutil.WriteFile(record+".json", b, 0644); err != nil {
if err := os.WriteFile(record+".json", b, 0644); err != nil {
return err
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions models/experimentalDrivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
// "github.com/jcelliott/lumber"
"github.com/shubhexists/go-json-db/utils"
Expand All @@ -23,7 +23,7 @@ func (driver *Driver) Update(collection string, data string, v interface{}, newV
return err
}

b, err := ioutil.ReadFile(record + ".json")
b, err := os.ReadFile(record + ".json")
if err != nil {
return err
}
Expand All @@ -50,7 +50,7 @@ func (driver *Driver) Update(collection string, data string, v interface{}, newV
}

b = append(b, byte('\n'))
if err := ioutil.WriteFile(record+".json", b, 0644); err != nil {
if err := os.WriteFile(record+".json", b, 0644); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Stat(path string) (fi os.FileInfo, err error) {
return
}

// Utility fucntion to check for tag "db" with value main in the struct and returning the struct member name
// Utility function to check for tag "db" with value main in the struct and returning the struct member name
func CheckTag(s interface{}) (string, error) {
v := reflect.ValueOf(s)
t := v.Type()
Expand Down

0 comments on commit 96951e4

Please sign in to comment.