{"modulePath":"github.com/bitechdev/ResolveSpec","version":"v1.3.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package modelregistry\n\nPackage modelregistry is the shared catalogue of the Go model structs that the ResolveSpec front ends (resolvespec, restheadspec, websocketspec, mqttspec, resolvemcp, ...) expose as database entities.\n\nA registry maps a model name (\"schema.entity\") to a struct type and holds:\n\n  - ModelRules: which operations (read/create/update/delete, public or not) are allowed, and whether security checks are disabled.\n  - ModelInfo: optional documentation (description, purpose, tags, per-column descriptions) meant for humans and AI agents. It never affects queries or permissions.\n\nRegister models on a registry created with NewModelRegistry, or through the package-level functions that use the default registry:\n\n\treg := modelregistry.NewModelRegistry()\n\t_ = reg.RegisterModelWithRules(\"public.users\", User{}, modelregistry.DefaultModelRules())\n\treg.SetModelInfo(\"public.users\", modelregistry.ModelInfo{\n\t\tDescription: \"Application accounts\",\n\t\tPurpose:     \"Look up who a person is; never store credentials here\",\n\t\tColumns:     map[string]string{\"email\": \"Login address, unique\"},\n\t})\n\nDescriptions come from, in priority order:\n\n 1. ModelInfo set with SetModelInfo or loaded from an external JSON map with LoadModelInfoFile (the map can be maintained outside the Go code).\n 2. The model's Describer (ModelDescription() string) for the description.\n 3. Struct tags, read per column by FieldComment: comment, note, desc or description tags, then \"comment:\" inside the gorm or bun tag.\n\nModels must be non-pointer structs; pointers, slices and arrays of structs are unwrapped on registration. All registry methods are safe for concurrent use.\n\n## Variables\n\n```go\nvar (\n\tErrModelNotFound = errors.New(\"model not found\")\n\tErrModelExists   = errors.New(\"model already registered\")\n\tErrInvalidModel  = errors.New(\"invalid model\")\n)\n```\nSentinel errors so callers (notably the security layer) can distinguish \"not registered\" from every other failure with errors.Is.\n\n## Functions\n\n```go\nfunc AddRegistry(registry *DefaultModelRegistry)\n```\nAddRegistry adds a registry to the global list of registries Registries are searched in the order they were added\n\n```go\nfunc FieldComment(sf reflect.StructField) string\n```\nFieldComment returns the description of a struct field from its tags. Order: standalone comment/note/desc/description tags, then a \"comment:\" entry inside the gorm tag (semicolon separated), then inside the bun tag (comma separated). It returns \"\" when the field carries none.\n\n```go\nfunc GetModelByName(name string) (interface{}, error)\n```\nGetModelByName retrieves a model by searching through all registries in order Returns the first match found\n\n```go\nfunc GetModels() []interface{}\n```\nGetModels returns a list of all models from all registries. Only the first occurrence of each model name is included.\n\n```go\nfunc IterateModels(fn func(name string, model interface{}))\n```\nIterateModels iterates over all models in the default global registry. It iterates over a snapshot, so fn may safely call back into the registry. A panic in fn is recovered and logged with the model name, and iteration continues with the remaining models.\n\n```go\nfunc RegisterModel(model interface{}, name string) error\n```\nRegisterModel registers a model with the default global registry\n\n```go\nfunc RegisterModelWithRules(model interface{}, name string, rules ModelRules) error\n```\nRegisterModelWithRules registers a model with specific rules in the default registry\n\n```go\nfunc SetDefaultRegistry(registry *DefaultModelRegistry)\n```\nSetDefaultRegistry replaces the default registry. A nil registry is ignored.\n\n```go\nfunc SetModelRules(name string, rules ModelRules) error\n```\nSetModelRules sets the rules for a specific model in the default registry\n\n## Types\n\n```go\ntype DefaultModelRegistry struct {\n\t// contains filtered or unexported fields\n}\n```\nDefaultModelRegistry implements ModelRegistry interface\n\n```go\nfunc GetDefaultRegistry() *DefaultModelRegistry\n```\nGetDefaultRegistry returns the current default registry.\n\n```go\nfunc NewModelRegistry() *DefaultModelRegistry\n```\nNewModelRegistry creates a new model registry\n\n```go\nfunc (r *DefaultModelRegistry) GetAllModels() map[string]interface{}\n```\n\n```go\nfunc (r *DefaultModelRegistry) GetModel(name string) (interface{}, error)\n```\n\n```go\nfunc (r *DefaultModelRegistry) GetModelByEntity(schema, entity string) (interface{}, error)\n```\n\n```go\nfunc (r *DefaultModelRegistry) GetModelInfo(name string) (ModelInfo, bool)\n```\nGetModelInfo returns the documentation stored with SetModelInfo (or loaded from a descriptions file), without any fallback.\n\n```go\nfunc (r *DefaultModelRegistry) GetModelRules(name string) (ModelRules, error)\n```\nGetModelRules retrieves the rules for a specific model Returns default rules if model exists but rules are not set\n\n```go\nfunc (r *DefaultModelRegistry) LoadModelInfo(src io.Reader) (int, error)\n```\nLoadModelInfo reads an external descriptions map from r and applies it. The JSON is an object keyed by model name:\n\n\t{\"public.users\": {\"description\": \"...\", \"purpose\": \"...\", \"tags\": [\"x\"],\n\t                  \"columns\": {\"email\": \"Login address\"}}}\n\nEntries replace any existing info for the same name and take precedence over the model's Describer and its struct-tag comments. It returns the number of models loaded.\n\n```go\nfunc (r *DefaultModelRegistry) LoadModelInfoFile(path string) (int, error)\n```\nLoadModelInfoFile is LoadModelInfo reading from a JSON file.\n\n```go\nfunc (r *DefaultModelRegistry) RegisterModel(name string, model interface{}) error\n```\n\n```go\nfunc (r *DefaultModelRegistry) RegisterModelWithInfo(name string, model interface{}, info ModelInfo) error\n```\nRegisterModelWithInfo registers a model together with its documentation.\n\n```go\nfunc (r *DefaultModelRegistry) RegisterModelWithRules(name string, model interface{}, rules ModelRules) error\n```\nRegisterModelWithRules registers a model with specific rules atomically\n\n```go\nfunc (r *DefaultModelRegistry) ResolveModelInfo(name string) ModelInfo\n```\nResolveModelInfo returns the effective documentation for a registered model. Stored/loaded info wins; an empty Description falls back to the model's Describer. Column descriptions are not resolved here: use the stored map and fall back to FieldComment per field.\n\n```go\nfunc (r *DefaultModelRegistry) SetModelInfo(name string, info ModelInfo)\n```\nSetModelInfo stores documentation for a model name (\"schema.entity\"). The model does not have to be registered yet, so descriptions can be loaded before or after registration. Any previous info for the name is replaced.\n\n```go\nfunc (r *DefaultModelRegistry) SetModelRules(name string, rules ModelRules) error\n```\nSetModelRules sets the rules for a specific model\n\n```go\ntype Describer interface {\n\tModelDescription() string\n}\n```\nDescriber can be implemented by a model to document itself. It is the fallback used when no ModelInfo description was registered or loaded. (The method is not called Description so models may keep a Description field.)\n\n```go\ntype ModelInfo struct {\n\t// Description says what the model/table holds.\n\tDescription string `json:\"description,omitempty\"`\n\t// Purpose says why it exists / when an agent should use it.\n\tPurpose string `json:\"purpose,omitempty\"`\n\t// Tags are free-form labels (e.g. \"billing\", \"pii\").\n\tTags []string `json:\"tags,omitempty\"`\n\t// Columns maps a JSON column name to its description.\n\tColumns map[string]string `json:\"columns,omitempty\"`\n}\n```\nModelInfo is human/AI-facing documentation for a registered model: what it is for and what its columns mean. It is optional and has no effect on permissions or queries.\n\n```go\nfunc (i ModelInfo) IsZero() bool\n```\nIsZero reports whether the info carries no documentation.\n\n```go\ntype ModelRules struct {\n\tCanPublicRead    bool // Whether the model can be read (GET operations)\n\tCanPublicUpdate  bool // Whether the model can be updated (PUT/PATCH operations)\n\tCanPublicCreate  bool // Whether the model can be created (POST operations)\n\tCanPublicDelete  bool // Whether the model can be deleted (DELETE operations)\n\tCanRead          bool // Whether the model can be read (GET operations)\n\tCanUpdate        bool // Whether the model can be updated (PUT/PATCH operations)\n\tCanCreate        bool // Whether the model can be created (POST operations)\n\tCanDelete        bool // Whether the model can be deleted (DELETE operations)\n\tSecurityDisabled bool // Whether security checks are disabled for this model\n}\n```\nModelRules defines the permissions and security settings for a model\n\n```go\nfunc DefaultModelRules() ModelRules\n```\nDefaultModelRules returns the default rules for a model (all operations allowed, security enabled)\n\n```go\nfunc GetModelRules(name string) (ModelRules, error)\n```\nGetModelRules retrieves the rules for a specific model from the default registry\n\n```go\nfunc GetModelRulesByName(name string) (ModelRules, error)\n```\nGetModelRulesByName retrieves the rules for a model by searching through all registries in order Returns the first match found. The error wraps ErrModelNotFound when no registry has the model.\n\n","path":"github.com/bitechdev/ResolveSpec/pkg/modelregistry","name":"modelregistry","synopsis":"Package modelregistry is the shared catalogue of the Go model structs that the ResolveSpec front ends (resolvespec, restheadspec, websocketspec, mqttspec, resolvemcp, ...) expose as database entities.","isRedistributable":true}
