mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50c1033ad1 | ||
|
|
7a791965ca | ||
|
|
05b057c67c | ||
|
|
9c729d814a | ||
|
|
9d5e2122b8 | ||
|
|
a19f1d257a | ||
|
|
563c952c78 | ||
|
|
2f259a4b3f | ||
|
|
957857537a | ||
|
|
4ea4bbb6cc | ||
|
|
36ef16ad3d | ||
|
|
42170f9b8e | ||
|
|
4f438b7eac | ||
|
|
569867d04b | ||
|
|
9a58df7a54 | ||
|
|
4e60a90ce4 | ||
|
|
2f4c8cc5d8 | ||
|
|
154233df14
|
||
|
|
29afbd08fd
|
||
|
|
a107ee953b
|
@@ -0,0 +1,73 @@
|
|||||||
|
name: Autoupdate go.mod and go.sum
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
schedule:
|
||||||
|
- cron: "0 3 * * *"
|
||||||
|
|
||||||
|
env:
|
||||||
|
GO_VERSION: ">=1.21"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
# This job is responsible for preparation of the build
|
||||||
|
# environment variables.
|
||||||
|
prepare:
|
||||||
|
name: Preparing build context
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repo
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
id: cache
|
||||||
|
with:
|
||||||
|
go-version: ${{env.GO_VERSION}}
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
|
||||||
|
- name: Go get dependencies
|
||||||
|
if: steps.cache.outputs.cache-hit != 'true'
|
||||||
|
run: |
|
||||||
|
go get ./...
|
||||||
|
|
||||||
|
# This job is responsible for running tests and linting the codebase
|
||||||
|
test:
|
||||||
|
name: "Unit testing"
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container: golang:1
|
||||||
|
needs: [prepare]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0 # Ensure full history is checked out
|
||||||
|
token: ${{ secrets.GHCR_TOKEN }}
|
||||||
|
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: ${{env.GO_VERSION}}
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
apt-get update
|
||||||
|
apt-get install ca-certificates make -y
|
||||||
|
update-ca-certificates
|
||||||
|
go mod tidy
|
||||||
|
go get -u -v ./...
|
||||||
|
go mod tidy -v
|
||||||
|
|
||||||
|
- name: Run unit tests
|
||||||
|
run: |
|
||||||
|
CI_RUN=${CI} make test
|
||||||
|
git config --global --add safe.directory /__w/go-simple-graphql/go-simple-graphql
|
||||||
|
|
||||||
|
- name: Commit changes
|
||||||
|
uses: stefanzweifel/git-auto-commit-action@v5
|
||||||
|
with:
|
||||||
|
commit_message: "Update go.mod and go.sum"
|
||||||
|
commit_options: "--no-verify --signoff"
|
||||||
|
file_pattern: "go.mod go.sum"
|
||||||
Vendored
+52
-47
@@ -3,7 +3,6 @@ package libpack_cache
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"flag"
|
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -15,26 +14,32 @@ type CacheEntry struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
bytePool sync.Pool
|
compressPool sync.Pool
|
||||||
entries sync.Map
|
decompressPool sync.Pool
|
||||||
globalTTL time.Duration
|
entries sync.Map
|
||||||
|
globalTTL time.Duration
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(globalTTL time.Duration) *Cache {
|
func New(globalTTL time.Duration) *Cache {
|
||||||
cache := &Cache{
|
cache := &Cache{
|
||||||
globalTTL: globalTTL,
|
globalTTL: globalTTL,
|
||||||
|
compressPool: sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
w := gzip.NewWriter(nil)
|
||||||
|
return w
|
||||||
|
},
|
||||||
|
},
|
||||||
|
decompressPool: sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
// Ensure that new is returning a new reader initialized with an empty byte buffer
|
||||||
|
r, _ := gzip.NewReader(bytes.NewReader([]byte{}))
|
||||||
|
return r
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the byte pool.
|
go cache.cleanupRoutine(globalTTL)
|
||||||
cache.bytePool.New = func() interface{} {
|
|
||||||
return make([]byte, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the cache cleanup.
|
|
||||||
if flag.Lookup("test.v") == nil {
|
|
||||||
go cache.cleanupRoutine(globalTTL)
|
|
||||||
}
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,10 +51,10 @@ func (c *Cache) cleanupRoutine(globalTTL time.Duration) {
|
|||||||
c.CleanExpiredEntries()
|
c.CleanExpiredEntries()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
||||||
c.Lock()
|
c.Lock() // use the lock
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
||||||
expiresAt := time.Now().Add(ttl)
|
expiresAt := time.Now().Add(ttl)
|
||||||
|
|
||||||
compressedValue, err := c.compress(value)
|
compressedValue, err := c.compress(value)
|
||||||
@@ -57,25 +62,15 @@ func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a byte slice from the pool and ensure it's properly sized.
|
|
||||||
b := c.bytePool.Get().([]byte)
|
|
||||||
if cap(b) < len(compressedValue) {
|
|
||||||
b = make([]byte, len(compressedValue))
|
|
||||||
} else {
|
|
||||||
b = b[:len(compressedValue)]
|
|
||||||
}
|
|
||||||
|
|
||||||
copy(b, compressedValue)
|
|
||||||
|
|
||||||
entry := CacheEntry{
|
entry := CacheEntry{
|
||||||
Value: b,
|
Value: compressedValue,
|
||||||
ExpiresAt: expiresAt,
|
ExpiresAt: expiresAt,
|
||||||
}
|
}
|
||||||
c.entries.Store(key, entry)
|
c.entries.Store(key, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Get(key string) ([]byte, bool) {
|
func (c *Cache) Get(key string) ([]byte, bool) {
|
||||||
c.RLock()
|
c.RLock() // use the read lock
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
|
|
||||||
entry, ok := c.entries.Load(key)
|
entry, ok := c.entries.Load(key)
|
||||||
@@ -95,15 +90,11 @@ func (c *Cache) Delete(key string) {
|
|||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
||||||
entry, ok := c.entries.Load(key)
|
_, ok := c.entries.Load(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the byte slice to the pool.
|
|
||||||
c.bytePool.Put(entry.(CacheEntry).Value)
|
|
||||||
|
|
||||||
// Delete the entry from the cache.
|
|
||||||
c.entries.Delete(key)
|
c.entries.Delete(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,37 +103,51 @@ func (c *Cache) CleanExpiredEntries() {
|
|||||||
c.entries.Range(func(key, value interface{}) bool {
|
c.entries.Range(func(key, value interface{}) bool {
|
||||||
entry := value.(CacheEntry)
|
entry := value.(CacheEntry)
|
||||||
if entry.ExpiresAt.Before(now) {
|
if entry.ExpiresAt.Before(now) {
|
||||||
// Return the byte slice to the pool.
|
|
||||||
c.bytePool.Put(entry.Value)
|
|
||||||
|
|
||||||
// Delete the entry from the cache.
|
|
||||||
c.entries.Delete(key)
|
c.entries.Delete(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return true to continue iterating over the map.
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) compress(data []byte) ([]byte, error) {
|
func (c *Cache) compress(data []byte) ([]byte, error) {
|
||||||
|
w := c.compressPool.Get().(*gzip.Writer)
|
||||||
|
defer c.compressPool.Put(w)
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
w := gzip.NewWriter(&buf)
|
w.Reset(&buf)
|
||||||
_, err := w.Write(data)
|
if _, err := w.Write(data); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = w.Close()
|
if err := w.Close(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) decompress(data []byte) ([]byte, error) {
|
func (c *Cache) decompress(data []byte) ([]byte, error) {
|
||||||
r, err := gzip.NewReader(bytes.NewBuffer(data))
|
r, ok := c.decompressPool.Get().(*gzip.Reader)
|
||||||
if err != nil {
|
if !ok || r == nil {
|
||||||
return nil, err
|
// If r is nil or type assertion fails, create a new gzip.Reader
|
||||||
|
var err error
|
||||||
|
r, err = gzip.NewReader(bytes.NewReader(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err // Handle the error if gzip.NewReader fails
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Reset the existing reader with new data
|
||||||
|
if err := r.Reset(bytes.NewReader(data)); err != nil {
|
||||||
|
return nil, err // Handle the error if Reset fails
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
return io.ReadAll(r)
|
|
||||||
|
// Ensure the reader is returned to the pool
|
||||||
|
defer c.decompressPool.Put(r)
|
||||||
|
|
||||||
|
// Read all the data from the reader
|
||||||
|
decompressedData, err := io.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err // Handle the error if reading fails
|
||||||
|
}
|
||||||
|
return decompressedData, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+46
@@ -0,0 +1,46 @@
|
|||||||
|
package libpack_cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Assume that New function initializes the cache and it is defined somewhere in the libpack_cache package.
|
||||||
|
|
||||||
|
func BenchmarkCacheSet(b *testing.B) {
|
||||||
|
cache := New(30 * time.Second) // Initializing the cache with a TTL of 30 seconds
|
||||||
|
key := "benchmark-key"
|
||||||
|
value := []byte("benchmark-value")
|
||||||
|
|
||||||
|
b.ResetTimer() // Reset the timer to exclude the setup time from the benchmark
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
cache.Set(key, value, 5*time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCacheGet(b *testing.B) {
|
||||||
|
cache := New(30 * time.Second) // Initializing the cache
|
||||||
|
key := "benchmark-key"
|
||||||
|
value := []byte("benchmark-value")
|
||||||
|
cache.Set(key, value, 5*time.Second) // Pre-set a value to retrieve
|
||||||
|
|
||||||
|
b.ResetTimer() // Start timing
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _ = cache.Get(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCacheExpire(b *testing.B) {
|
||||||
|
key := "benchmark-expire-key"
|
||||||
|
value := []byte("benchmark-value")
|
||||||
|
ttl := 5 * time.Millisecond // Setting a short TTL for quick expiration
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
cache := New(30 * time.Second)
|
||||||
|
cache.Set(key, value, ttl)
|
||||||
|
time.Sleep(ttl) // Wait for the key to expire
|
||||||
|
_, _ = cache.Get(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
-36
@@ -110,39 +110,3 @@ func (suite *CacheTestSuite) Test_CacheExpire() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *CacheTestSuite) Test_CacheCleanExpiredEntries() {
|
|
||||||
cache := New(5 * time.Second)
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
cache_value string
|
|
||||||
ttl time.Duration
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "test1",
|
|
||||||
cache_value: "test1-123",
|
|
||||||
ttl: 2 * time.Second,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "test2",
|
|
||||||
cache_value: "test2-123",
|
|
||||||
ttl: 5 * time.Second,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
suite.T().Run(tt.name, func(t *testing.T) {
|
|
||||||
cache.Set(tt.name, []byte(tt.name), tt.ttl)
|
|
||||||
c, ok := cache.Get(tt.name)
|
|
||||||
suite.Equal(true, ok)
|
|
||||||
suite.Equal(tt.name, string(c))
|
|
||||||
time.Sleep(tt.ttl)
|
|
||||||
c, ok = cache.Get(tt.name)
|
|
||||||
suite.Equal(false, ok)
|
|
||||||
suite.Equal("", string(c))
|
|
||||||
cache.CleanExpiredEntries()
|
|
||||||
c, ok = cache.Get(tt.name)
|
|
||||||
suite.Equal(false, ok)
|
|
||||||
suite.Equal("", string(c))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+91
-36
@@ -2,99 +2,154 @@ package gql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/avast/retry-go/v4"
|
"github.com/avast/retry-go/v4"
|
||||||
|
"github.com/goccy/go-json"
|
||||||
|
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var bufferPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return new(bytes.Buffer)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||||
httpRequest, err := http.NewRequest("POST", qe.endpoint, bytes.NewBuffer(qe.Query))
|
// Reuse buffer from pool to avoid allocations
|
||||||
|
buf := bufferPool.Get().(*bytes.Buffer)
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
buf.Write(qe.Query)
|
||||||
|
|
||||||
|
httpRequest, err := http.NewRequest(http.MethodPost, qe.endpoint, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qe.Logger.Error("Can't create HTTP request;", map[string]interface{}{"error": err})
|
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Can't create HTTP request",
|
||||||
|
Pairs: map[string]interface{}{"error": err.Error()},
|
||||||
|
})
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range qe.Headers {
|
for key, value := range qe.Headers {
|
||||||
httpRequest.Header.Set(key, value.(string))
|
httpRequest.Header.Set(key, fmt.Sprint(value))
|
||||||
|
}
|
||||||
|
// Set Content-Type header if not already set
|
||||||
|
if httpRequest.Header.Get("Content-Type") == "" {
|
||||||
|
httpRequest.Header.Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
var retries_max int
|
retriesMax := 1
|
||||||
|
|
||||||
if qe.Retries {
|
if qe.Retries {
|
||||||
qe.Logger.Debug("Retries enabled - setting max retries", map[string]interface{}{"retries": qe.retries_number})
|
retriesMax = qe.retries_number
|
||||||
retries_max = qe.retries_number
|
|
||||||
} else {
|
|
||||||
qe.Logger.Debug("Retries disabled - setting max retries", map[string]interface{}{"retries": 1})
|
|
||||||
retries_max = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpResponse *http.Response
|
|
||||||
var queryResult queryResults
|
var queryResult queryResults
|
||||||
|
|
||||||
err = retry.Do(
|
err = retry.Do(
|
||||||
func() error {
|
func() error {
|
||||||
httpResponse, err = qe.client.Do(httpRequest)
|
// Reset buffer before each retry
|
||||||
|
buf.Reset()
|
||||||
|
buf.Write(qe.Query)
|
||||||
|
|
||||||
|
// Set the body of the request
|
||||||
|
httpRequest.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
|
||||||
|
httpRequest.ContentLength = int64(buf.Len())
|
||||||
|
|
||||||
|
httpResponse, err := qe.client.Do(httpRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qe.Logger.Debug("Error while executing http request", map[string]interface{}{"error": err.Error()})
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer io.Copy(io.Discard, httpResponse.Body)
|
defer func() {
|
||||||
defer httpResponse.Body.Close()
|
io.Copy(io.Discard, httpResponse.Body)
|
||||||
|
httpResponse.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 204 {
|
if httpResponse.StatusCode < http.StatusOK || httpResponse.StatusCode >= http.StatusMultipleChoices {
|
||||||
return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL)
|
return fmt.Errorf("HTTP error - status code: %s for %s", httpResponse.Status, httpRequest.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(httpResponse.Body)
|
var reader io.Reader
|
||||||
|
encoding := httpResponse.Header.Get("Content-Encoding")
|
||||||
|
if encoding == "gzip" {
|
||||||
|
gzipReader, err := gzip.NewReader(httpResponse.Body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error creating gzip reader: %w", err)
|
||||||
|
}
|
||||||
|
defer gzipReader.Close()
|
||||||
|
reader = gzipReader
|
||||||
|
} else {
|
||||||
|
reader = httpResponse.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read all response body
|
||||||
|
body, err := io.ReadAll(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qe.Logger.Debug("Error while reading http response;", map[string]interface{}{"error": err.Error()})
|
return fmt.Errorf("error reading HTTP response: %w", err)
|
||||||
return fmt.Errorf("Error while reading http response: %s", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmarshal response
|
||||||
err = json.Unmarshal(body, &queryResult)
|
err = json.Unmarshal(body, &queryResult)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qe.Logger.Debug("Error while unmarshalling http response;", map[string]interface{}{"error": err.Error()})
|
return fmt.Errorf("error unmarshalling HTTP response: %w", err)
|
||||||
return fmt.Errorf("Error while unmarshalling http response: %s", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
retry.OnRetry(func(n uint, err error) {
|
retry.OnRetry(func(n uint, err error) {
|
||||||
qe.Logger.Warning("Retrying query", map[string]interface{}{"error": err.Error(), "attempt": n})
|
qe.Logger.Warning(&libpack_logger.LogMessage{
|
||||||
|
Message: "Retrying query",
|
||||||
|
Pairs: map[string]interface{}{"error": err.Error(), "attempt": n + 1},
|
||||||
|
})
|
||||||
}),
|
}),
|
||||||
retry.Attempts(uint(retries_max)),
|
retry.Attempts(uint(retriesMax)),
|
||||||
retry.DelayType(retry.BackOffDelay),
|
retry.DelayType(retry.BackOffDelay),
|
||||||
retry.Delay(time.Duration(qe.retries_delay)),
|
retry.Delay(time.Duration(qe.retries_delay)),
|
||||||
retry.LastErrorOnly(true),
|
retry.LastErrorOnly(true),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qe.Logger.Debug("Error while executing http request - target server", map[string]interface{}{"error": err.Error()})
|
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
|
Message: "Error executing HTTP request",
|
||||||
|
Pairs: map[string]interface{}{"error": err.Error()},
|
||||||
|
})
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(queryResult.Errors) > 0 {
|
if len(queryResult.Errors) > 0 {
|
||||||
qe.Logger.Debug("Error while executing query;", map[string]interface{}{"error": queryResult.Errors})
|
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
return nil, fmt.Errorf("Error while executing query: %s", queryResult.Errors)
|
Message: "Error executing query",
|
||||||
|
Pairs: map[string]interface{}{"error": queryResult.Errors},
|
||||||
|
})
|
||||||
|
return nil, fmt.Errorf("error executing query: %s", queryResult.Errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
if queryResult.Data == nil {
|
if queryResult.Data == nil {
|
||||||
qe.Logger.Debug("Error while executing query", map[string]interface{}{"error": "no data"})
|
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
return nil, fmt.Errorf("Error while executing query: no data")
|
Message: "Error executing query",
|
||||||
|
Pairs: map[string]interface{}{"error": "no data"},
|
||||||
|
})
|
||||||
|
return nil, errors.New("error executing query: no data")
|
||||||
}
|
}
|
||||||
|
|
||||||
json_data, err := json.Marshal(queryResult.Data)
|
// Marshal queryResult.Data
|
||||||
|
jsonData, err := json.Marshal(queryResult.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qe.Logger.Debug("Error while marshalling query result;", map[string]interface{}{"error": err.Error(), "data": queryResult.Data})
|
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
return nil, fmt.Errorf("Error while marshalling query result: %s. Data: %s", err.Error(), queryResult.Data)
|
Message: "Error marshalling query result",
|
||||||
|
Pairs: map[string]interface{}{"error": err.Error(), "data": queryResult.Data},
|
||||||
|
})
|
||||||
|
return nil, fmt.Errorf("error marshalling query result: %w. Data: %s", err, queryResult.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
if qe.CacheKey != "no-cache" {
|
if qe.CacheKey != "no-cache" {
|
||||||
qe.cache.Set(qe.CacheKey, json_data, qe.CacheTTL)
|
qe.cache.Set(qe.CacheKey, jsonData, qe.CacheTTL)
|
||||||
}
|
}
|
||||||
|
|
||||||
return json_data, nil
|
return jsonData, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,26 +3,21 @@ module github.com/lukaszraczylo/go-simple-graphql
|
|||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/avast/retry-go/v4 v4.5.1
|
github.com/avast/retry-go/v4 v4.6.0
|
||||||
github.com/gookit/goutil v0.6.14
|
github.com/goccy/go-json v0.10.3
|
||||||
github.com/json-iterator/go v1.1.12
|
github.com/gookit/goutil v0.6.16
|
||||||
github.com/rs/zerolog v1.31.0
|
github.com/stretchr/testify v1.9.0
|
||||||
github.com/stretchr/testify v1.8.4
|
golang.org/x/net v0.29.0
|
||||||
golang.org/x/net v0.19.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/gookit/color v1.5.4 // indirect
|
github.com/gookit/color v1.5.4 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
golang.org/x/sync v0.5.0 // indirect
|
golang.org/x/sync v0.8.0 // indirect
|
||||||
golang.org/x/sys v0.15.0 // indirect
|
golang.org/x/sys v0.25.0 // indirect
|
||||||
golang.org/x/term v0.15.0 // indirect
|
golang.org/x/term v0.24.0 // indirect
|
||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/text v0.18.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,55 +1,31 @@
|
|||||||
github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh7o=
|
github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinRJA=
|
||||||
github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc=
|
github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE=
|
||||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||||
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
|
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
|
||||||
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
|
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
|
||||||
github.com/gookit/goutil v0.6.14 h1:96elyOG4BvVoDaiT7vx1vHPrVyEtFfYlPPBODR0/FGQ=
|
github.com/gookit/goutil v0.6.16 h1:9fRMCF4X9abdRD5+2HhBS/GwafjBlTUBjRtA5dgkvuw=
|
||||||
github.com/gookit/goutil v0.6.14/go.mod h1:YyDBddefmjS+mU2PDPgCcjVzTDM5WgExiDv5ZA/b8I8=
|
github.com/gookit/goutil v0.6.16/go.mod h1:op2q8AoPDFSiY2+qkHxcBWQMYxOLQ1GbLXqe7vrwscI=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
|
||||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
|
||||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
|
||||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
|
||||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||||
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
|
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||||
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
|
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||||
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
|
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||||
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
|
||||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
|
||||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||||
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
|
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
|
|
||||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
|
||||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
+15
-5
@@ -1,12 +1,15 @@
|
|||||||
package gql
|
package gql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gookit/goutil/strutil"
|
"github.com/goccy/go-json"
|
||||||
|
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
func searchForKeysInMapStringInterface(msi map[string]interface{}, key string) (value any) {
|
func searchForKeysInMapStringInterface(msi map[string]interface{}, key string) interface{} {
|
||||||
if msi == nil {
|
if msi == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -14,7 +17,8 @@ func searchForKeysInMapStringInterface(msi map[string]interface{}, key string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
func calculateHash(query *Query) string {
|
func calculateHash(query *Query) string {
|
||||||
return strutil.Md5(query.JsonQuery)
|
hash := md5.Sum(query.JsonQuery)
|
||||||
|
return hex.EncodeToString(hash[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseClient) cacheLookup(hash string) []byte {
|
func (b *BaseClient) cacheLookup(hash string) []byte {
|
||||||
@@ -28,7 +32,10 @@ func (b *BaseClient) decodeResponse(response []byte) (any, error) {
|
|||||||
var result map[string]interface{}
|
var result map[string]interface{}
|
||||||
err := json.Unmarshal(response, &result)
|
err := json.Unmarshal(response, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Logger.Error("Can't decode response into mapstring", map[string]interface{}{"error": err.Error()})
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Can't decode response into mapstring",
|
||||||
|
Pairs: map[string]interface{}{"error": err.Error()},
|
||||||
|
})
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
@@ -37,7 +44,10 @@ func (b *BaseClient) decodeResponse(response []byte) (any, error) {
|
|||||||
case "byte":
|
case "byte":
|
||||||
return response, nil
|
return response, nil
|
||||||
default:
|
default:
|
||||||
b.Logger.Error("Can't decode response;", map[string]interface{}{"error": "unknown response type"})
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Can't decode response",
|
||||||
|
Pairs: map[string]interface{}{"error": "unknown response type"},
|
||||||
|
})
|
||||||
return nil, fmt.Errorf("Can't decode response - unknown response type specified")
|
return nil, fmt.Errorf("Can't decode response - unknown response type specified")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -50,12 +50,12 @@ func (suite *Tests) TestBaseClient_decodeResponse() {
|
|||||||
response []byte
|
response []byte
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want any
|
want any
|
||||||
wantErr bool
|
name string
|
||||||
setType string
|
setType string
|
||||||
wantType string
|
wantType string
|
||||||
|
args args
|
||||||
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "TestBaseClient_decodeResponse_mapstring",
|
name: "TestBaseClient_decodeResponse_mapstring",
|
||||||
|
|||||||
@@ -6,25 +6,33 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
||||||
httpTransport := &http.Transport{
|
httpTransport := &http.Transport{
|
||||||
MaxIdleConns: 10,
|
MaxIdleConns: 512,
|
||||||
|
MaxConnsPerHost: 512,
|
||||||
|
MaxIdleConnsPerHost: 512,
|
||||||
IdleConnTimeout: 15 * time.Second,
|
IdleConnTimeout: 15 * time.Second,
|
||||||
ResponseHeaderTimeout: 15 * time.Second,
|
ResponseHeaderTimeout: 15 * time.Second,
|
||||||
DisableKeepAlives: false,
|
DisableKeepAlives: false,
|
||||||
|
DisableCompression: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(b.endpoint, "http://") {
|
if strings.HasPrefix(b.endpoint, "http://") {
|
||||||
http_client = &http.Client{
|
http_client = &http.Client{
|
||||||
|
Timeout: 15 * time.Second,
|
||||||
Transport: httpTransport,
|
Transport: httpTransport,
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
return http.ErrUseLastResponse
|
return http.ErrUseLastResponse
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
b.Logger.Debug("Using HTTP/1.1 over http")
|
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
|
Message: "Using HTTP/1.1 over http",
|
||||||
|
Pairs: nil,
|
||||||
|
})
|
||||||
} else if strings.HasPrefix(b.endpoint, "https://") {
|
} else if strings.HasPrefix(b.endpoint, "https://") {
|
||||||
tlsClientConfig := &tls.Config{}
|
tlsClientConfig := &tls.Config{}
|
||||||
if strings.HasPrefix(b.endpoint, "https://") {
|
if strings.HasPrefix(b.endpoint, "https://") {
|
||||||
@@ -42,9 +50,14 @@ func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
|||||||
return http.ErrUseLastResponse
|
return http.ErrUseLastResponse
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
b.Logger.Debug("Using HTTP/2 over https")
|
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
|
Message: "Using HTTP/2 over https",
|
||||||
|
Pairs: nil,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
b.Logger.Critical("Invalid endpoint - neither http or https")
|
b.Logger.Critical(&libpack_logger.LogMessage{
|
||||||
|
Message: "Invalid endpoint - neither http or https",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return http_client
|
return http_client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,204 @@
|
|||||||
|
package libpack_logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
_ = iota
|
||||||
|
LEVEL_DEBUG
|
||||||
|
LEVEL_INFO
|
||||||
|
LEVEL_WARN
|
||||||
|
LEVEL_ERROR
|
||||||
|
LEVEL_FATAL
|
||||||
|
)
|
||||||
|
|
||||||
|
var LevelNames = [...]string{
|
||||||
|
"none",
|
||||||
|
"debug",
|
||||||
|
"info",
|
||||||
|
"warn",
|
||||||
|
"error",
|
||||||
|
"fatal",
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultFormat = time.RFC3339
|
||||||
|
defaultMinLevel = LEVEL_INFO
|
||||||
|
defaultShowCaller = false
|
||||||
|
)
|
||||||
|
|
||||||
|
var defaultOutput = os.Stdout
|
||||||
|
|
||||||
|
type Logger struct {
|
||||||
|
output io.Writer
|
||||||
|
format string
|
||||||
|
minLogLevel int
|
||||||
|
showCaller bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogMessage struct {
|
||||||
|
output io.Writer
|
||||||
|
Pairs map[string]any
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *LogMessage) String() string {
|
||||||
|
return m.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
var fieldNames = map[string]string{
|
||||||
|
"timestamp": "timestamp",
|
||||||
|
"level": "level",
|
||||||
|
"message": "message",
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Logger {
|
||||||
|
return &Logger{
|
||||||
|
format: defaultFormat,
|
||||||
|
minLogLevel: defaultMinLevel,
|
||||||
|
output: defaultOutput,
|
||||||
|
showCaller: defaultShowCaller,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) SetOutput(output io.Writer) *Logger {
|
||||||
|
l.output = output
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
var bufferPool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return new(bytes.Buffer)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultPairs = make(map[string]any)
|
||||||
|
|
||||||
|
func GetLogLevel(level string) int {
|
||||||
|
for i, name := range LevelNames {
|
||||||
|
if name == strings.ToLower(level) {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultMinLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) log(level int, m *LogMessage) {
|
||||||
|
if m.Pairs == nil {
|
||||||
|
m.Pairs = defaultPairs
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Pairs[fieldNames["timestamp"]] = time.Now().Format(l.format)
|
||||||
|
m.Pairs[fieldNames["level"]] = LevelNames[level]
|
||||||
|
m.Pairs[fieldNames["message"]] = m.Message
|
||||||
|
|
||||||
|
if l.showCaller {
|
||||||
|
m.Pairs["caller"] = getCaller()
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer := bufferPool.Get().(*bytes.Buffer)
|
||||||
|
defer bufferPool.Put(buffer)
|
||||||
|
buffer.Reset()
|
||||||
|
|
||||||
|
var encoder = json.NewEncoder(buffer)
|
||||||
|
err := encoder.Encode(m.Pairs)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error marshalling log message:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not running in test - use stderr and stdout, otherwise - use logger's output setting
|
||||||
|
if flag.Lookup("test.v") != nil {
|
||||||
|
m.output = os.Stdout
|
||||||
|
if level >= LEVEL_ERROR {
|
||||||
|
m.output = os.Stderr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use logger's output setting instead of os.Stdout or os.Stderr
|
||||||
|
l.output.Write(buffer.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Debug(m *LogMessage) {
|
||||||
|
if l.shouldLog(LEVEL_DEBUG) {
|
||||||
|
l.log(LEVEL_DEBUG, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Info(m *LogMessage) {
|
||||||
|
if l.shouldLog(LEVEL_INFO) {
|
||||||
|
l.log(LEVEL_INFO, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Warn(m *LogMessage) {
|
||||||
|
if l.shouldLog(LEVEL_WARN) {
|
||||||
|
l.log(LEVEL_WARN, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Warning(m *LogMessage) {
|
||||||
|
l.Warn(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Error(m *LogMessage) {
|
||||||
|
if l.shouldLog(LEVEL_ERROR) {
|
||||||
|
l.log(LEVEL_ERROR, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Fatal(m *LogMessage) {
|
||||||
|
if l.shouldLog(LEVEL_FATAL) {
|
||||||
|
l.log(LEVEL_FATAL, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Critical(m *LogMessage) {
|
||||||
|
l.Fatal(m)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) shouldLog(level int) bool {
|
||||||
|
return level >= l.minLogLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) SetFormat(format string) *Logger {
|
||||||
|
l.format = format
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) SetMinLogLevel(level int) *Logger {
|
||||||
|
l.minLogLevel = level
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) SetFieldName(field, name string) *Logger {
|
||||||
|
fieldNames[field] = name
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) SetShowCaller(show bool) *Logger {
|
||||||
|
l.showCaller = show
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCaller() string {
|
||||||
|
_, file, line, ok := runtime.Caller(3)
|
||||||
|
if !ok {
|
||||||
|
return "unknown:0"
|
||||||
|
}
|
||||||
|
file = filepath.Base(file)
|
||||||
|
return fmt.Sprintf("%s:%d", file, line)
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
package libpack_logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Benchmark_NewLogger(b *testing.B) {
|
||||||
|
type triggers struct {
|
||||||
|
ModFormat struct {
|
||||||
|
Format string
|
||||||
|
}
|
||||||
|
ModLevel struct {
|
||||||
|
Level int
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
triggers triggers
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "BenchmarkNew",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "BenchmarkNewChangeTimeFormat",
|
||||||
|
triggers: triggers{
|
||||||
|
ModFormat: struct{ Format string }{
|
||||||
|
Format: time.RFC3339Nano,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "BenchmarkNewChangeLogLevel",
|
||||||
|
triggers: triggers{
|
||||||
|
ModLevel: struct{ Level int }{
|
||||||
|
Level: LEVEL_DEBUG,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "BenchmarkNewChangeTimeFormatAndLogLevel",
|
||||||
|
triggers: triggers{
|
||||||
|
ModFormat: struct{ Format string }{
|
||||||
|
Format: time.RFC3339Nano,
|
||||||
|
},
|
||||||
|
ModLevel: struct{ Level int }{
|
||||||
|
Level: LEVEL_DEBUG,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
b.Run(tt.name, func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
got := New()
|
||||||
|
|
||||||
|
if tt.triggers.ModFormat.Format != "" {
|
||||||
|
got = got.SetFormat(tt.triggers.ModFormat.Format)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt.triggers.ModLevel.Level != 0 {
|
||||||
|
got = got.SetMinLogLevel(tt.triggers.ModLevel.Level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_Log_Debug(b *testing.B) {
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
logger := New().SetMinLogLevel(LEVEL_DEBUG).SetOutput(output)
|
||||||
|
msg := &LogMessage{
|
||||||
|
Message: "debug message",
|
||||||
|
Pairs: make(map[string]any),
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
logger.Debug(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_Log_Info(b *testing.B) {
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
logger := New().SetMinLogLevel(LEVEL_INFO).SetOutput(output)
|
||||||
|
msg := &LogMessage{
|
||||||
|
Message: "info message",
|
||||||
|
Pairs: make(map[string]any),
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
logger.Info(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_Log_Warn(b *testing.B) {
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
logger := New().SetMinLogLevel(LEVEL_WARN).SetOutput(output)
|
||||||
|
msg := &LogMessage{
|
||||||
|
Message: "warn message",
|
||||||
|
Pairs: make(map[string]any),
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
logger.Warn(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_Log_Error(b *testing.B) {
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
logger := New().SetMinLogLevel(LEVEL_ERROR).SetOutput(output)
|
||||||
|
msg := &LogMessage{
|
||||||
|
Message: "error message",
|
||||||
|
Pairs: map[string]any{"key": "value"},
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
logger.Error(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_Log_Fatal(b *testing.B) {
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
logger := New().SetMinLogLevel(LEVEL_FATAL).SetOutput(output)
|
||||||
|
msg := &LogMessage{
|
||||||
|
Message: "fatal message",
|
||||||
|
Pairs: make(map[string]any),
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
logger.Fatal(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package libpack_logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
assertions "github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LoggerTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
assert *assertions.Assertions
|
||||||
|
)
|
||||||
|
|
||||||
|
func (suite *LoggerTestSuite) BeforeTest(suiteName, testName string) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *LoggerTestSuite) SetupTest() {
|
||||||
|
assert = assertions.New(suite.T())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TearDownTest is run after each test to clean up
|
||||||
|
func (suite *LoggerTestSuite) TearDownTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(LoggerTestSuite))
|
||||||
|
}
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
package libpack_logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
|
)
|
||||||
|
|
||||||
|
func captureStderr(f func()) string {
|
||||||
|
originalStderr := os.Stderr
|
||||||
|
r, w, _ := os.Pipe()
|
||||||
|
os.Stderr = w
|
||||||
|
f()
|
||||||
|
w.Close()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.ReadFrom(r)
|
||||||
|
os.Stderr = originalStderr
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func captureStdOut(f func()) string {
|
||||||
|
originalStdout := os.Stdout
|
||||||
|
r, w, _ := os.Pipe()
|
||||||
|
os.Stdout = w
|
||||||
|
f()
|
||||||
|
w.Close()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.ReadFrom(r)
|
||||||
|
os.Stdout = originalStdout
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *LoggerTestSuite) Test_LogMessageString() {
|
||||||
|
msg := &LogMessage{
|
||||||
|
Message: "test message",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("test message", msg.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func callLoggerMethod(logger *Logger, methodName string, message *LogMessage) {
|
||||||
|
// Get the method by name using reflection
|
||||||
|
method := reflect.ValueOf(logger).MethodByName(methodName)
|
||||||
|
if method.IsValid() {
|
||||||
|
// Call the method with the message as an argument
|
||||||
|
method.Call([]reflect.Value{reflect.ValueOf(message)})
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Method %s does not exist on Logger\n", methodName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *LoggerTestSuite) Test_LogsLevelsPrint() {
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
logger := New().SetOutput(output)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
pairs map[string]any
|
||||||
|
name string
|
||||||
|
method string
|
||||||
|
message string
|
||||||
|
loggerMinLevel int
|
||||||
|
messageLogLevel int
|
||||||
|
wantOutput bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Log: Debug, Level: Debug - no pairs",
|
||||||
|
method: "Debug",
|
||||||
|
loggerMinLevel: LEVEL_DEBUG,
|
||||||
|
messageLogLevel: LEVEL_DEBUG,
|
||||||
|
message: "debug message",
|
||||||
|
wantOutput: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Log: Info, Level: Info - one pair",
|
||||||
|
method: "Info",
|
||||||
|
loggerMinLevel: LEVEL_INFO,
|
||||||
|
messageLogLevel: LEVEL_INFO,
|
||||||
|
message: "info message",
|
||||||
|
pairs: map[string]any{
|
||||||
|
"key": "value",
|
||||||
|
},
|
||||||
|
wantOutput: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Log: Info, Level: Warn - with pairs",
|
||||||
|
method: "Info",
|
||||||
|
loggerMinLevel: LEVEL_WARN,
|
||||||
|
messageLogLevel: LEVEL_INFO,
|
||||||
|
message: "warn message",
|
||||||
|
pairs: map[string]any{
|
||||||
|
"key1": "value1",
|
||||||
|
"key2": "value2",
|
||||||
|
},
|
||||||
|
wantOutput: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Log: Warn, Level: Info - with 500 pairs",
|
||||||
|
method: "Warn",
|
||||||
|
loggerMinLevel: LEVEL_INFO,
|
||||||
|
messageLogLevel: LEVEL_WARN,
|
||||||
|
message: "warn message with 500 pairs",
|
||||||
|
pairs: func() map[string]any {
|
||||||
|
pairs := make(map[string]any)
|
||||||
|
for i := 0; i < 500; i++ {
|
||||||
|
pairs[fmt.Sprintf("key%d", i)] = fmt.Sprintf("value%d", i)
|
||||||
|
}
|
||||||
|
return pairs
|
||||||
|
}(),
|
||||||
|
wantOutput: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
suite.T().Run(tt.name, func(t *testing.T) {
|
||||||
|
msg := &LogMessage{
|
||||||
|
Message: tt.message,
|
||||||
|
Pairs: tt.pairs,
|
||||||
|
}
|
||||||
|
output.Reset()
|
||||||
|
|
||||||
|
// Set logger's minimum log level
|
||||||
|
logger.SetMinLogLevel(tt.loggerMinLevel)
|
||||||
|
fmt.Println("Logger min log level:", LevelNames[logger.minLogLevel])
|
||||||
|
|
||||||
|
// Call the logging method
|
||||||
|
callLoggerMethod(logger, tt.method, msg)
|
||||||
|
|
||||||
|
logOutput := output.String()
|
||||||
|
fmt.Println("Output:", logOutput)
|
||||||
|
|
||||||
|
if tt.wantOutput {
|
||||||
|
var loggedMessage map[string]any
|
||||||
|
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error unmarshalling log message: %v\nLog output: %s", err, logOutput)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !containsLogMessage(logOutput, tt.message) {
|
||||||
|
t.Errorf("Expected log message %q, but got %q", tt.message, logOutput)
|
||||||
|
}
|
||||||
|
assert.Equal(LevelNames[tt.messageLogLevel], loggedMessage["level"])
|
||||||
|
if tt.pairs != nil {
|
||||||
|
for k, v := range tt.pairs {
|
||||||
|
assert.Equal(v, loggedMessage[k])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assert.Equal("", logOutput)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsLogMessage(logOutput, expectedMessage string) bool {
|
||||||
|
return bytes.Contains([]byte(logOutput), []byte(expectedMessage))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *LoggerTestSuite) Test_SetFormat() {
|
||||||
|
logger := New().SetFormat(time.RFC3339Nano)
|
||||||
|
|
||||||
|
assert.Equal(time.RFC3339Nano, logger.format)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *LoggerTestSuite) Test_SetMinLogLevel() {
|
||||||
|
logger := New().SetMinLogLevel(LEVEL_DEBUG)
|
||||||
|
|
||||||
|
assert.Equal(LEVEL_DEBUG, logger.minLogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *LoggerTestSuite) Test_ShouldLog() {
|
||||||
|
logger := New().SetMinLogLevel(LEVEL_WARN)
|
||||||
|
|
||||||
|
assert.True(logger.shouldLog(LEVEL_WARN))
|
||||||
|
assert.True(logger.shouldLog(LEVEL_ERROR))
|
||||||
|
assert.False(logger.shouldLog(LEVEL_INFO))
|
||||||
|
assert.False(logger.shouldLog(LEVEL_DEBUG))
|
||||||
|
}
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
package libpack_logging
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gookit/goutil/envutil"
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LogConfig struct {
|
|
||||||
baseLogger zerolog.Logger
|
|
||||||
errorLogger zerolog.Logger
|
|
||||||
nopLogger zerolog.Logger
|
|
||||||
mu sync.Mutex
|
|
||||||
minLevel zerolog.Level
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
zerolog.TimeFieldFormat = time.RFC3339
|
|
||||||
zerolog.MessageFieldName = "msg"
|
|
||||||
zerolog.TimestampFieldName = "ts"
|
|
||||||
zerolog.LevelFieldName = "level"
|
|
||||||
zerolog.LevelFatalValue = "critical"
|
|
||||||
}
|
|
||||||
|
|
||||||
func getMinLogLevel() zerolog.Level {
|
|
||||||
levelStr := strings.ToLower(envutil.Getenv("LOG_LEVEL", "info"))
|
|
||||||
return matchLogLevel(levelStr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add function set the minimum log level instead of using environment variable
|
|
||||||
func (lc *LogConfig) SetLogLevel(level string) {
|
|
||||||
lc.mu.Lock()
|
|
||||||
defer lc.mu.Unlock()
|
|
||||||
lc.minLevel = matchLogLevel(level)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match the log level provided as string with the log level provided as zerolog.Level
|
|
||||||
func matchLogLevel(level string) zerolog.Level {
|
|
||||||
switch level {
|
|
||||||
case "debug":
|
|
||||||
return zerolog.DebugLevel
|
|
||||||
case "warn":
|
|
||||||
return zerolog.WarnLevel
|
|
||||||
case "error":
|
|
||||||
return zerolog.ErrorLevel
|
|
||||||
case "critical":
|
|
||||||
return zerolog.FatalLevel
|
|
||||||
default:
|
|
||||||
return zerolog.InfoLevel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLogger() *LogConfig {
|
|
||||||
zl := zerolog.New(os.Stdout).With().Timestamp().Logger()
|
|
||||||
// Determine the minimum log level from environment variable
|
|
||||||
return &LogConfig{
|
|
||||||
baseLogger: zl,
|
|
||||||
errorLogger: zl.Output(os.Stderr),
|
|
||||||
nopLogger: zerolog.Nop(),
|
|
||||||
minLevel: getMinLogLevel(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *LogConfig) getLogger(level zerolog.Level) zerolog.Logger {
|
|
||||||
if level >= zerolog.ErrorLevel {
|
|
||||||
return lc.errorLogger
|
|
||||||
}
|
|
||||||
if level < lc.minLevel {
|
|
||||||
return lc.nopLogger
|
|
||||||
}
|
|
||||||
return lc.baseLogger
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *LogConfig) Log(level zerolog.Level, message string, fields []map[string]interface{}) {
|
|
||||||
if level < lc.minLevel {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logger := lc.getLogger(level)
|
|
||||||
event := logger.WithLevel(level).CallerSkipFrame(3)
|
|
||||||
if len(fields) == 0 {
|
|
||||||
event.Msg(message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
field := fields[0]
|
|
||||||
for k, val := range field {
|
|
||||||
switch v := val.(type) {
|
|
||||||
case string:
|
|
||||||
event.Str(k, v)
|
|
||||||
case int:
|
|
||||||
event.Int(k, v)
|
|
||||||
case float64:
|
|
||||||
event.Float64(k, v)
|
|
||||||
default:
|
|
||||||
event.Interface(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
event.Msg(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *LogConfig) Info(message string, fields ...map[string]interface{}) {
|
|
||||||
lc.Log(zerolog.InfoLevel, message, fields)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *LogConfig) Debug(message string, fields ...map[string]interface{}) {
|
|
||||||
lc.Log(zerolog.DebugLevel, message, fields)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *LogConfig) Warn(message string, fields ...map[string]interface{}) {
|
|
||||||
lc.Log(zerolog.WarnLevel, message, fields)
|
|
||||||
}
|
|
||||||
|
|
||||||
// alias Warning to Warn
|
|
||||||
func (lc *LogConfig) Warning(message string, fields ...map[string]interface{}) {
|
|
||||||
lc.Warn(message, fields...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *LogConfig) Error(message string, fields ...map[string]interface{}) {
|
|
||||||
lc.Log(zerolog.ErrorLevel, message, fields)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *LogConfig) Critical(message string, fields ...map[string]interface{}) {
|
|
||||||
lc.Log(zerolog.FatalLevel, message, fields)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// alias Fatal to Critical
|
|
||||||
func (lc *LogConfig) Fatal(message string, fields ...map[string]interface{}) {
|
|
||||||
lc.Critical(message, fields...)
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package libpack_logging
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func BenchmarkNewLogger(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
NewLogger()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkInfoLog(b *testing.B) {
|
|
||||||
oldEnv := os.Getenv("LOG_LEVEL")
|
|
||||||
os.Setenv("LOG_LEVEL", "info")
|
|
||||||
oldStdout := os.Stdout
|
|
||||||
oldStderr := os.Stderr
|
|
||||||
os.Stdout, _ = os.Open(os.DevNull)
|
|
||||||
os.Stderr, _ = os.Open(os.DevNull)
|
|
||||||
defer func() {
|
|
||||||
os.Stdout = oldStdout
|
|
||||||
os.Stderr = oldStderr
|
|
||||||
os.Setenv("LOG_LEVEL", oldEnv)
|
|
||||||
}()
|
|
||||||
|
|
||||||
testsLogger := NewLogger()
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
testsLogger.Info("test", map[string]interface{}{"test": "test"})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package gql
|
package gql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gookit/goutil/envutil"
|
"github.com/gookit/goutil/envutil"
|
||||||
@@ -12,7 +13,7 @@ func NewConnection() (b *BaseClient) {
|
|||||||
b = &BaseClient{
|
b = &BaseClient{
|
||||||
endpoint: envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql"),
|
endpoint: envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql"),
|
||||||
responseType: envutil.Getenv("GRAPHQL_OUTPUT", "string"),
|
responseType: envutil.Getenv("GRAPHQL_OUTPUT", "string"),
|
||||||
Logger: logging.NewLogger(),
|
Logger: logging.New(),
|
||||||
cache: cache.New(time.Duration(envutil.GetInt("GRAPHQL_CACHE_TTL", 5)) * time.Second),
|
cache: cache.New(time.Duration(envutil.GetInt("GRAPHQL_CACHE_TTL", 5)) * time.Second),
|
||||||
cache_global: envutil.GetBool("GRAPHQL_CACHE_ENABLED", false),
|
cache_global: envutil.GetBool("GRAPHQL_CACHE_ENABLED", false),
|
||||||
retries_enable: envutil.GetBool("GRAPHQL_RETRIES_ENABLE", false),
|
retries_enable: envutil.GetBool("GRAPHQL_RETRIES_ENABLE", false),
|
||||||
@@ -20,7 +21,11 @@ func NewConnection() (b *BaseClient) {
|
|||||||
retries_number: envutil.GetInt("GRAPHQL_RETRIES_NUMBER", 3),
|
retries_number: envutil.GetInt("GRAPHQL_RETRIES_NUMBER", 3),
|
||||||
}
|
}
|
||||||
b.client = b.createHttpClient()
|
b.client = b.createHttpClient()
|
||||||
b.Logger.Debug("Created new GraphQL client connection", map[string]interface{}{"values": b})
|
b.Logger.Debug(&logging.LogMessage{
|
||||||
|
Message: "Created new GraphQL client connection",
|
||||||
|
Pairs: map[string]interface{}{
|
||||||
|
"values": b,
|
||||||
|
}})
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,3 +39,7 @@ func (b *BaseClient) SetOutput(responseType string) {
|
|||||||
// TODO: implement
|
// TODO: implement
|
||||||
b.responseType = responseType
|
b.responseType = responseType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BaseClient) SetHTTPClient(client *http.Client) {
|
||||||
|
b.client = client
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
// mock_server.go
|
||||||
|
package gql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/http2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StartMockServer() *httptest.Server {
|
||||||
|
// Create your handler
|
||||||
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Your existing handler code
|
||||||
|
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
Variables map[string]interface{} `json:"variables"`
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(bodyBytes, &req)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple validation to check for unbalanced braces as a syntax error
|
||||||
|
if !strings.HasPrefix(strings.TrimSpace(req.Query), "query") || !strings.HasSuffix(strings.TrimSpace(req.Query), "}") {
|
||||||
|
resp := []byte(`{"errors":[{"message":"Syntax Error: Invalid query"}]}`)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write(resp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp []byte
|
||||||
|
if strings.Contains(req.Query, "viewer") {
|
||||||
|
resp = []byte(`{"data":{"viewer":{"login":"mockuser"}}}`)
|
||||||
|
} else if strings.Contains(req.Query, "dragons") {
|
||||||
|
resp = []byte(`{"data":{"dragons":[{"name":"Mock Dragon 1"},{"name":"Mock Dragon 2"}]}}`)
|
||||||
|
} else {
|
||||||
|
resp = []byte(`{"errors":[{"message":"Unknown query"}]}`)
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Create an unstarted server
|
||||||
|
server := httptest.NewUnstartedServer(handler)
|
||||||
|
|
||||||
|
// Configure the server to support HTTP/2
|
||||||
|
http2.ConfigureServer(server.Config, &http2.Server{})
|
||||||
|
server.TLS = server.Config.TLSConfig
|
||||||
|
|
||||||
|
// Start the server with TLS
|
||||||
|
server.StartTLS()
|
||||||
|
return server
|
||||||
|
}
|
||||||
@@ -1,72 +1,105 @@
|
|||||||
package gql
|
package gql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
"github.com/gookit/goutil"
|
"github.com/gookit/goutil"
|
||||||
"github.com/gookit/goutil/strutil"
|
"github.com/gookit/goutil/strutil"
|
||||||
jsoniter "github.com/json-iterator/go"
|
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
||||||
|
|
||||||
func (b *BaseClient) convertToJSON(v any) []byte {
|
func (b *BaseClient) convertToJSON(v any) []byte {
|
||||||
|
// Reuse buffer to reduce allocations
|
||||||
|
buf := bufferPool.Get().(*bytes.Buffer)
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
// Use json.Marshal to avoid adding extra newline
|
||||||
jsonData, err := json.Marshal(v)
|
jsonData, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Logger.Error("Can't convert to json;", map[string]interface{}{"error": err.Error()})
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Can't convert to JSON",
|
||||||
|
Pairs: map[string]interface{}{"error": err.Error()},
|
||||||
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return jsonData
|
|
||||||
|
// Copy the bytes to the buffer
|
||||||
|
buf.Write(jsonData)
|
||||||
|
|
||||||
|
// Copy the bytes to a new slice before returning
|
||||||
|
result := make([]byte, buf.Len())
|
||||||
|
copy(result, buf.Bytes())
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseClient) compileQuery(query_partials ...any) *Query {
|
func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
|
||||||
q := new(Query)
|
var query string
|
||||||
for _, partial := range query_partials {
|
var variables map[string]interface{}
|
||||||
|
for _, partial := range queryPartials {
|
||||||
switch val := partial.(type) {
|
switch val := partial.(type) {
|
||||||
case string:
|
case string:
|
||||||
q.Query = val
|
query = val
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
q.Variables = val
|
variables = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if q.Query == "" {
|
|
||||||
b.Logger.Error("Can't compile query;", map[string]interface{}{"error": "query is empty"})
|
if query == "" {
|
||||||
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Can't compile query",
|
||||||
|
Pairs: map[string]interface{}{"error": "query is empty"},
|
||||||
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
q.JsonQuery = b.convertToJSON(q)
|
|
||||||
return q
|
jsonQuery := b.convertToJSON(&Query{Query: query, Variables: variables})
|
||||||
|
return &Query{
|
||||||
|
Query: query,
|
||||||
|
Variables: variables,
|
||||||
|
JsonQuery: jsonQuery,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (returned_value any, err error) {
|
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) {
|
||||||
compiledQuery := b.compileQuery(query, variables)
|
compiledQuery := b.compileQuery(query, variables)
|
||||||
if compiledQuery.JsonQuery == nil {
|
if compiledQuery.JsonQuery == nil {
|
||||||
b.Logger.Error("Can't compile query;", map[string]interface{}{"error": "query is empty"})
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
return nil, fmt.Errorf("Can't compile query")
|
Message: "Can't compile query",
|
||||||
|
Pairs: map[string]interface{}{"error": "query is empty"},
|
||||||
|
})
|
||||||
|
return nil, fmt.Errorf("can't compile query")
|
||||||
}
|
}
|
||||||
|
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
|
Message: "Compiled query",
|
||||||
|
Pairs: map[string]interface{}{"query": compiledQuery},
|
||||||
|
})
|
||||||
|
|
||||||
b.Logger.Debug("Compiled query;", map[string]interface{}{"query": compiledQuery})
|
enableCache, enableRetries, recompileRequired := compiledQuery.parseHeadersAndVariables(headers)
|
||||||
enable_cache, enable_retries, recompile_required := compiledQuery.parseHeadersAndVariables(headers)
|
if recompileRequired {
|
||||||
|
|
||||||
if recompile_required {
|
|
||||||
compiledQuery = b.compileQuery(query, variables)
|
compiledQuery = b.compileQuery(query, variables)
|
||||||
}
|
}
|
||||||
|
|
||||||
var queryHash string
|
var queryHash string
|
||||||
|
if (enableCache || b.cache_global) && strutil.HasPrefix(compiledQuery.Query, "query") {
|
||||||
if (enable_cache || b.cache_global) && strutil.HasPrefix(compiledQuery.Query, "query") {
|
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
b.Logger.Debug("Cache enabled")
|
Message: "Cache enabled",
|
||||||
|
Pairs: nil,
|
||||||
|
})
|
||||||
queryHash = calculateHash(compiledQuery)
|
queryHash = calculateHash(compiledQuery)
|
||||||
cached_value := b.cacheLookup(queryHash)
|
if cachedValue := b.cacheLookup(queryHash); cachedValue != nil {
|
||||||
if cached_value != nil {
|
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
b.Logger.Debug("Cache hit", map[string]interface{}{"query": compiledQuery})
|
Message: "Cache hit",
|
||||||
return cached_value, nil
|
Pairs: map[string]interface{}{"query": compiledQuery},
|
||||||
} else {
|
})
|
||||||
b.Logger.Debug("Cache miss", map[string]interface{}{"query": compiledQuery})
|
return b.decodeResponse(cachedValue)
|
||||||
}
|
}
|
||||||
}
|
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
|
Message: "Cache miss",
|
||||||
if enable_retries || b.retries_enable {
|
Pairs: map[string]interface{}{"query": compiledQuery},
|
||||||
b.Logger.Debug("Retries enabled")
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
q := &QueryExecutor{
|
q := &QueryExecutor{
|
||||||
@@ -76,33 +109,40 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
|
|||||||
CacheKey: func() string {
|
CacheKey: func() string {
|
||||||
if queryHash != "" {
|
if queryHash != "" {
|
||||||
return queryHash
|
return queryHash
|
||||||
} else {
|
|
||||||
return "no-cache"
|
|
||||||
}
|
}
|
||||||
|
return "no-cache"
|
||||||
}(),
|
}(),
|
||||||
Retries: enable_retries || b.retries_enable,
|
Retries: enableRetries || b.retries_enable,
|
||||||
}
|
}
|
||||||
defer func() { q = nil }()
|
|
||||||
rv, err := q.executeQuery()
|
rv, err := q.executeQuery()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Logger.Error("Error while executing query;", map[string]interface{}{"error": err.Error()})
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Error executing query",
|
||||||
|
Pairs: map[string]interface{}{"error": err.Error()},
|
||||||
|
})
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
returned_value, err = q.decodeResponse(rv)
|
|
||||||
return returned_value, err
|
return b.decodeResponse(rv)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) parseHeadersAndVariables(headers map[string]interface{}) (enable_cache bool, enable_retries bool, recompile_required bool) {
|
func (q *Query) parseHeadersAndVariables(headers map[string]interface{}) (enableCache, enableRetries, recompileRequired bool) {
|
||||||
if headers != nil {
|
enableCache, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlcache"))
|
||||||
enable_cache, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlcache"))
|
enableRetries, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlretries"))
|
||||||
enable_retries, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlretries"))
|
|
||||||
}
|
|
||||||
if q.Variables != nil {
|
if q.Variables != nil {
|
||||||
enable_cache, _ = goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlcache"))
|
varEnableCache, _ := goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlcache"))
|
||||||
enable_retries, _ = goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlretries"))
|
varEnableRetries, _ := goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlretries"))
|
||||||
delete(q.Variables, "gqlcache")
|
enableCache = enableCache || varEnableCache
|
||||||
delete(q.Variables, "gqlretries")
|
enableRetries = enableRetries || varEnableRetries
|
||||||
recompile_required = true
|
|
||||||
|
if varEnableCache || varEnableRetries {
|
||||||
|
delete(q.Variables, "gqlcache")
|
||||||
|
delete(q.Variables, "gqlretries")
|
||||||
|
recompileRequired = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
return enableCache, enableRetries, recompileRequired
|
||||||
}
|
}
|
||||||
|
|||||||
+79
-3
@@ -1,6 +1,8 @@
|
|||||||
package gql
|
package gql
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func Benchmark_compileQuery(bn *testing.B) {
|
func Benchmark_compileQuery(bn *testing.B) {
|
||||||
b := NewConnection()
|
b := NewConnection()
|
||||||
@@ -12,12 +14,86 @@ func Benchmark_compileQuery(bn *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_convertToJson(bn *testing.B) {
|
func Benchmark_compileQueryWithVariables(bn *testing.B) {
|
||||||
b := NewConnection()
|
b := NewConnection()
|
||||||
|
variables := map[string]interface{}{
|
||||||
|
"var1": "value1",
|
||||||
|
}
|
||||||
bn.ResetTimer()
|
bn.ResetTimer()
|
||||||
for i := 0; i < bn.N; i++ {
|
for i := 0; i < bn.N; i++ {
|
||||||
b.convertToJSON(
|
b.compileQuery(
|
||||||
`query { viewer { login } }`,
|
`query { viewer { login } }`,
|
||||||
|
variables,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Benchmark_convertToJson(bn *testing.B) {
|
||||||
|
b := NewConnection()
|
||||||
|
query := `query { viewer { login } }`
|
||||||
|
bn.ResetTimer()
|
||||||
|
for i := 0; i < bn.N; i++ {
|
||||||
|
b.convertToJSON(query)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func Benchmark_Query(bn *testing.B) {
|
||||||
|
mockServer := StartMockServer()
|
||||||
|
bn.Cleanup(func() {
|
||||||
|
mockServer.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
b := NewConnection()
|
||||||
|
b.SetEndpoint(mockServer.URL)
|
||||||
|
query := "query { viewer { login } }"
|
||||||
|
variables := map[string]interface{}{
|
||||||
|
"var1": "value1",
|
||||||
|
}
|
||||||
|
headers := map[string]interface{}{
|
||||||
|
"Authorization": "Bearer token",
|
||||||
|
"gqlcache": "true",
|
||||||
|
}
|
||||||
|
bn.ResetTimer()
|
||||||
|
for i := 0; i < bn.N; i++ {
|
||||||
|
_, _ = b.Query(query, variables, headers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_QueryNoCacheNoRetry(bn *testing.B) {
|
||||||
|
mockServer := StartMockServer()
|
||||||
|
bn.Cleanup(func() {
|
||||||
|
mockServer.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
b := NewConnection()
|
||||||
|
b.SetEndpoint(mockServer.URL)
|
||||||
|
query := `query { viewer { login } }`
|
||||||
|
variables := map[string]interface{}{
|
||||||
|
"var1": "value1",
|
||||||
|
}
|
||||||
|
headers := map[string]interface{}{
|
||||||
|
"Authorization": "Bearer token",
|
||||||
|
}
|
||||||
|
bn.ResetTimer()
|
||||||
|
for i := 0; i < bn.N; i++ {
|
||||||
|
_, _ = b.Query(query, variables, headers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_QueryWithEmptyVariables(bn *testing.B) {
|
||||||
|
mockServer := StartMockServer()
|
||||||
|
bn.Cleanup(func() {
|
||||||
|
mockServer.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
b := NewConnection()
|
||||||
|
b.SetEndpoint(mockServer.URL)
|
||||||
|
query := `query { viewer { login } }`
|
||||||
|
headers := map[string]interface{}{
|
||||||
|
"Authorization": "Bearer token",
|
||||||
|
"gqlcache": "true",
|
||||||
|
}
|
||||||
|
bn.ResetTimer()
|
||||||
|
for i := 0; i < bn.N; i++ {
|
||||||
|
_, _ = b.Query(query, nil, headers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+20
-155
@@ -115,182 +115,48 @@ func (suite *Tests) TestBaseClient_Query() {
|
|||||||
args args
|
args args
|
||||||
wantReturned_value any
|
wantReturned_value any
|
||||||
name string
|
name string
|
||||||
graphQLServer string
|
|
||||||
wantType string
|
wantType string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "TestBaseClient_Query_spacex_working",
|
name: "TestBaseClient_Query_viewer",
|
||||||
graphQLServer: "https://spacex-production.up.railway.app/",
|
|
||||||
args: args{
|
args: args{
|
||||||
query: `query Dragons {
|
query: "query { viewer { login } }",
|
||||||
dragons {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
variables: nil,
|
variables: nil,
|
||||||
headers: map[string]interface{}{
|
headers: map[string]interface{}{
|
||||||
"x-apollo-operation-name": "Dragons/github.com/lukaszraczylo/go-simple-graphql",
|
"x-apollo-operation-name": "ViewerQuery",
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantReturned_value: string(`{"dragons":[{"name":"Dragon 1"},{"name":"Dragon 2"}]}`),
|
wantReturned_value: `{"viewer":{"login":"mockuser"}}`, // Adjusted expected value
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "TestBaseClient_Query_spacex_invalid_query",
|
name: "TestBaseClient_Query_dragons",
|
||||||
graphQLServer: "https://spacex-production.up.railway.app/",
|
|
||||||
args: args{
|
args: args{
|
||||||
query: `query Dragons {
|
query: `query Dragons {
|
||||||
dragons {
|
dragons {
|
||||||
name
|
name
|
||||||
}
|
}
|
||||||
`,
|
}`,
|
||||||
variables: nil,
|
variables: nil,
|
||||||
headers: map[string]interface{}{
|
headers: map[string]interface{}{
|
||||||
"x-apollo-operation-name": "Dragons/github.com/lukaszraczylo/go-simple-graphql",
|
"x-apollo-operation-name": "DragonsQuery",
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantReturned_value: nil,
|
wantReturned_value: `{"dragons":[{"name":"Mock Dragon 1"},{"name":"Mock Dragon 2"}]}`, // Adjusted expected value
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_working",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
wantReturned_value: string(`{"__type":null}`),
|
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "TestBaseClient_Query_tgbotapp_invalid_query",
|
name: "TestBaseClient_Query_invalid_query",
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
args: args{
|
args: args{
|
||||||
query: `query {
|
query: "query { potato { login } ",
|
||||||
__type(name: "Query") {
|
variables: nil,
|
||||||
name
|
headers: map[string]interface{}{
|
||||||
}
|
"x-apollo-operation-name": "InvalidQuery",
|
||||||
`,
|
"content-type": "application/json",
|
||||||
},
|
|
||||||
wantReturned_value: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_invalid_wrong_field",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name2
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
wantReturned_value: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_wrong_url",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v0/graphql",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
wantReturned_value: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_valid_query_mapstring",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
wantType: "mapstring",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
wantReturned_value: map[string]interface{}{"__type": nil},
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_valid_query_byte",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
wantType: "byte",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
wantReturned_value: []byte(`{"__type":null}`),
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_valid_query_string",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
wantType: "string",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
wantReturned_value: string(`{"__type":null}`),
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_valid_query_invalid_type",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
wantType: "potato",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
wantReturned_value: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_valid_query_cache",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v1/graphql",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
variables: map[string]interface{}{
|
|
||||||
"gqlcache": true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
wantReturned_value: string(`{"__type":null}`),
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "TestBaseClient_Query_tgbotapp_valid_query_with_retry",
|
|
||||||
graphQLServer: "https://telegram-bot.app/v0/graphql",
|
|
||||||
wantType: "byte",
|
|
||||||
args: args{
|
|
||||||
query: `query {
|
|
||||||
__type(name: "Query") {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
variables: map[string]interface{}{
|
|
||||||
"gqlretries": true,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantReturned_value: nil,
|
wantReturned_value: nil,
|
||||||
@@ -300,9 +166,8 @@ func (suite *Tests) TestBaseClient_Query() {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
suite.T().Run(tt.name, func(t *testing.T) {
|
suite.T().Run(tt.name, func(t *testing.T) {
|
||||||
b := NewConnection()
|
b := NewConnection()
|
||||||
if tt.graphQLServer != "" {
|
b.SetEndpoint(mockServer.URL)
|
||||||
b.SetEndpoint(tt.graphQLServer)
|
b.SetHTTPClient(mockServer.Client())
|
||||||
}
|
|
||||||
if tt.wantType != "" {
|
if tt.wantType != "" {
|
||||||
b.SetOutput(tt.wantType)
|
b.SetOutput(tt.wantType)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
type BaseClient struct {
|
type BaseClient struct {
|
||||||
cache *cache.Cache
|
cache *cache.Cache
|
||||||
Logger *logging.LogConfig
|
Logger *logging.Logger
|
||||||
client *http.Client
|
client *http.Client
|
||||||
endpoint string
|
endpoint string
|
||||||
responseType string
|
responseType string
|
||||||
|
|||||||
+15
-1
@@ -1,6 +1,7 @@
|
|||||||
package gql
|
package gql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
assertions "github.com/stretchr/testify/assert"
|
assertions "github.com/stretchr/testify/assert"
|
||||||
@@ -12,9 +13,22 @@ type Tests struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
assert *assertions.Assertions
|
assert *assertions.Assertions
|
||||||
|
mockServer *httptest.Server
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (suite *Tests) SetupSuite() {
|
||||||
|
// Start mock GraphQL server
|
||||||
|
mockServer = StartMockServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *Tests) TearDownSuite() {
|
||||||
|
// Close the mock server
|
||||||
|
if mockServer != nil {
|
||||||
|
mockServer.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (suite *Tests) SetupTest() {
|
func (suite *Tests) SetupTest() {
|
||||||
assert = assertions.New(suite.T())
|
assert = assertions.New(suite.T())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user