xmlwire

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 14 Imported by: 5 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package xmlwire provides bounded XML encoding and decoding with explicit namespace, root-element, strictness, and character-set policy.

Use Decode or Root for XML 1.0/namespace-aware token validation, then Encode for package-selected XML, not canonical XML or schema validation. Decode uses independent validation and target passes; charset callbacks may run twice. Directives are not DTD validation or external entity acquisition. Reader calls buffer input and never close it. Targets/custom callbacks are caller-owned; no context API, transport, cgo or background workers are introduced.

Index

Constants

View Source
const (
	// DefaultMaxBytes is the default maximum XML document size.
	DefaultMaxBytes int64 = 1 << 20
	// DefaultMaxDepth is the default maximum nested XML element depth.
	DefaultMaxDepth = 1000
)
View Source
const SchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"

SchemaInstanceNamespace is the XML Schema instance namespace that owns the xsi:nil attribute.

Variables

View Source
var ErrNestingTooDeep = errors.New("XML nesting exceeds depth limit")

ErrNestingTooDeep identifies XML documents over the configured depth limit.

View Source
var ErrPayloadTooLarge = errors.New("payload exceeds size limit")

ErrPayloadTooLarge identifies XML documents over the configured limit.

Functions

func CharsetReader

func CharsetReader(label string, input io.Reader) (io.Reader, error)

CharsetReader converts a deliberately limited set of common vendor encodings to UTF-8 with DefaultMaxBytes as the raw input limit. Labels are limited to 64 bytes before normalization. Conversion may produce up to three UTF-8 bytes per input byte. Caller reader code must return promptly.

func CharsetReaderWithLimit

func CharsetReaderWithLimit(label string, input io.Reader, maxBytes int64) (io.Reader, error)

CharsetReaderWithLimit converts supported encodings with an inclusive raw input byte quota. Zero uses DefaultMaxBytes; negative limits are invalid. Unsupported or overlong labels are rejected without reading input. Errors expose categories only; wrapped reader causes are trusted diagnostics.

func Decode

func Decode(payload []byte, target any, options DecodeOptions) error

Decode parses one complete XML document into target.

func DecodeReader

func DecodeReader(reader io.Reader, target any, options DecodeOptions) error

DecodeReader buffers borrowed reader input, checks one root/depth and optional ExpectedRoot, then independently decodes into a non-nil pointer. It never closes the reader; there is no cancellation API. Target mutation can precede failure. Custom charset/target callbacks must be bounded and input-stable across passes.

func Encode

func Encode(value any, options EncodeOptions) ([]byte, error)

Encode serializes value using encoding/xml's deterministic struct traversal.

func EncodeWriter

func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error

EncodeWriter completes serialization before touching a borrowed writer, which it never closes. Encoding failure writes nothing; a later write error can leave a prefix visible and matches wire.ErrWrite. It is not atomic publication.

func Root

func Root(payload []byte, options DecodeOptions) (xml.Name, error)

Root scans a bounded XML token stream and requires exactly one root element within the depth policy. It returns the resolved namespace/local name, ignoring ExpectedRoot. It is not schema/DTD validation and does not reject non-element character data outside that root. Charset callbacks own their conversion work.

Types

type DecodeOptions

type DecodeOptions struct {
	// MaxBytes caps supplied input bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// MaxDepth caps nested input elements; zero selects DefaultMaxDepth; negative values fail.
	MaxDepth int
	// AllowNonStrict opts into encoding/xml recovery and can change the interpreted tree.
	AllowNonStrict bool
	// ExpectedRoot requires an exact namespace/local-name match in Decode; zero disables the
	// check and Root only returns the actual name.
	ExpectedRoot xml.Name
	// CharsetReader is a borrowed conversion callback, invoked independently by validation and
	// target decoding. Nil uses the bounded built-in allowlist; custom work/output admission
	// is caller-owned.
	CharsetReader func(string, io.Reader) (io.Reader, error)
}

DecodeOptions controls XML parsing and validation.

type EncodeOptions

type EncodeOptions struct {
	// MaxBytes caps returned encoded bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// Indent selects encoding/xml indentation, not canonical XML.
	Indent string
	// IncludeHeader prepends the XML declaration and counts it toward MaxBytes.
	// With a Charset, the declaration names that encoding.
	IncludeHeader bool
	// Charset selects the output encoding: UTF-8 when empty, or one of the
	// labels CharsetReader accepts (UTF-8, US-ASCII, ISO-8859-1 and
	// Windows-1252 with their aliases). A character the encoding cannot
	// represent fails as wire.ErrValidation; nothing is replaced or escaped.
	Charset string
}

EncodeOptions controls XML serialization.

type Nillable added in v3.2.0

type Nillable[T any] struct {
	// Present reports that the element appeared.
	Present bool
	// Null reports an explicit xsi:nil="true" or "1".
	Null bool
	// Value is the decoded content when the element is present and not nil.
	Value T
}

Nillable keeps the three states an optional, nillable XML Schema element can have: absent (Present false), explicitly nil (Present and Null true) and a value. Decoding honors xsi:nil only in SchemaInstanceNamespace; a nil element must have no child elements and no text other than XML whitespace. A non-nil element decodes through encoding/xml, except that empty content for a numeric or boolean T, also behind pointers, is rejected instead of becoming zero or false, so a blank value cannot pass as a real one. String types keep empty content, and a T that has its own XML or text unmarshaler (on T or *T) decides what empty content means. Encoding writes nothing when Present is false, writes an empty element whose nil attribute is in SchemaInstanceNamespace (encoding/xml chooses the prefix) when Null is true, and otherwise encodes Value with the element's name. A present, non-nil value whose Value is a nil pointer or interface is an encoding error, because encoding/xml would write nothing and turn the present value into an absent one.

func (Nillable[T]) MarshalXML added in v3.2.0

func (value Nillable[T]) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error

MarshalXML encodes the nillable value, or nothing when it is absent.

func (*Nillable[T]) UnmarshalXML added in v3.2.0

func (value *Nillable[T]) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error

UnmarshalXML decodes one element into the nillable value.

Jump to

Keyboard shortcuts

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