Skip to content

Commit

Permalink
Changed variable names and added more cases for key datatypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverLok committed Dec 5, 2024
1 parent 21e9a3d commit e73d107
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions cmd/call-endpoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"sort"
"strconv"
"strings"

"github.com/alecthomas/kong"
Expand All @@ -25,9 +26,8 @@ var args struct {
}

const (
USERS = "users"
COURSES = "courses"
TYPE = "type"
USERS_KEY = "users"
COURSES_KEY = "courses"
)

func main() {
Expand Down Expand Up @@ -107,7 +107,7 @@ func printCMDResponseTable(response core.APIResponse) string {
return ""
}

users, ok := responseContent[USERS].([]any)
users, ok := responseContent[USERS_KEY].([]any)
if !ok {
return ""
}
Expand All @@ -119,24 +119,25 @@ func printCMDResponseTable(response core.APIResponse) string {

var headers []string
for key := range firstUser {
if key == COURSES {
if key == COURSES_KEY {
continue
}

headers = append(headers, key)
}

sort.Strings(headers)

// Add courses to the end of the slice for better readability in the output.
_, exists := firstUser[COURSES]
_, exists := firstUser[COURSES_KEY]
if exists {
headers = append(headers, COURSES)
// Add courses to the end of the slice for better readability in the output.
headers = append(headers, COURSES_KEY)
}

var usersTable strings.Builder
usersTable.WriteString(strings.Join(headers, "\t"))

lines := strings.Split(usersTable.String(), "\t")
headerKeys := strings.Split(usersTable.String(), "\t")

usersTable.WriteString("\n")

Expand All @@ -147,10 +148,14 @@ func printCMDResponseTable(response core.APIResponse) string {
}

var row []string
for _, key := range lines {
for _, key := range headerKeys {
switch value := userMap[key].(type) {
case string:
row = append(row, value)
case int:
row = append(row, strconv.Itoa(value))
case bool:
row = append(row, strconv.FormatBool(value))
default:
row = append(row, util.MustToJSON(value))
}
Expand Down

0 comments on commit e73d107

Please sign in to comment.