mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-08-25 20:38:13 +02:00
Refactor: move internal package from src/ to internal/ and move src/main.go to cmd/eosio-api-healthcheck/main.go
This commit is contained in:
parent
c27abb5ed9
commit
6448aeb0f7
22 changed files with 19 additions and 17 deletions
17
internal/utils/json.go
Normal file
17
internal/utils/json.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
package utils
|
||||
|
||||
// JsonGetInt64
|
||||
// performs float64 (json numbers are always float64)
|
||||
// type assertion and casts to int64.
|
||||
//
|
||||
// if the type assertion fails, the function defaults 0 (zero).
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func JsonGetInt64(input interface{}) (int64) {
|
||||
v, res := input.(float64)
|
||||
if res {
|
||||
return (int64) (v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
22
internal/utils/json_test.go
Normal file
22
internal/utils/json_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestJsonGetInt64(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
want int64
|
||||
}{
|
||||
{"String", "test", 0 },
|
||||
{"Int", 1234, 0 },
|
||||
{"Float", float64(1234), 1234 },
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := JsonGetInt64(tt.input); got != tt.want {
|
||||
t.Errorf("JsonGetInt64() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
20
internal/utils/parse_log_formatter.go
Normal file
20
internal/utils/parse_log_formatter.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
package utils
|
||||
|
||||
import (
|
||||
log "github.com/inconshreveable/log15"
|
||||
)
|
||||
|
||||
func ParseLogFormatter(name string) log.Format {
|
||||
|
||||
switch name {
|
||||
case "logfmt" :
|
||||
return log.LogfmtFormat()
|
||||
case "json" :
|
||||
return log.JsonFormat()
|
||||
case "json-pretty" :
|
||||
return log.JsonFormatEx(true, true)
|
||||
default :
|
||||
return log.TerminalFormat()
|
||||
}
|
||||
}
|
||||
29
internal/utils/parse_log_formatter_test.go
Normal file
29
internal/utils/parse_log_formatter_test.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
log "github.com/inconshreveable/log15"
|
||||
)
|
||||
|
||||
func Test_ParseLogFormatter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
want log.Format
|
||||
}{
|
||||
{ "Default", "", log.TerminalFormat() },
|
||||
{ "LogFmt", "logfmt", log.LogfmtFormat() },
|
||||
{ "Json", "json", log.JsonFormat() },
|
||||
{ "JsonPretty", "json-pretty", log.JsonFormat() },
|
||||
{ "Unknown", "unknown", log.TerminalFormat() },
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ParseLogFormatter(tt.arg); reflect.ValueOf(got).Pointer() != reflect.ValueOf(tt.want).Pointer() {
|
||||
t.Errorf("parseLogFormatter() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
22
internal/utils/time.go
Normal file
22
internal/utils/time.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Time struct {
|
||||
ts time.Time
|
||||
}
|
||||
|
||||
func (t *Time) SetTime(value time.Time) {
|
||||
t.ts = value
|
||||
}
|
||||
|
||||
func (t Time) GetTime() time.Time {
|
||||
|
||||
if ! t.ts.IsZero() {
|
||||
return t.ts
|
||||
}
|
||||
return time.Now().In(time.UTC)
|
||||
}
|
||||
26
internal/utils/time_test.go
Normal file
26
internal/utils/time_test.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTimeGetTimeWithDefaultValue(t *testing.T) {
|
||||
|
||||
var ts Time
|
||||
|
||||
// Assert that time is NOW (+-10 seconds)
|
||||
assert.InDelta(t, ts.GetTime().Unix(), time.Now().In(time.UTC).Unix(), float64(10))
|
||||
}
|
||||
|
||||
func TestTimeGetTimeWithSetTime(t *testing.T) {
|
||||
|
||||
var ts Time
|
||||
|
||||
expected := time.Unix(1048722042, 500)
|
||||
ts.SetTime(expected)
|
||||
|
||||
assert.Equal(t, expected, ts.GetTime())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue