Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

readonly.gno

3.06 Kb · 103 lines
  1package groups
  2
  3import "gno.land/p/moul/addrset/v0"
  4
  5// ReadonlyRole is a read-only view of a Role. It exposes only read-side
  6// methods and holds the *Role in an unexported field, so cross-package
  7// callers cannot mutate the role through this type.
  8type ReadonlyRole struct {
  9	role *Role
 10}
 11
 12// Name returns the role's name.
 13func (rr ReadonlyRole) Name() string {
 14	return rr.role.name
 15}
 16
 17// Members returns a read-only view of the role's member set.
 18func (rr ReadonlyRole) Members() *addrset.ReadonlySet {
 19	return rr.role.members.Readonly()
 20}
 21
 22// Meta returns the role's metadata slot.
 23//
 24// NOTE: a mutable pointer stored in meta is NOT protected by this readonly
 25// view — the pointee remains mutable by anyone who retrieves it. See the
 26// package doc.
 27func (rr ReadonlyRole) Meta() any {
 28	return rr.role.meta
 29}
 30
 31// ReadonlyGroup is a read-only view of a Group. Every method mirrors the
 32// read-side of Group; mutators are absent. It holds the *Group in an
 33// unexported field, so cross-package callers cannot mutate through it.
 34type ReadonlyGroup struct {
 35	group *Group
 36}
 37
 38// --- Base set (base only) ---
 39
 40// Has reports whether addr is in the base set (roles not consulted).
 41func (rg ReadonlyGroup) Has(addr address) bool {
 42	return rg.group.Has(addr)
 43}
 44
 45// Size returns the number of addresses in the base set only.
 46func (rg ReadonlyGroup) Size() int {
 47	return rg.group.Size()
 48}
 49
 50// Iterate walks the base set (only); see Group.Iterate.
 51func (rg ReadonlyGroup) Iterate(offset, count int, fn func(addr address) bool) (stopped bool) {
 52	return rg.group.Iterate(offset, count, fn)
 53}
 54
 55// --- Role registry ---
 56
 57// GetRole returns a read-only view of the named role if it exists.
 58func (rg ReadonlyGroup) GetRole(name string) (rr *ReadonlyRole, found bool) {
 59	r, ok := rg.group.GetRole(name)
 60	if !ok {
 61		return nil, false
 62	}
 63	return r.Readonly(), true
 64}
 65
 66// HasRole reports whether a role with the given name exists.
 67func (rg ReadonlyGroup) HasRole(name string) bool {
 68	return rg.group.HasRole(name)
 69}
 70
 71// RoleCount returns the number of registered roles.
 72func (rg ReadonlyGroup) RoleCount() int {
 73	return rg.group.RoleCount()
 74}
 75
 76// IterateRoles walks roles in name order; see Group.IterateRoles.
 77func (rg ReadonlyGroup) IterateRoles(offset, count int, fn func(*ReadonlyRole) bool) (stopped bool) {
 78	return rg.group.IterateRoles(offset, count, fn)
 79}
 80
 81// --- Aggregations ---
 82
 83// HasAny reports whether addr is in the base set OR in any role.
 84func (rg ReadonlyGroup) HasAny(addr address) bool {
 85	return rg.group.HasAny(addr)
 86}
 87
 88// TotalSize returns the deduplicated count across base + all roles.
 89func (rg ReadonlyGroup) TotalSize() int {
 90	return rg.group.TotalSize()
 91}
 92
 93// IterateAll walks every distinct address across base + all roles; see
 94// Group.IterateAll.
 95func (rg ReadonlyGroup) IterateAll(offset, count int, fn func(addr address) bool) (stopped bool) {
 96	return rg.group.IterateAll(offset, count, fn)
 97}
 98
 99// RolesContaining returns the names of all roles containing addr, in name
100// order; see Group.RolesContaining.
101func (rg ReadonlyGroup) RolesContaining(addr address) []string {
102	return rg.group.RolesContaining(addr)
103}