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

gormnul

package module
v0.1.0 Latest Latest
Warning

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

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

README

gormnul

Go Go Reference

A GORM plugin that strips NUL (\x00) characters from PostgreSQL text and JSON write values.

TL;DR

PostgreSQL does not accept NUL in text or jsonb. One stray \x00 from user input or an upstream API fails the whole write:

ERROR: invalid byte sequence for encoding "UTF8": 0x00 (SQLSTATE 22021)
ERROR: unsupported Unicode escape sequence (SQLSTATE 22P05)

gormnul removes NUL from the values GORM binds for text and JSON columns, so these writes succeed. Binary (bytea) data is left untouched.

Installation

go get github.com/wuhan005/gormnul

Usage

Register the plugin once on the root *gorm.DB, before it is used concurrently:

db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
	return err
}

if err := db.Use(gormnul.Plugin{}); err != nil {
	return err
}

After that, Create, CreateInBatches, Save, Updates, UpdateColumn(s) and OnConflict upserts are cleaned automatically.

License

MIT License

Documentation

Overview

Package gormnul removes NUL from PostgreSQL text and JSON write values. Register Plugin once, before the root GORM DB is used concurrently. It changes bound write values, not the input model or WHERE conditions. Model schema must identify column types. Table-only maps with untyped JSON strings, raw SQL, and SQL expressions need a separate typed write boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Plugin

type Plugin struct{}

Plugin applies an intentionally lossy policy: delete NUL in text/JSON values, reject JSON keys containing NUL, and preserve binary data. It handles GORM's VALUES, SET and ON CONFLICT clauses. Raw SQL and SQL expressions are opaque.

func (Plugin) Initialize

func (Plugin) Initialize(db *gorm.DB) error

func (Plugin) Name

func (Plugin) Name() string

Jump to

Keyboard shortcuts

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