From fb6955465a69529d74cc68500441f9cb5ee6f0ae Mon Sep 17 00:00:00 2001 From: ycc Date: Tue, 22 Aug 2023 21:35:14 +0200 Subject: [PATCH] deprecated funcs fix --- db.go | 24 +++++++----------------- my_test.go | 16 ++++------------ pg_test.go | 15 +++------------ test_table.json | 4 ++-- 4 files changed, 16 insertions(+), 43 deletions(-) diff --git a/db.go b/db.go index 98002ba..efe7485 100755 --- a/db.go +++ b/db.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "html/template" - "io/ioutil" "log" "os" "reflect" @@ -402,16 +401,13 @@ func (t *TableInfo) DeleteColumn(name string) error { } func (db *Db) ImportSchema(filename string) { - jsonFile, err := os.Open(filename) + byteValue, _ := os.ReadFile(filename) + + var jsonSource []TableInfo + err := json.Unmarshal([]byte(byteValue), &jsonSource) if err != nil { log.Println(err) } - defer jsonFile.Close() - - byteValue, _ := ioutil.ReadAll(jsonFile) - - var jsonSource []TableInfo - json.Unmarshal([]byte(byteValue), &jsonSource) for _, ti := range jsonSource { ti.db = db err = db.CreateTable(ti) @@ -422,19 +418,13 @@ func (db *Db) ImportSchema(filename string) { } func (db *Db) ClearImportSchema(filename string) { - jsonFile, err := os.Open(filename) - if err != nil { - log.Println(err) - } - defer jsonFile.Close() - - byteValue, _ := ioutil.ReadAll(jsonFile) + byteValue, _ := os.ReadFile(filename) var jsonSource []TableInfo json.Unmarshal([]byte(byteValue), &jsonSource) for _, ti := range jsonSource { ti.db = db - err = ti.DeleteTable() + err := ti.DeleteTable() if err != nil { log.Println(err.Error()) } @@ -600,7 +590,7 @@ func (db *Db) SaveSchema(generatedFilename string) error { } // file, _ := json.Marshal(schema) file, _ := json.MarshalIndent(schema, "", " ") - _ = ioutil.WriteFile(generatedFilename, file, 0644) + _ = os.WriteFile(generatedFilename, file, 0644) return nil } diff --git a/my_test.go b/my_test.go index a1fe5a0..ca83097 100755 --- a/my_test.go +++ b/my_test.go @@ -3,7 +3,6 @@ package sqldb import ( "encoding/json" "fmt" - "io/ioutil" "os" "testing" ) @@ -12,18 +11,12 @@ func TestMyCreateTable(t *testing.T) { db := Open("mysql", "test:test@tcp(127.0.0.1:3306)/test?parseTime=true") defer db.Close() - jsonFile, err := os.Open("test_table.json") - if err != nil { - fmt.Println(err) - } - defer jsonFile.Close() - - byteValue, _ := ioutil.ReadAll(jsonFile) + byteValue, _ := os.ReadFile("test_table.json") var jsonSource TableInfo json.Unmarshal([]byte(byteValue), &jsonSource) - err = db.CreateTable(jsonSource) + err := db.CreateTable(jsonSource) if err != nil { fmt.Println(err.Error()) } @@ -88,10 +81,9 @@ func TestMyInsert(t *testing.T) { if len(jsonStringOld) == len(jsonStringNew) { t.Errorf("Error row not created") } - jsonFile, err := os.Open("insert.json") - defer jsonFile.Close() + var result map[string]interface{} - byteValue, _ := ioutil.ReadAll(jsonFile) + byteValue, _ := os.ReadFile("insert.json") json.Unmarshal(byteValue, &result) } diff --git a/pg_test.go b/pg_test.go index 97a7887..11317cb 100755 --- a/pg_test.go +++ b/pg_test.go @@ -3,7 +3,6 @@ package sqldb import ( "encoding/json" "fmt" - "io/ioutil" "os" "testing" ) @@ -12,18 +11,12 @@ func TestPgCreateTable(t *testing.T) { db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable") defer db.Close() - jsonFile, err := os.Open("test_table.json") - if err != nil { - fmt.Println(err) - } - defer jsonFile.Close() - - byteValue, _ := ioutil.ReadAll(jsonFile) + byteValue, _ := os.ReadFile("test_table.json") var jsonSource TableInfo json.Unmarshal([]byte(byteValue), &jsonSource) - err = db.CreateTable(jsonSource) + err := db.CreateTable(jsonSource) if err != nil { fmt.Println(err.Error()) } @@ -88,10 +81,8 @@ func TestPgInsert(t *testing.T) { if len(jsonStringOld) == len(jsonStringNew) { t.Errorf("Error row not created") } - jsonFile, err := os.Open("insert.json") - defer jsonFile.Close() var result map[string]interface{} - byteValue, _ := ioutil.ReadAll(jsonFile) + byteValue, _ := os.ReadFile("insert.json") json.Unmarshal(byteValue, &result) } diff --git a/test_table.json b/test_table.json index 456bd39..0122342 100644 --- a/test_table.json +++ b/test_table.json @@ -5,8 +5,8 @@ "id":"integer", "name":"varchar(255)|comment", "description":"varchar(1000)", - "startdate":"timestamp", - "enddate":"timestamp", + "startdate":"date", + "endtime":"timestamp", "latitude":"float|map", "longitude":"float|map", "intvalue":"integer",