This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package typiobj | ||
|
||
import "reflect" | ||
|
||
// Name of object. Return value from name field if available or return its type. | ||
func Name(obj interface{}) (name string) { | ||
val := reflect.Indirect(reflect.ValueOf(obj)).FieldByName("Name") | ||
name = val.String() | ||
if name == "" || name == "<invalid Value>" { | ||
typ := reflect.TypeOf(obj) | ||
name = typ.Name() | ||
if name == "" { | ||
name = typ.String() | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Description of Object. Return value from description field if available or return its type | ||
func Description(obj interface{}) (description string) { | ||
val := reflect.Indirect(reflect.ValueOf(obj)).FieldByName("Description") | ||
description = val.String() | ||
if description == "<invalid Value>" { | ||
description = "" | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package typiobj_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj" | ||
) | ||
|
||
type SampleAttribute struct { | ||
Name string | ||
Description string | ||
} | ||
|
||
func TestName(t *testing.T) { | ||
testcases := []struct { | ||
obj interface{} | ||
name string | ||
}{ | ||
{ | ||
obj: SampleAttribute{ | ||
Name: "some-name", | ||
}, | ||
name: "some-name", | ||
}, | ||
{ | ||
obj: SampleAttribute{}, | ||
name: "SampleAttribute", | ||
}, | ||
{ | ||
obj: struct{}{}, | ||
name: "struct {}", | ||
}, | ||
} | ||
for _, tt := range testcases { | ||
require.Equal(t, tt.name, typiobj.Name(tt.obj)) | ||
} | ||
} | ||
|
||
func TestDescription(t *testing.T) { | ||
testcases := []struct { | ||
obj interface{} | ||
description string | ||
}{ | ||
{ | ||
obj: SampleAttribute{ | ||
Description: "some-description", | ||
}, | ||
description: "some-description", | ||
}, | ||
{ | ||
obj: SampleAttribute{}, | ||
description: "", | ||
}, | ||
{ | ||
obj: struct{}{}, | ||
description: "", | ||
}, | ||
} | ||
for _, tt := range testcases { | ||
require.Equal(t, tt.description, typiobj.Description(tt.obj)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters