Skip to content

Commit

Permalink
encoding/gob: add RegisteredTypes()
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: golang#67401

Closes: golang#71602
  • Loading branch information
laskoviymishka committed Feb 11, 2025
1 parent 7a2f757 commit 7833e4d
Show file tree
Hide file tree
Showing 2 changed files with 21 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 @@ -8,6 +8,7 @@ import (
"encoding"
"errors"
"fmt"
"iter"
"maps"
"os"
"reflect"
Expand Down Expand Up @@ -899,6 +900,16 @@ func Register(value any) {
RegisterName(name, value)
}

// RegisteredTypes is list of registered type with their aliases
// that registered with [Register] or [RegisterName] methods
func RegisteredTypes() iter.Seq2[string, reflect.Type] {
return func(yield func(string, reflect.Type) bool) {
nameToConcreteType.Range(func(k, v any) bool {
return yield(k.(string), v.(reflect.Type))
})
}
}

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

func TestRegisteredNames(t *testing.T) {
var names []string
for name, _ := range RegisteredTypes() {
names = append(names, name)
}
if len(names) < 34 {
t.Errorf("registered names should contains all primitive type")
}
}

0 comments on commit 7833e4d

Please sign in to comment.