improve package gcmd

This commit is contained in:
John Guo 2022-01-01 00:18:50 +08:00
parent 00692c50da
commit 09f9d1cb7a
3 changed files with 81 additions and 1 deletions

View File

@ -103,7 +103,8 @@ func (s *Server) doBindObject(ctx context.Context, in doBindObjectInput) {
shutFunc func(*Request)
)
// If given `object` is not pointer, it then creates a temporary one,
// of which the value is `v`.
// of which the value is `reflectValue`.
// It then can retrieve all the methods both of struct/*struct.
if reflectValue.Kind() == reflect.Struct {
newValue := reflect.New(reflectType)
newValue.Elem().Set(reflectValue)

View File

@ -46,6 +46,16 @@ func NewFromObject(object interface{}) (rootCmd *Command, err error) {
)
return
}
var reflectValue = originValueAndKind.InputValue
// If given `object` is not pointer, it then creates a temporary one,
// of which the value is `reflectValue`.
// It then can retrieve all the methods both of struct/*struct.
if reflectValue.Kind() == reflect.Struct {
newValue := reflect.New(reflectValue.Type())
newValue.Elem().Set(reflectValue)
reflectValue = newValue
}
// Root command creating.
rootCmd, err = newCommandFromObjectMeta(object)
if err != nil {

View File

@ -133,6 +133,19 @@ func Test_Command_RootTag(t *testing.T) {
cmd, err := gcmd.NewFromObject(TestObjectForRootTag{})
t.AssertNil(err)
os.Args = []string{"root", "-n=john"}
value, err := cmd.RunWithValue(ctx)
t.AssertNil(err)
t.Assert(value, `{"Content":"john"}`)
})
// Pointer.
gtest.C(t, func(t *gtest.T) {
var (
ctx = gctx.New()
)
cmd, err := gcmd.NewFromObject(&TestObjectForRootTag{})
t.AssertNil(err)
os.Args = []string{"root", "-n=john"}
value, err := cmd.RunWithValue(ctx)
t.AssertNil(err)
@ -188,3 +201,59 @@ func Test_Command_NeedArgs(t *testing.T) {
t.Assert(value, `{"Args":["a","b","john"]}`)
})
}
type TestObjectPointerTag struct {
g.Meta `name:"root" root:"root"`
}
type TestObjectPointerTagEnvInput struct {
g.Meta `name:"env" usage:"root env" brief:"root env command" dc:"root env command description" ad:"root env command ad"`
}
type TestObjectPointerTagEnvOutput struct{}
type TestObjectPointerTagTestInput struct {
g.Meta `name:"root"`
Name string `v:"required" short:"n" orphan:"false" brief:"name for test command"`
}
type TestObjectPointerTagTestOutput struct {
Content string
}
func (c *TestObjectPointerTag) Env(ctx context.Context, in TestObjectPointerTagEnvInput) (out *TestObjectPointerTagEnvOutput, err error) {
return
}
func (c *TestObjectPointerTag) Root(ctx context.Context, in TestObjectPointerTagTestInput) (out *TestObjectPointerTagTestOutput, err error) {
out = &TestObjectPointerTagTestOutput{
Content: in.Name,
}
return
}
func Test_Command_Pointer(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctx = gctx.New()
)
cmd, err := gcmd.NewFromObject(TestObjectPointerTag{})
t.AssertNil(err)
os.Args = []string{"root", "-n=john"}
value, err := cmd.RunWithValue(ctx)
t.AssertNil(err)
t.Assert(value, `{"Content":"john"}`)
})
gtest.C(t, func(t *gtest.T) {
var (
ctx = gctx.New()
)
cmd, err := gcmd.NewFromObject(&TestObjectPointerTag{})
t.AssertNil(err)
os.Args = []string{"root", "-n=john"}
value, err := cmd.RunWithValue(ctx)
t.AssertNil(err)
t.Assert(value, `{"Content":"john"}`)
})
}