diff options
author | Will Winder <wwinder.unh@gmail.com> | 2022-04-24 10:02:29 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-24 10:02:29 -0400 |
commit | cfe6e57874948dc50c51c8e8fa4e5c9d8b2c7317 (patch) | |
tree | 9bd7ebec377ab206190c2b79787c3479594ee166 | |
parent | 452af1c8c7156edc89216efef1115942d6747005 (diff) |
algocfg: Fix algocfg get for non-string parameters. (#3907)jackstest
-rw-r--r-- | cmd/algocfg/getCommand.go | 10 | ||||
-rw-r--r-- | cmd/algocfg/getCommand_test.go | 64 |
2 files changed, 69 insertions, 5 deletions
diff --git a/cmd/algocfg/getCommand.go b/cmd/algocfg/getCommand.go index 86af9cc10..c22ff597c 100644 --- a/cmd/algocfg/getCommand.go +++ b/cmd/algocfg/getCommand.go @@ -51,14 +51,14 @@ var getCmd = &cobra.Command{ return } - val, err := getObjectProperty(cfg, getParameterArg) + val, err := serializeObjectProperty(cfg, getParameterArg) if err != nil { reportWarnf("Error retrieving property '%s' - %s", getParameterArg, err) anyError = true return } - fmt.Printf("%s", val) + fmt.Print(val) }) if anyError { os.Exit(1) @@ -66,14 +66,14 @@ var getCmd = &cobra.Command{ }, } -func getObjectProperty(object interface{}, property string) (ret interface{}, err error) { +func serializeObjectProperty(object interface{}, property string) (ret string, err error) { v := reflect.ValueOf(object) val := reflect.Indirect(v) f := val.FieldByName(property) if !f.IsValid() { - return object, fmt.Errorf("unknown property named '%s'", property) + return "", fmt.Errorf("unknown property named '%s'", property) } - return f.Interface(), nil + return fmt.Sprintf("%v", f.Interface()), nil } diff --git a/cmd/algocfg/getCommand_test.go b/cmd/algocfg/getCommand_test.go new file mode 100644 index 000000000..0547c1ccc --- /dev/null +++ b/cmd/algocfg/getCommand_test.go @@ -0,0 +1,64 @@ +// Copyright (C) 2019-2022 Algorand, Inc. +// This file is part of go-algorand +// +// go-algorand is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// go-algorand is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. + +package main + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/algorand/go-algorand/test/partitiontest" +) + +func TestPrint(t *testing.T) { + partitiontest.PartitionTest(t) + + testcases := []struct { + Input interface{} + expected string + }{ + { + Input: "string", + expected: "string", + }, + { + Input: uint64(1234), + expected: "1234", + }, + { + Input: int64(-1234), + expected: "-1234", + }, + { + Input: true, + expected: "true", + }, + { + Input: time.Second, + expected: "1s", + }, + } + for i, tc := range testcases { + t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) { + ret, err := serializeObjectProperty(tc, "Input") + assert.NoError(t, err) + assert.Equal(t, tc.expected, ret) + }) + } +} |