Skip to content

Commit

Permalink
encoding/gob: add RegisteredNames()
Browse files Browse the repository at this point in the history
Starting from Go 1.23, direct access to `encoding/gob.nameToConcreteType` is no longer possible due to the removal of internal symbol linking. While this change improves encapsulation, it also removes the ability to retrieve a list of all registered types in the gob package.

Relates: #67401

Closes: #71602
  • Loading branch information
laskoviymishka committed Feb 7, 2025
1 parent 7a2f757 commit f42ab36
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/encoding/gob/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,17 @@ func Register(value any) {
RegisterName(name, value)
}

// RegisteredNames is list of registered type names that registered
// with [Register] or [RegisterName] methods
func RegisteredNames() []string {
var val []string
nameToConcreteType.Range(func(k, v any) bool {
val = append(val, k.(string))
return true
})
return val
}

func registerBasics() {
Register(int(0))
Register(int8(0))
Expand Down
7 changes: 7 additions & 0 deletions src/encoding/gob/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,10 @@ func TestTypeRace(t *testing.T) {
close(c)
wg.Wait()
}

func TestRegisteredNames(t *testing.T) {
names := RegisteredNames()
if len(names) < 34 {
t.Errorf("registered names should contains all primitive type")
}
}

0 comments on commit f42ab36

Please sign in to comment.