diff --git a/diff.go b/diff.go index 6dadac1..00d76ea 100644 --- a/diff.go +++ b/diff.go @@ -30,3 +30,34 @@ func Diff(old, new []int64) (added, removed []int64) { return added, removed } + +// DiffString ... +func DiffString(old, new []string) (added, removed []string) { + for i := range old { + isRemoved := true + for j := range new { + if old[i] == new[j] { + isRemoved = false + break + } + } + if isRemoved { + removed = append(removed, old[i]) + } + } + + for i := range new { + isAdded := true + for j := range old { + if new[i] == old[j] { + isAdded = false + break + } + } + if isAdded { + added = append(added, new[i]) + } + } + + return added, removed +} diff --git a/http.go b/http.go index af16056..7e04a44 100644 --- a/http.go +++ b/http.go @@ -1,12 +1,10 @@ package webutility import ( - "bytes" "encoding/json" "fmt" "io" "net/http" - "net/url" ) // StatusRecorder ... @@ -158,71 +156,3 @@ func InternalServerError(w http.ResponseWriter, r *http.Request, err string) { func DecodeJSON(r io.Reader, v interface{}) error { return json.NewDecoder(r).Decode(v) } - -func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) { - p := params.Encode() - if p != "" { - url += "?" + p - } - - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - return 0, err - } - - if headers != nil { - for k, head := range headers { - for i, h := range head { - if i == 0 { - req.Header.Set(k, h) - } else { - req.Header.Add(k, h) - } - } - } - } - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return 0, err - } - status = resp.StatusCode - - return status, DecodeJSON(resp.Body, v) -} - -func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) { - buffer := bytes.NewBuffer(make([]byte, 0)) - json.NewEncoder(buffer).Encode(v) - - p := params.Encode() - if p != "" { - url += "?" + p - } - - req, err := http.NewRequest(http.MethodPost, url, buffer) - if err != nil { - return 0, err - } - - if headers != nil { - for k, head := range headers { - for i, h := range head { - if i == 0 { - req.Header.Set(k, h) - } else { - req.Header.Add(k, h) - } - } - } - } - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return 0, err - } - status = resp.StatusCode - - return status, DecodeJSON(resp.Body, r) -} diff --git a/json.go b/json.go new file mode 100644 index 0000000..4e51ab1 --- /dev/null +++ b/json.go @@ -0,0 +1,76 @@ +package webutility + +import ( + "bytes" + "encoding/json" + "net/http" + "net/url" +) + +func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) { + p := params.Encode() + if p != "" { + url += "?" + p + } + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return 0, err + } + + if headers != nil { + for k, head := range headers { + for i, h := range head { + if i == 0 { + req.Header.Set(k, h) + } else { + req.Header.Add(k, h) + } + } + } + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return 0, err + } + status = resp.StatusCode + + return status, DecodeJSON(resp.Body, v) +} + +func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) { + buffer := bytes.NewBuffer(make([]byte, 0)) + json.NewEncoder(buffer).Encode(v) + + p := params.Encode() + if p != "" { + url += "?" + p + } + + req, err := http.NewRequest(http.MethodPost, url, buffer) + if err != nil { + return 0, err + } + + if headers != nil { + for k, head := range headers { + for i, h := range head { + if i == 0 { + req.Header.Set(k, h) + } else { + req.Header.Add(k, h) + } + } + } + } + req.Header.Set("Content-Type", "application/json") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return 0, err + } + status = resp.StatusCode + + return status, DecodeJSON(resp.Body, r) +}