Skip to content

Commit

Permalink
rename spec for clearity
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci committed May 6, 2024
1 parent 0c61721 commit 417aed6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
26 changes: 13 additions & 13 deletions cmd/client/resources/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ObjectNamespaceFlags struct {

var globalAPIResources []*api.APIResource

func generateTableMap(objectSpecs []ObjectSpec, namespace string) map[string]Table {
func generateTableMap(objectSpecs []SpecInfo, namespace string) map[string]Table {
tables := map[string]Table{}

wrapRow := func(row TableRow) TableRow {
Expand Down Expand Up @@ -252,7 +252,7 @@ func GetObject(cmd *cobra.Command, args *general.ArgInfo, kind string, flags *Ob
}

if flags.AllNamespace {
err := unmarshalPrintNamespaceObjectSpec(body, func(s ObjectSpec) bool {
err := unmarshalPrintNamespaceObjectSpec(body, func(s SpecInfo) bool {
return s.GetKind() == kind
})
if err != nil {
Expand All @@ -261,7 +261,7 @@ func GetObject(cmd *cobra.Command, args *general.ArgInfo, kind string, flags *Ob
return nil
}

err = unmarshalPrintMetaSpec(body, !args.ContainName(), func(o ObjectSpec) bool {
err = unmarshalPrintMetaSpec(body, !args.ContainName(), func(o SpecInfo) bool {
return o.GetKind() == kind
})
if err != nil {
Expand All @@ -270,7 +270,7 @@ func GetObject(cmd *cobra.Command, args *general.ArgInfo, kind string, flags *Ob
return nil
}

func unmarshalPrintMetaSpec(body []byte, list bool, filter func(ObjectSpec) bool) error {
func unmarshalPrintMetaSpec(body []byte, list bool, filter func(SpecInfo) bool) error {
specs, err := unmarshalObjectSpec(body, list)
if err != nil {
return err
Expand All @@ -285,7 +285,7 @@ func unmarshalPrintMetaSpec(body []byte, list bool, filter func(ObjectSpec) bool
return nil
}

func unmarshalPrintNamespaceObjectSpec(body []byte, filter func(ObjectSpec) bool) error {
func unmarshalPrintNamespaceObjectSpec(body []byte, filter func(SpecInfo) bool) error {
allObjectSpecs, err := unmarshalNamespaceMetaSpec(body)
if err != nil {
return err
Expand All @@ -307,18 +307,18 @@ func unmarshalPrintNamespaceObjectSpec(body []byte, filter func(ObjectSpec) bool
return nil
}

func unmarshalObjectSpec(body []byte, listBody bool) ([]ObjectSpec, error) {
func unmarshalObjectSpec(body []byte, listBody bool) ([]SpecInfo, error) {
res, err := general.UnmarshalMapInterface(body, listBody)
if err != nil {
return nil, err
}
specs := []ObjectSpec{}
specs := []SpecInfo{}
for _, m := range res {
data, err := codectool.MarshalJSON(m)
if err != nil {
return nil, err
}
spec, err := GetObjectSpec(m["kind"].(string), data)
spec, err := GetSpecInfo(m["kind"].(string), data)
if err != nil {
return nil, err
}
Expand All @@ -327,20 +327,20 @@ func unmarshalObjectSpec(body []byte, listBody bool) ([]ObjectSpec, error) {
return specs, nil
}

func unmarshalNamespaceMetaSpec(body []byte) (map[string][]ObjectSpec, error) {
func unmarshalNamespaceMetaSpec(body []byte) (map[string][]SpecInfo, error) {
raw := map[string][]map[string]interface{}{}
err := codectool.Unmarshal(body, &raw)
if err != nil {
return nil, err
}
res := map[string][]ObjectSpec{}
res := map[string][]SpecInfo{}
for ns, v := range raw {
for _, s := range v {
data, err := codectool.MarshalJSON(s)
if err != nil {
return nil, err
}
spec, err := GetObjectSpec(s["kind"].(string), data)
spec, err := GetSpecInfo(s["kind"].(string), data)
if err != nil {
return nil, err
}
Expand All @@ -350,7 +350,7 @@ func unmarshalNamespaceMetaSpec(body []byte) (map[string][]ObjectSpec, error) {
return res, nil
}

func printNamespaceObjectSpec(metas map[string][]ObjectSpec) {
func printNamespaceObjectSpec(metas map[string][]SpecInfo) {
defaults := metas[DefaultNamespace]
res := generateTableMap(defaults, DefaultNamespace)

Expand All @@ -376,7 +376,7 @@ func printNamespaceObjectSpec(metas map[string][]ObjectSpec) {
}
}

func printObjectSpec(metas []ObjectSpec) {
func printObjectSpec(metas []SpecInfo) {
tableMap := generateTableMap(metas, "")
tables := tableMapToArray(tableMap)
for _, table := range tables {
Expand Down
18 changes: 9 additions & 9 deletions cmd/client/resources/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import (
const defaultTableType = "__default__"

// kindToSpec maps the kind to the spec.
var kindToSpec = map[string]func() ObjectSpec{
httpserver.Kind: func() ObjectSpec { return &HTTPServerSpec{} },
defaultTableType: func() ObjectSpec { return &MetaSpec{} },
var kindToSpec = map[string]func() SpecInfo{
httpserver.Kind: func() SpecInfo { return &HTTPServerSpec{} },
defaultTableType: func() SpecInfo { return &MetaSpec{} },
}

// GetObjectSpec returns the spec of the object, used to generate table.
func GetObjectSpec(kind string, data []byte) (ObjectSpec, error) {
// GetSpecInfo returns the spec of the object, used to generate table.
func GetSpecInfo(kind string, data []byte) (SpecInfo, error) {
specFn, ok := kindToSpec[kind]
if !ok {
specFn = kindToSpec[defaultTableType]
Expand All @@ -60,8 +60,8 @@ func (row TableRow) WithNamespace(namespace string) TableRow {
return append(row, namespace)
}

// ObjectSpec is the interface of the object spec.
type ObjectSpec interface {
// SpecInfo is the interface for the object specs to get information used by table.
type SpecInfo interface {
GetName() string
GetKind() string
GetAge() string
Expand All @@ -74,7 +74,7 @@ type MetaSpec struct {
supervisor.MetaSpec `json:",inline"`
}

var _ ObjectSpec = &MetaSpec{}
var _ SpecInfo = &MetaSpec{}

// GetName returns the name of the object.
func (m *MetaSpec) GetName() string {
Expand Down Expand Up @@ -111,7 +111,7 @@ type HTTPServerSpec struct {
httpserver.Spec `json:",inline"`
}

var _ ObjectSpec = &HTTPServerSpec{}
var _ SpecInfo = &HTTPServerSpec{}

// TableHeader returns the table header.
func (h *HTTPServerSpec) TableHeader() (string, TableRow) {
Expand Down

0 comments on commit 417aed6

Please sign in to comment.