-
Notifications
You must be signed in to change notification settings - Fork 70
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
1 parent
8dff604
commit 3464ed3
Showing
3 changed files
with
49 additions
and
6 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,20 @@ | ||
package json | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/sagernet/sing/common" | ||
) | ||
|
||
func ObjectKeys(object reflect.Type) []string { | ||
switch object.Kind() { | ||
case reflect.Pointer: | ||
return ObjectKeys(object.Elem()) | ||
case reflect.Struct: | ||
default: | ||
panic("invalid non-struct input") | ||
} | ||
return common.Map(cachedTypeFields(object).list, func(field field) string { | ||
return field.name | ||
}) | ||
} |
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,25 @@ | ||
package json_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
json "github.com/sagernet/sing/common/json/internal/contextjson" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MyObject struct { | ||
Hello string `json:"hello,omitempty"` | ||
MyWorld | ||
MyWorld2 string `json:"-"` | ||
} | ||
|
||
type MyWorld struct { | ||
World string `json:"world,omitempty"` | ||
} | ||
|
||
func TestObjectKeys(t *testing.T) { | ||
keys := json.ObjectKeys(reflect.TypeOf(&MyObject{})) | ||
require.Equal(t, []string{"hello", "world"}, keys) | ||
} |