move resolve into package
This commit is contained in:
57
resolve/resolve.go
Executable file
57
resolve/resolve.go
Executable file
@@ -0,0 +1,57 @@
|
||||
package resolve
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var DefaultNamespace = "namespace"
|
||||
|
||||
func Namespace(ns []string) string {
|
||||
namespace := DefaultNamespace
|
||||
if len(ns) > 0 {
|
||||
segments := []string{}
|
||||
for i := range ns {
|
||||
if ns[i] != "" {
|
||||
segments = append(segments, ns[i])
|
||||
}
|
||||
}
|
||||
namespace = strings.Join(segments, ".")
|
||||
}
|
||||
return namespace
|
||||
}
|
||||
|
||||
func Limits(input []string) []string {
|
||||
return []string{
|
||||
LimitsStart(input),
|
||||
LimitsStop(input),
|
||||
LimitsLimit(input),
|
||||
LimitsAscending(input),
|
||||
}
|
||||
}
|
||||
|
||||
func LimitsStart(input []string) string {
|
||||
if len(input) > 0 {
|
||||
return input[0]
|
||||
}
|
||||
return " "
|
||||
}
|
||||
|
||||
func LimitsStop(input []string) string {
|
||||
if len(input) > 1 {
|
||||
return input[1]
|
||||
}
|
||||
return "}}}}}}"
|
||||
}
|
||||
|
||||
func LimitsLimit(input []string) string {
|
||||
if len(input) > 2 {
|
||||
v, _ := strconv.Atoi(input[2])
|
||||
return strconv.Itoa(v)
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
func LimitsAscending(input []string) string {
|
||||
return strconv.FormatBool(len(input) < 4 || input[3] != "-")
|
||||
}
|
||||
80
resolve/resolve_test.go
Executable file
80
resolve/resolve_test.go
Executable file
@@ -0,0 +1,80 @@
|
||||
package resolve
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResolveLimitsStart(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
"explicit": {in: "no", out: "no"},
|
||||
}
|
||||
|
||||
for name, c := range cases {
|
||||
input := strings.Split(c.in, ",")
|
||||
out := LimitsStart(input)
|
||||
if out != c.out {
|
||||
t.Errorf("%v: got %v, want %v from %v", name, out, c.out, c.in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveLimitsStop(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
"short arr": {in: "", out: "}}}}}}"},
|
||||
"explicit": {in: ",no", out: "no"},
|
||||
}
|
||||
|
||||
for name, c := range cases {
|
||||
input := strings.Split(c.in, ",")
|
||||
out := LimitsStop(input)
|
||||
if out != c.out {
|
||||
t.Errorf("%v: got %v, want %v from %v", name, out, c.out, c.in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveLimitsLimit(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
"15": {in: ",,15", out: "15"},
|
||||
"0 set": {in: ",,0", out: "0"},
|
||||
"0 default": {in: ",,", out: "0"},
|
||||
}
|
||||
|
||||
for name, c := range cases {
|
||||
input := strings.Split(c.in, ",")
|
||||
out := LimitsLimit(input)
|
||||
if out != c.out {
|
||||
t.Errorf("%v: got %v, want %v from %v", name, out, c.out, c.in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveLimitsAscending(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
"desc": {in: "a,b,c,-", out: "false"},
|
||||
"asc": {in: "a,b,c,+", out: "true"},
|
||||
"default asc prev": {in: "a,b,c,", out: "true"},
|
||||
"default asc empty": {in: ",,,", out: "true"},
|
||||
}
|
||||
|
||||
for name, c := range cases {
|
||||
input := strings.Split(c.in, ",")
|
||||
out := LimitsAscending(input)
|
||||
if out != c.out {
|
||||
t.Errorf("%v: got %v, want %v from %v", name, out, c.out, c.in)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user