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

morphmap

package
v1.20.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package morphmap holds the process-wide registry of polymorphic aliases shared by the orm and gorm packages. It mirrors fedaco's static Relation._morphMap (libs/fedaco/src/fedaco/ relations/relation.ts:31) while remaining safe for concurrent reads from many query goroutines.

Most app code interacts with the registry via the wrappers in package orm (orm.MorphMap, orm.MorphedModel, orm.MorphAlias, orm.RequireMorphMap, orm.RequiresMorphMap, orm.EnforceMorphMap). The lower-level gorm wrapper imports this package directly to resolve morph values during query construction without introducing a circular dependency on package orm.

Strict morph mode (Relation::requireMorphMap / requiresMorphMap / enforceMorphMap — Relation.php:437/447/459) is a process-level flag: when on, MorphValue raises errors.OrmClassMorphViolation for a model that resolves neither through ModelWithMorphClass nor the registry, instead of returning the not-found pair. The base relations.Pivot type stays exempt (HasRelationships.php:1014); the flag defaults to off.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AliasOf

func AliasOf(model any) (string, bool)

AliasOf returns the alias registered for the given model's underlying type.

func All

func All() map[string]reflect.Type

All returns a snapshot copy of the alias-to-type registry.

func EnforceMorphMap

func EnforceMorphMap(entries map[string]any, merge ...bool)

EnforceMorphMap is Relation::enforceMorphMap (Relation.php:459): requireMorphMap(true) plus Register(entries, merge...) — registering the map and switching strict mode on in one call. PHP returns the resulting map; the Go registry's Register has no return, so neither does this.

func Find

func Find(alias string) any

Find returns a fresh pointer to a new instance of the model registered under alias, or nil if no model is registered.

func MorphValue

func MorphValue(model any) (string, bool, error)

MorphValue resolves the morph class value for a model — the port of Eloquent's Model::getMorphClass (HasRelationships.php:1006). Resolution order:

  1. model.MorphClass() if the model (or its pointer) implements ModelWithMorphClass — an override replaces the whole resolution, exactly as a getMorphClass override does in PHP
  2. global morph map (registered via Register) — HasRelationships.php:1011
  3. the base relations.Pivot type — the `static::class === Pivot::class` carve-out (HasRelationships.php:1014): exempt from strict checking and resolved to its own class name
  4. strict mode (RequireMorphMap / EnforceMorphMap): any other unmapped model raises errors.OrmClassMorphViolation — the ClassMorphViolationException throw at HasRelationships.php:1019

Returns "" and false with a nil error when nothing resolves and strict mode is off. The caller is then expected to fall back to GORM's `polymorphicValue:` tag or the parent's table name (where PHP would return the class name — the documented divergence on orm.MorphAlias).

func Register

func Register(entries map[string]any, merge ...bool)

Register stores a set of alias-to-sample-model bindings. Subsequent calls merge with previously registered entries; later writes win on conflict. Pass false in the optional merge argument to replace the registry instead of merging.

func RequireMorphMap

func RequireMorphMap(requireMorphMap ...bool)

RequireMorphMap ports Relation::requireMorphMap (Relation.php:437): it turns the process-level strict-morph flag on — or off when passed false, the explicit form Laravel's $requireMorphMap parameter takes. The default is true, matching the PHP default argument.

func RequiresMorphMap

func RequiresMorphMap() bool

RequiresMorphMap is Relation::requiresMorphMap (Relation.php:447): the getter half of the flag.

func Reset

func Reset()

Reset clears all entries. Intended for tests. Like Relation::morphMap([], false) it leaves the strict flag untouched — reset that with RequireMorphMap(false), the two halves of the corpus's tearDown (DatabaseEloquentStrictMorphsTest.php:71-77).

Types

This section is empty.

Jump to

Keyboard shortcuts

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