Skip to content

Commit

Permalink
feat - add array and crypt, and complete other modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Oct 8, 2023
1 parent 56545a1 commit 59037f4
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 6 deletions.
10 changes: 10 additions & 0 deletions array/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package array

func StringIn(key string, list []string) bool {
for _, l := range list {
if l == key {
return true
}
}
return false
}
28 changes: 28 additions & 0 deletions array/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package array_test

import (
"testing"

"github.com/debeando/go-common/array"
)

func TestStringIn(t *testing.T) {
list := []string{"foo", "bar"}
result := array.StringIn("bar", list)

if !result {
t.Error("Expected: false")
}

result = array.StringIn("test", list)

if result {
t.Error("Expected: true")
}

result = array.StringIn("", list)

if result {
t.Error("Expected: false")
}
}
18 changes: 18 additions & 0 deletions cast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ func ToDateTime(timestamp string, layout string) string {
}
return t.Format("2006-01-02 15:04:05")
}

func IntToString(value int) string {
return strconv.Itoa(value)
}

func InterfaceToInt64(value interface{}) int64 {
if v, ok := value.(int64); ok {
return v
}
return 0
}

func InterfaceToFloat64(value interface{}) float64 {
if v, ok := value.(float64); ok {
return float64(v)
}
return 0
}
11 changes: 11 additions & 0 deletions crypt/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package crypt

import (
"crypto/md5"
"encoding/hex"
)

func MD5(s string) string {
hash := md5.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}
16 changes: 16 additions & 0 deletions crypt/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package crypt_test

import (
"testing"

"github.com/debeando/go-common/crypt"
)

func TestMD5(t *testing.T) {
expected := "098f6bcd4621d373cade4e832627b4f6"
result := crypt.MD5("test")

if result != expected {
t.Error("Expected: 098f6bcd4621d373cade4e832627b4f6")
}
}
65 changes: 65 additions & 0 deletions file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,68 @@ func GetInt64(path string) int64 {
}
return 0
}

func GetInt(path string) int {
lines := ReadAsString(path)
if len(lines) > 0 {
return cast.StringToInt(lines)
}
return 0
}

func Create(f string) bool {
if !Exist(f) {
var file, err = os.Create(f)
if err != nil {
return false
}
defer file.Close()
}

return true
}

func Write(f string, s string) bool {
// open file using READ & WRITE permission
file, err := os.OpenFile(f, os.O_RDWR, 0644)
defer file.Close()
if err != nil {
return false
}

// write some text line-by-line to file
_, err = file.WriteString(s)
if err != nil {
return false
}

// save changes
err = file.Sync()
if err != nil {
return false
}

return true
}

func Truncate(f string) bool {
// open file using READ & WRITE permission
file, err := os.OpenFile(f, os.O_RDWR, 0644)
defer file.Close()
if err != nil {
return false
}

file.Truncate(0)
file.Seek(0, 0)
file.Sync()

return true
}

func Delete(f string) bool {
if err := os.Remove(f); err != nil {
return false
}
return true
}
69 changes: 68 additions & 1 deletion file/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,75 @@ package file_test

import (
"os"
"path/filepath"
"testing"

"github.com/debeando/go-common/file"
)

var wd string
var (
wd string
twd string
tf string
)

func init() {
ex, _ := os.Executable()
twd = filepath.Dir(ex)
tf = twd + "/zenit.txt"
}

func TestMain(m *testing.M) {
wd, _ = os.Getwd()
}

func TestExist(t *testing.T) {
if file.Exist(tf) {
t.Error("The file exist, should be not.")
}
}

func TestCreate(t *testing.T) {
if !file.Create(tf) {
t.Error("Problem to create file.")
}

if _, err := os.Stat(tf); os.IsNotExist(err) {
t.Error("File not exist in: zenit.txt")
}
}

func TestWrite(t *testing.T) {
if !file.Write(tf, "Test 1\nTest 2") {
t.Error("Problem to write in file.")
}
}

func TestRead(t *testing.T) {
result := file.ReadAsString(tf)
expected := "Test 1\nTest 2"

if result != expected {
t.Errorf("Expected: '%s', got: '%s'.", expected, result)
}
}

func TestTruncate(t *testing.T) {
if !file.Truncate(tf) {
t.Error("Problem to truncate file.")
}

if len(file.Read(tf)) != 0 {
t.Error("Is not truncated file.")
}
}

func TestDelete(t *testing.T) {
if !file.Delete(tf) {
t.Error("Problem to delete file.")
}
}

func TestGetInt64(t *testing.T) {
expected := int64(1234567890)
result := file.GetInt64(wd + "/../assets/tests/int64.txt")
Expand All @@ -28,3 +86,12 @@ func TestGetInt64(t *testing.T) {
t.Error("Expected: int64(0)")
}
}

func TestGetInt(t *testing.T) {
expected := 1234567890
result := file.GetInt(wd + "/../assets/tests/int.txt")

if result != expected {
t.Error("Expected: 1234567890")
}
}
18 changes: 18 additions & 0 deletions maps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@ func In(key string, list map[string]string) bool {
}
return false
}

func ComparteString(a, b map[string]string) bool {
if (a == nil) != (b == nil) {
return false
}

if len(a) != len(b) {
return false
}

for k := range a {
if a[k] != b[k] {
return false
}
}

return true
}
24 changes: 19 additions & 5 deletions mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,37 @@ type MySQL struct {

type Connection struct {
Instance *sql.DB
Name string
DSN string
Name string
DSN string
}

var instance = make(map[string]*Connection)

func GetInstance(name string) *Connection {
func New(name, dsn string) *Connection {
if instance[name] == nil {
instance[name] = &Connection{}
instance[name] = &Connection{
Name: name,
DSN: dsn,
}
instance[name].Name = name
}
return instance[name]
}

func Get(name string) *Connection {
return instance[name]
}

func (c *Connection) Connect() error {
if c.Instance == nil {
conn, err := sql.Open("mysql", c.DSN)
if err != nil {
log.ErrorWithFields("MySQL execute", log.Fields{"name": c.Name, "message": err})
return err
}

if err := conn.Ping(); err != nil {
log.ErrorWithFields("MySQL execute", log.Fields{"name": c.Name, "message": err})
return err
}

Expand All @@ -53,23 +62,27 @@ func (c *Connection) Connect() error {

func (c *Connection) Query(query string) (map[int]map[string]string, error) {
log.DebugWithFields("MySQL execute", log.Fields{
"Query": query,
"name": c.Name,
"query": query,
})

if err := c.Instance.Ping(); err != nil {
log.ErrorWithFields("MySQL execute", log.Fields{"name": c.Name, "message": err})
return nil, err
}

// Execute the query
rows, err := c.Instance.Query(query)
if err != nil {
log.ErrorWithFields("MySQL execute", log.Fields{"name": c.Name, "message": err})
return nil, err
}
defer rows.Close()

// Get column names
cols, _ := rows.Columns()
if err != nil {
log.ErrorWithFields("MySQL execute", log.Fields{"name": c.Name, "message": err})
return nil, err
}

Expand All @@ -85,6 +98,7 @@ func (c *Connection) Query(query string) (map[int]map[string]string, error) {
for rows.Next() {
err = rows.Scan(columnPointers...)
if err != nil {
log.ErrorWithFields("MySQL execute", log.Fields{"name": c.Name, "message": err})
return nil, err
}

Expand Down
Loading

0 comments on commit 59037f4

Please sign in to comment.