types: move json struct to types package

This commit is contained in:
c9s 2022-08-23 01:54:29 +08:00
parent 3761d6c36b
commit 2611012d28
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 21 additions and 18 deletions

View File

@ -7,26 +7,15 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/util"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text" "github.com/jedib0t/go-pretty/v6/text"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
) )
type JsonStruct struct {
Key string
Json string
Type string
Value interface{}
}
type JsonArr []JsonStruct
func (a JsonArr) Len() int { return len(a) }
func (a JsonArr) Less(i, j int) bool { return a[i].Key < a[j].Key }
func (a JsonArr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func DefaultStyle() *table.Style { func DefaultStyle() *table.Style {
style := table.Style{ style := table.Style{
Name: "StyleRounded", Name: "StyleRounded",
@ -86,7 +75,7 @@ func PrintConfig(s interface{}, f io.Writer, style *table.Style, withColor bool,
if val.Type().Kind() == util.Pointer { if val.Type().Kind() == util.Pointer {
val = val.Elem() val = val.Elem()
} }
var values JsonArr var values types.JsonArr
for i := 0; i < val.Type().NumField(); i++ { for i := 0; i < val.Type().NumField(); i++ {
t := val.Type().Field(i) t := val.Type().Field(i)
if !t.IsExported() { if !t.IsExported() {
@ -123,7 +112,7 @@ func PrintConfig(s interface{}, f io.Writer, style *table.Style, withColor bool,
} }
redundantSet[name] = struct{}{} redundantSet[name] = struct{}{}
value := field.Field(j).Interface() value := field.Field(j).Interface()
values = append(values, JsonStruct{Key: fieldName, Json: name, Type: tt.Type.String(), Value: value}) values = append(values, types.JsonStruct{Key: fieldName, Json: name, Type: tt.Type.String(), Value: value})
} }
} }
} }
@ -133,7 +122,7 @@ func PrintConfig(s interface{}, f io.Writer, style *table.Style, withColor bool,
continue continue
} }
redundantSet[name] = struct{}{} redundantSet[name] = struct{}{}
values = append(values, JsonStruct{Key: fieldName, Json: name, Type: t.Type.String(), Value: val.Field(i).Interface()}) values = append(values, types.JsonStruct{Key: fieldName, Json: name, Type: t.Type.String(), Value: val.Field(i).Interface()})
} }
} }
sort.Sort(values) sort.Sort(values)

14
pkg/types/json.go Normal file
View File

@ -0,0 +1,14 @@
package types
type JsonStruct struct {
Key string
Json string
Type string
Value interface{}
}
type JsonArr []JsonStruct
func (a JsonArr) Len() int { return len(a) }
func (a JsonArr) Less(i, j int) bool { return a[i].Key < a[j].Key }
func (a JsonArr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }