Skip to content

Commit

Permalink
update pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
David Wertenteil committed Apr 17, 2022
1 parent 060b821 commit c6c57c6
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 3 deletions.
2 changes: 1 addition & 1 deletion utils/pointerconerter.go → boolutils/pointerconerter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package boolutils

import (
"strings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package boolutils

import (
"testing"
Expand Down
21 changes: 21 additions & 0 deletions httputils/httphelper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package shared

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSetHeaders(t *testing.T) {
{
req, _ := http.NewRequest("", "", nil)
setHeaders(req, nil)
assert.Equal(t, 0, len(req.Header))
}
{
req, _ := http.NewRequest("", "", nil)
setHeaders(req, map[string]string{"a": "aa", "b": "bb"})
assert.Equal(t, 2, len(req.Header))
}
}
88 changes: 88 additions & 0 deletions httputils/httphelpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package shared

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)

// JSONDecoder returns JSON decoder for given string
func JSONDecoder(origin string) *json.Decoder {
dec := json.NewDecoder(strings.NewReader(origin))
dec.UseNumber()
return dec
}

func HttpDelete(httpClient *http.Client, fullURL string, headers map[string]string) (*http.Response, error) {

req, err := http.NewRequest("DELETE", fullURL, nil)
if err != nil {
return nil, err
}
setHeaders(req, headers)

return httpClient.Do(req)
}
func HttpGet(httpClient *http.Client, fullURL string, headers map[string]string) (*http.Response, error) {

req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
return nil, err
}
setHeaders(req, headers)

return httpClient.Do(req)
}

func HttpPost(httpClient *http.Client, fullURL string, headers map[string]string, body []byte) (*http.Response, error) {

req, err := http.NewRequest("POST", fullURL, bytes.NewReader(body))
if err != nil {
return nil, err
}
setHeaders(req, headers)

return httpClient.Do(req)
}

func setHeaders(req *http.Request, headers map[string]string) {
if len(headers) > 0 { // might be nil
for k, v := range headers {
req.Header.Set(k, v)
}
}
}

// HTTPRespToString parses the body as string and checks the HTTP status code, it closes the body reader at the end
func HttpRespToString(resp *http.Response) (string, error) {
if resp == nil || resp.Body == nil {
return "", nil
}
strBuilder := strings.Builder{}
defer resp.Body.Close()
if resp.ContentLength > 0 {
strBuilder.Grow(int(resp.ContentLength))
}
_, err := io.Copy(&strBuilder, resp.Body)
respStr := strBuilder.String()
if err != nil {
respStrNewLen := len(respStr)
if respStrNewLen > 1024 {
respStrNewLen = 1024
}
return "", fmt.Errorf("http-error: '%s', reason: '%s'", resp.Status, respStr[:respStrNewLen])
// return "", fmt.Errorf("HTTP request failed. URL: '%s', Read-ERROR: '%s', HTTP-CODE: '%s', BODY(top): '%s', HTTP-HEADERS: %v, HTTP-BODY-BUFFER-LENGTH: %v", resp.Request.URL.RequestURI(), err, resp.Status, respStr[:respStrNewLen], resp.Header, bytesNum)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
respStrNewLen := len(respStr)
if respStrNewLen > 1024 {
respStrNewLen = 1024
}
err = fmt.Errorf("http-error: '%s', reason: '%s'", resp.Status, respStr[:respStrNewLen])
}

return respStr, err
}
43 changes: 43 additions & 0 deletions str/has_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package str

import (
"testing"

"github.com/stretchr/testify/assert"
)

type object struct {
a string
b bool
c interface{}
}

func TestAsSHA256(t *testing.T) {
o := object{
a: "aaa",
b: true,
c: object{
a: "ooop",
},
}
assert.Equal(t, "0afe3bec0a40a0d63fb0a570f3fffb572e46b6e0fb4a1eda8d8c055903d121c6", AsSHA256(o))

o.b = false
assert.Equal(t, "0a9659419d14f92700f028dffa8b7a9a50686c0278a4846b082c89a2b45f5faf", AsSHA256(o))

}

func TestAsFNVHash(t *testing.T) {
o := object{
a: "aaa",
b: true,
c: object{
a: "ooop",
},
}
assert.Equal(t, "1583525587", AsFNVHash(o))

o.b = false
assert.Equal(t, "848808146", AsFNVHash(o))

}
2 changes: 1 addition & 1 deletion utils/hash.go → str/hash.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package str

import (
"crypto/sha256"
Expand Down
41 changes: 41 additions & 0 deletions str/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package str

import "strings"

// StringInSlice return true if string found in slice of strings
func StringInSlice(strSlice []string, str string) bool {
for i := range strSlice {
if strSlice[i] == str {
return true
}
}
return false
}

// StringInSliceCaseInsensitive return true if string found in slice of strings, ignore case sensitive
func StringInSliceCaseInsensitive(strSlice []string, str string) bool {
for i := range strSlice {
if strings.EqualFold(strSlice[i], str) {
return true
}
}
return false
}

// MapStringToSlice returns map's keys
func MapStringToSlice(strMap map[string]interface{}) []string {
strSlice := []string{}
for k := range strMap {
strSlice = append(strSlice, k)
}
return strSlice
}

// SliceStringToUnique returns unique values of slice
func SliceStringToUnique(strSlice []string) []string {
strMap := map[string]interface{}{}
for i := range strSlice {
strMap[strSlice[i]] = nil
}
return MapStringToSlice(strMap)
}
41 changes: 41 additions & 0 deletions str/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package str

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestStringInSlice(t *testing.T) {
assert.True(t, StringInSlice([]string{"a"}, "a"))
assert.True(t, StringInSlice([]string{"a", "b", "c"}, "a"))
assert.True(t, StringInSlice([]string{"a", "b", "c"}, "b"))
assert.True(t, StringInSlice([]string{"a", "b", "c"}, "c"))
assert.True(t, StringInSlice([]string{"a", "a"}, "a"))
assert.False(t, StringInSlice([]string{"a", "b", "c"}, "d"))
assert.False(t, StringInSlice([]string{""}, "a"))
assert.False(t, StringInSlice([]string{"a"}, ""))
}

func TestStringInSliceCaseInsensitive(t *testing.T) {
assert.True(t, StringInSliceCaseInsensitive([]string{"A"}, "a"))
assert.True(t, StringInSliceCaseInsensitive([]string{"a"}, "A"))
assert.True(t, StringInSlice([]string{"A", "a", "b", "c"}, "a"))
assert.True(t, StringInSlice([]string{"a", "Bb", "cC"}, "cC"))
assert.True(t, StringInSlice([]string{"a", "Bb", "Cc"}, "Cc"))
assert.True(t, StringInSlice([]string{"a", "Bb", "C c"}, "C c"))
assert.False(t, StringInSlice([]string{"a", "bb", "c"}, "b"))
}

func TestMapStringToSlice(t *testing.T) {
assert.ElementsMatch(t, MapStringToSlice(map[string]interface{}{"a": nil}), []string{"a"})
assert.ElementsMatch(t, MapStringToSlice(map[string]interface{}{"a": nil, "b": nil}), []string{"a", "b"})
assert.ElementsMatch(t, MapStringToSlice(nil), []string{})
assert.ElementsMatch(t, MapStringToSlice(map[string]interface{}{}), []string{})
}

func TestSliceStringToUnique(t *testing.T) {
assert.ElementsMatch(t, SliceStringToUnique([]string{"a"}), []string{"a"})
assert.ElementsMatch(t, SliceStringToUnique([]string{}), []string{})
assert.ElementsMatch(t, SliceStringToUnique([]string{"a", "b", "b", "a"}), []string{"a", "b"})
}

0 comments on commit c6c57c6

Please sign in to comment.