From 09f9d1cb7a9ebeba661118e83f5c839a82152627 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sat, 1 Jan 2022 00:18:50 +0800 Subject: [PATCH] improve package gcmd --- net/ghttp/ghttp_server_service_object.go | 3 +- os/gcmd/gcmd_command_object.go | 10 +++ os/gcmd/gcmd_z_unit_feature_object1_test.go | 69 +++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/net/ghttp/ghttp_server_service_object.go b/net/ghttp/ghttp_server_service_object.go index 13e141fab..543492e34 100644 --- a/net/ghttp/ghttp_server_service_object.go +++ b/net/ghttp/ghttp_server_service_object.go @@ -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) diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 9308bf8d7..4948741c7 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -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 { diff --git a/os/gcmd/gcmd_z_unit_feature_object1_test.go b/os/gcmd/gcmd_z_unit_feature_object1_test.go index c6c62bd7d..901062d74 100644 --- a/os/gcmd/gcmd_z_unit_feature_object1_test.go +++ b/os/gcmd/gcmd_z_unit_feature_object1_test.go @@ -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"}`) + }) +}