Skip to content

Commit

Permalink
Merge pull request #3 from misterunix/addion
Browse files Browse the repository at this point in the history
Added strings to jsonio and updated the readme
  • Loading branch information
misterunix committed Oct 27, 2021
2 parents bf0010d + ba6b229 commit 8977ba5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# sniffle

What a name right? When creating this repo Github showed.

```Great repository names are short and memorable. Need inspiration? How about shiny-sniffle?```

And that cracked me up, so I chose sniffle. What the heck right?

## What is it?
Expand All @@ -12,25 +14,27 @@ I have never made a public module before and wanted to give it a shot and see wh

## What is there?
- Hashing functions.
- file hash
- string hash
- Return the MD5, SHA256 or SHA512 of a file.
- Return the MD5, SHA256 or SHA512 of a string.
- JSON functions - this is the big one for me. I use JSON a lot!
- write a slice of a struct to a file
- read a file into a slice of a struct
- write a slice of a struct or a struct to a file
- write a slice of a struct or a struct to a string
- read a file into a slice of a struct or a struct
- read a string into a slive of a struct or a struct
- CPU and system functions.
- uses on go only packages
- uses on go packages only
- gets CPU cores and type
- gets os and pid
- gets Go version and number of go routines.
- This will get more as I need them.
- More functions will be added over time.
- String
- Left justify with padding.
- Right justify with padding.
- Center with padded, optional padding on the right.
- Left justify a string padding out to a set width.
- Right justify a string with padding to a set width.
- Center a string with padding, optional padding on the right.
- Split lines at width and/or word boundry.

## Why in the world did you do this.
Well, as I said, it's stuff I use over and over again. We all know there are 100 different ways to do something in go and this is just my way. :beer:
Well, as I said, it's stuff I use over and over again. We all know there are 100 different ways to do something in Go and this is just my way. :beer:

## Contribs?
Sure, if they only require the standard Go library. Let's do it.
Expand Down
30 changes: 28 additions & 2 deletions jsonio/jsonio.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@ import (
"errors"
"io/ioutil"
"os"
"strings"
)

// LoadJSon : Loads a slice of a struct data from JSon file.
// LoadJSonFromString : Load a stuct or a slice of structs from string s
func LoadJSonFromString(s string, js interface{}) error {
if len(s) <= 0 {
return errors.New("string is empty")
}

r := strings.NewReader(s)
decoder := json.NewDecoder(r)
err := decoder.Decode(&js)
if err != nil {
return (err)
}

return nil
}

// LoadJSon : Loads a struct or a slice of a struct data from JSon file.
func LoadJSon(filename string, js interface{}) error {

if !fileExists(filename) {
Expand All @@ -33,7 +50,16 @@ func LoadJSon(filename string, js interface{}) error {
return nil
}

// SaveJSon : Saves a struct slice to a file. Creating it if needed.
// SaveJSonToString : Saves struct or a slice to a string
func SaveJSonToString(js interface{}) (string, error) {
rbytes, err := json.MarshalIndent(js, "", " ")
if err != nil {
return "", err
}
return string(rbytes), nil
}

// SaveJSon : Saves a struct or a struct slice to a file. Creating it if needed.
func SaveJSon(filename string, js interface{}) error {
file, _ := json.MarshalIndent(js, "", " ")
err := ioutil.WriteFile(filename, file, 0644)
Expand Down
29 changes: 29 additions & 0 deletions jsonio/jsonio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsonio

import (
"os"
"strings"
"testing"
)

Expand All @@ -11,7 +12,13 @@ type TmpStruct struct {
C bool
}

type Example struct {
Text string
MD5 string
}

var TheStruct []TmpStruct
var Ejson Example

func TestBoth(t *testing.T) {

Expand Down Expand Up @@ -50,4 +57,26 @@ func TestBoth(t *testing.T) {
if TheStruct[2].A != 30 || TheStruct[2].B != "Three" || TheStruct[2].C != true {
t.Errorf("LoadJSon want %d %s %t got %d %s %t", 30, "Three", true, TheStruct[2].A, TheStruct[2].B, TheStruct[2].C)
}

es := "{ \"text\": \"example_text\", \"md5\": \"fa4c6baa0812e5b5c80ed8885e55a8a6\" }"
err = LoadJSonFromString(es, &Ejson)
if err != nil {
t.Errorf("LoadJSonFromString returned an error: %s", err)
}
if strings.Compare(Ejson.Text, "example_text") != 0 || strings.Compare(Ejson.MD5, "fa4c6baa0812e5b5c80ed8885e55a8a6") != 0 {
t.Errorf("LoadJSonFromString wanted text:%s md5:%s but got text:%s md5:%s", "example_text", "fa4c6baa0812e5b5c80ed8885e55a8a6", Ejson.Text, Ejson.MD5)
}

// Wow this test was hard!!!!
es = `{
"Text": "example_text",
"MD5": "fa4c6baa0812e5b5c80ed8885e55a8a6"
}`
rs, err := SaveJSonToString(Ejson)
if err != nil {
t.Errorf("SaveJSonToString returned an error: %s", err)
}
if strings.Compare(rs, es) != 0 {
t.Errorf("SaveJSonToString: wanted\n%s got \n%s", es, rs)
}
}

0 comments on commit 8977ba5

Please sign in to comment.