diff options
author | Runxi Yu <me@runxiyu.org> | 2025-08-17 03:09:52 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-08-17 03:09:52 +0800 |
commit | 009a6e397651a9540b6c6bb74ef2230eeda9b577 (patch) | |
tree | 0a808670d95aba5804a73a558cc335a49d230aa6 /forged/internal/bare/unions.go | |
parent | Remove HTML templates from main server (diff) | |
download | forge-009a6e397651a9540b6c6bb74ef2230eeda9b577.tar.gz forge-009a6e397651a9540b6c6bb74ef2230eeda9b577.tar.zst forge-009a6e397651a9540b6c6bb74ef2230eeda9b577.zip |
Some mass renaming
Diffstat (limited to 'forged/internal/bare/unions.go')
-rw-r--r-- | forged/internal/bare/unions.go | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/forged/internal/bare/unions.go b/forged/internal/bare/unions.go deleted file mode 100644 index 0270a5f..0000000 --- a/forged/internal/bare/unions.go +++ /dev/null @@ -1,79 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: Copyright (c) 2025 Drew Devault <https://drewdevault.com> - -package bare - -import ( - "fmt" - "reflect" -) - -// Any type which is a union member must implement this interface. You must -// also call RegisterUnion for go-bare to marshal or unmarshal messages which -// utilize your union type. -type Union interface { - IsUnion() -} - -type UnionTags struct { - iface reflect.Type - tags map[reflect.Type]uint64 - types map[uint64]reflect.Type -} - -var unionInterface = reflect.TypeOf((*Union)(nil)).Elem() -var unionRegistry map[reflect.Type]*UnionTags - -func init() { - unionRegistry = make(map[reflect.Type]*UnionTags) -} - -// Registers a union type in this context. Pass the union interface and the -// list of types associated with it, sorted ascending by their union tag. -func RegisterUnion(iface interface{}) *UnionTags { - ity := reflect.TypeOf(iface).Elem() - if _, ok := unionRegistry[ity]; ok { - panic(fmt.Errorf("Type %s has already been registered", ity.Name())) - } - - if !ity.Implements(reflect.TypeOf((*Union)(nil)).Elem()) { - panic(fmt.Errorf("Type %s does not implement bare.Union", ity.Name())) - } - - utypes := &UnionTags{ - iface: ity, - tags: make(map[reflect.Type]uint64), - types: make(map[uint64]reflect.Type), - } - unionRegistry[ity] = utypes - return utypes -} - -func (ut *UnionTags) Member(t interface{}, tag uint64) *UnionTags { - ty := reflect.TypeOf(t) - if !ty.AssignableTo(ut.iface) { - panic(fmt.Errorf("Type %s does not implement interface %s", - ty.Name(), ut.iface.Name())) - } - if _, ok := ut.tags[ty]; ok { - panic(fmt.Errorf("Type %s is already registered for union %s", - ty.Name(), ut.iface.Name())) - } - if _, ok := ut.types[tag]; ok { - panic(fmt.Errorf("Tag %d is already registered for union %s", - tag, ut.iface.Name())) - } - ut.tags[ty] = tag - ut.types[tag] = ty - return ut -} - -func (ut *UnionTags) TagFor(v interface{}) (uint64, bool) { - tag, ok := ut.tags[reflect.TypeOf(v)] - return tag, ok -} - -func (ut *UnionTags) TypeFor(tag uint64) (reflect.Type, bool) { - t, ok := ut.types[tag] - return t, ok -} |