Document pgtype.Map and pgtype.Type are immutable after registration

pull/1644/head
Jack Christensen 2023-06-14 08:27:04 -05:00
parent 90f9aad67f
commit 45520d5a11
1 changed files with 7 additions and 2 deletions

View File

@ -147,7 +147,7 @@ const (
BinaryFormatCode = 1 BinaryFormatCode = 1
) )
// A Codec converts between Go and PostgreSQL values. // A Codec converts between Go and PostgreSQL values. A Codec must not be mutated after it is registered with a Map.
type Codec interface { type Codec interface {
// FormatSupported returns true if the format is supported. // FormatSupported returns true if the format is supported.
FormatSupported(int16) bool FormatSupported(int16) bool
@ -178,6 +178,7 @@ func (e *nullAssignmentError) Error() string {
return fmt.Sprintf("cannot assign NULL to %T", e.dst) return fmt.Sprintf("cannot assign NULL to %T", e.dst)
} }
// Type represents a PostgreSQL data type. It must not be mutated after it is registered with a Map.
type Type struct { type Type struct {
Codec Codec Codec Codec
Name string Name string
@ -244,6 +245,7 @@ func NewMap() *Map {
} }
} }
// RegisterType registers a data type with the Map. t must not be mutated after it is registered.
func (m *Map) RegisterType(t *Type) { func (m *Map) RegisterType(t *Type) {
m.oidToType[t.OID] = t m.oidToType[t.OID] = t
m.nameToType[t.Name] = t m.nameToType[t.Name] = t
@ -275,6 +277,7 @@ func (m *Map) RegisterDefaultPgType(value any, name string) {
} }
} }
// TypeForOID returns the Type registered for the given OID. The returned Type must not be mutated.
func (m *Map) TypeForOID(oid uint32) (*Type, bool) { func (m *Map) TypeForOID(oid uint32) (*Type, bool) {
if dt, ok := m.oidToType[oid]; ok { if dt, ok := m.oidToType[oid]; ok {
return dt, true return dt, true
@ -284,6 +287,7 @@ func (m *Map) TypeForOID(oid uint32) (*Type, bool) {
return dt, ok return dt, ok
} }
// TypeForName returns the Type registered for the given name. The returned Type must not be mutated.
func (m *Map) TypeForName(name string) (*Type, bool) { func (m *Map) TypeForName(name string) (*Type, bool) {
if dt, ok := m.nameToType[name]; ok { if dt, ok := m.nameToType[name]; ok {
return dt, true return dt, true
@ -303,7 +307,8 @@ func (m *Map) buildReflectTypeToType() {
} }
// TypeForValue finds a data type suitable for v. Use RegisterType to register types that can encode and decode // TypeForValue finds a data type suitable for v. Use RegisterType to register types that can encode and decode
// themselves. Use RegisterDefaultPgType to register that can be handled by a registered data type. // themselves. Use RegisterDefaultPgType to register that can be handled by a registered data type. The returned Type
// must not be mutated.
func (m *Map) TypeForValue(v any) (*Type, bool) { func (m *Map) TypeForValue(v any) (*Type, bool) {
if m.reflectTypeToType == nil { if m.reflectTypeToType == nil {
m.buildReflectTypeToType() m.buildReflectTypeToType()