Files
zip/group_closed_test.go
T
zeekayandHanzo Dev 5f71d2496b The router is three nouns, and every one of them is concrete
App, Group, Operation. A group is a prefix and what holds under it, an operation
is one typed route declared there, and the verb methods are generic methods —
which Go 1.27 allows on a concrete type and still refuses on an interface. So In
and Out come from the handler and the caller writes no type arguments:

	users := app.Group("/v1/users")
	users.Post("/", api.Create)

	mint := users.Group("/mint").With(middleware.Mint(gate))
	mint.Post("/deposit", api.Deposit)

A Group is a CLOSED HANDLE: two unexported fields, and no method that hands back
the app, the parent or the fiber underneath. A subsystem given the group at its
own prefix cannot reach above it, so confinement is a property of the type
instead of a rule something has to police — which is what a host needs in order
to hand out a bounded surface and know what comes back.

That is bought by giving up the router interface, deliberately. Router and
OpTarget are gone, and with them the ability to implement a route table outside
this package: a decorator is no longer a router, a gate is middleware on a group,
and a bound is a path. The five package-level verb functions go the same way —
a registration is a method on the thing it registers into.

Raw carries what is not a typed operation: a stream, an upgrade, a probe, a
foreign subtree. It names its method, and it is the longer spelling because
opting out of the typed model is the exception and should read like one.



Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:39:17 -07:00

56 lines
1.8 KiB
Go

package zip_test
import (
"reflect"
"strings"
"testing"
"github.com/zap-proto/zip"
)
// A GROUP CANNOT REACH ABOVE ITSELF, and this file is the compile-and-reflect
// proof of it.
//
// It is the property a host relies on to hand a subsystem a bounded surface: the
// subsystem holds a group at its own prefix, and there is no method that returns
// the app, the parent, the root or the router underneath. So confinement is a
// property of the type rather than a rule something has to police — which is
// what let hanzoai/cloud delete a whole file whose job was policing it, along
// with the bound view, the out-of-bound recording and the boot refusal that
// carried it.
//
// One accessor added back and every subsystem can reach the whole binary again.
// A comment asking for the property is not the property, so this test asks the
// type.
func TestGroup_CannotReachAboveItself(t *testing.T) {
g := reflect.TypeOf(&zip.Group{})
app := reflect.TypeOf(&zip.App{})
for i := range g.NumMethod() {
m := g.Method(i)
switch m.Name {
case "App", "Parent", "Root", "Fiber", "Router", "Unwrap":
t.Errorf("(*Group).%s exists; a group that hands back its host is not a bound", m.Name)
}
for j := range m.Type.NumOut() {
out := m.Type.Out(j)
if out == app {
t.Errorf("(*Group).%s returns *App", m.Name)
}
if p := out.PkgPath(); strings.Contains(p, "fiber") {
t.Errorf("(*Group).%s returns %s, a router this package is meant to hide", m.Name, out)
}
}
}
// And the state it holds is its own: a caller cannot read the app off it, or
// write a prefix onto it, from outside this package.
if n := reflect.TypeOf(zip.Group{}).NumField(); n != 0 {
for i := range n {
if f := reflect.TypeOf(zip.Group{}).Field(i); f.IsExported() {
t.Errorf("Group.%s is exported; the handle is only closed while its fields are", f.Name)
}
}
}
}