Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e4a2a85

Browse files
committedFeb 11, 2025·
encoding/gob: add RegisteredTypes function
Introduce the RegisteredTypes function to return a sequence of all registered types. This makes it easier to inspect registered types, which can be useful for debugging and understanding type usage. Relates: #67401 Fixes: #71605
1 parent 7a2f757 commit e4a2a85

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

‎src/encoding/gob/type.go

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding"
99
"errors"
1010
"fmt"
11+
"iter"
1112
"maps"
1213
"os"
1314
"reflect"
@@ -899,6 +900,16 @@ func Register(value any) {
899900
RegisterName(name, value)
900901
}
901902

903+
// RegisteredTypes is list of registered type with their aliases
904+
// that registered with [Register] or [RegisterName] methods
905+
func RegisteredTypes() iter.Seq2[string, reflect.Type] {
906+
return func(yield func(string, reflect.Type) bool) {
907+
nameToConcreteType.Range(func(k, v any) bool {
908+
return yield(k.(string), v.(reflect.Type))
909+
})
910+
}
911+
}
912+
902913
func registerBasics() {
903914
Register(int(0))
904915
Register(int8(0))

‎src/encoding/gob/type_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,13 @@ func TestTypeRace(t *testing.T) {
260260
close(c)
261261
wg.Wait()
262262
}
263+
264+
func TestRegisteredNames(t *testing.T) {
265+
var names []string
266+
for name, _ := range RegisteredTypes() {
267+
names = append(names, name)
268+
}
269+
if len(names) < 34 {
270+
t.Errorf("registered names should contains all primitive type")
271+
}
272+
}

0 commit comments

Comments
 (0)
Please sign in to comment.