Help us shape the future of Go! Take the 2026 Go Developer Survey

mapmap

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package mapmap holds a read-only map with a request-scoped layer of edits over it.

A superglobal is read far more often than it is written, and what it reads is the request, which does not change while the request is served. Rendering it into a *model.Array copies every entry into a second representation on the way in, whether or not the script names the variable. A MapMap answers from the request instead, and holds only what a script wrote.

The edits are request-scoped, so their map comes from a pool and goes back cleared rather than freed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapMap

type MapMap struct {
	// contains filtered or unexported fields
}

MapMap reads through a Source and writes to a layer of its own.

Read answers the layer first, so a script that assigns to a key sees what it assigned; the source is left as it was and stays shared. Nothing is copied out of the source until something asks for the whole thing.

func New

func New(src Source) *MapMap

New returns a MapMap reading through src. A nil source reads as empty.

func (*MapMap) Delete

func (m *MapMap) Delete(key string)

Delete forgets key, including one the source holds.

func (*MapMap) Has

func (m *MapMap) Has(key string) bool

Has reports whether anything holds key, which is what isset() asks.

func (*MapMap) Len

func (m *MapMap) Len() int

Len counts what Range would visit, which is count().

func (*MapMap) Range

func (m *MapMap) Range(fn func(key, value any) bool)

Range visits the source in its own order, then the keys only the layer holds.

A key the layer overwrote is visited in the source's position with the written value, so assigning to an existing key does not move it.

func (*MapMap) Read

func (m *MapMap) Read(key string) any

Read answers the value of key, or nil when nothing holds it.

func (*MapMap) Release

func (m *MapMap) Release()

Release returns the layer to the pool. The MapMap reads as the source alone afterwards, so a caller releases when the request it belongs to is over.

func (*MapMap) Write

func (m *MapMap) Write(key string, value any)

Write records a value for key, leaving the source as it was.

type MapSource

type MapSource map[string]string

MapSource reads a plain map. It is the source for a run with no request behind it, which is every CLI invocation.

func (MapSource) Get

func (s MapSource) Get(key string) (any, bool)

Get answers the value of key.

func (MapSource) Len

func (s MapSource) Len() int

Len counts the entries.

func (MapSource) Range

func (s MapSource) Range(fn func(key string, value any) bool) bool

Range visits every entry. Go map order is not insertion order, which is the order a script reading this sees.

type RequestSource

type RequestSource struct {
	Request *http.Request
	Extra   Source
}

RequestSource derives the CGI names from the request itself, so serving one builds no map.

Extra carries the names the request cannot answer on its own, which are the ones a caller computed: the clock for REQUEST_TIME, and whatever the host decided about the scheme behind a proxy. It is a Source of its own so a caller passes the map it already has rather than copying it into another. It is read, never written, and wins over a derived name.

func (*RequestSource) Get

func (s *RequestSource) Get(key string) (any, bool)

Get answers one CGI name.

func (*RequestSource) Len

func (s *RequestSource) Len() int

Len counts the names Range visits.

func (*RequestSource) Range

func (s *RequestSource) Range(fn func(key string, value any) bool) bool

Range visits the derived names, then the extras that are not among them.

type Source

type Source interface {
	// Get answers the value of key and whether the source holds it.
	Get(key string) (any, bool)

	// Len counts the keys Range would visit.
	Len() int

	// Range visits every key in the source until fn answers false.
	Range(fn func(key string, value any) bool) bool
}

Source is the read-only half of a MapMap: the request, or a map built for a run that has no request behind it.

A source is read while the request is served and is never written, so one may be shared by every MapMap over it.

func Lazy

func Lazy(build func() Source) Source

Lazy defers building a source until something reads it, which is what lets a superglobal cost nothing until a script names it.

build runs at most once, on the first Get, Len or Range. A request that reaches no read never parses, and one that reads twice parses once.

type ValueSource

type ValueSource map[string]any

ValueSource reads a map whose values are already PHP values, which is what a superglobal carrying nested data needs: a query string decodes `a[]=1&a[]=2` to an array under one name, and no map of strings can hold that.

func (ValueSource) Get

func (s ValueSource) Get(key string) (any, bool)

Get answers the value of key.

func (ValueSource) Len

func (s ValueSource) Len() int

Len counts the entries.

func (ValueSource) Range

func (s ValueSource) Range(fn func(key string, value any) bool) bool

Range visits every entry.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL