decoder

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package decoder 把不同编码(yaml/json/...)的字节流解码为统一的 map[string]any 中间表示。该中间表示只在 reload 流水线内部存在, 不会出现在公开 API 上。

解码器通过进程内 registry 注册(见 registry.go)。内置 yaml/yml/json 在 init 时注册;外部 codec(toml/hcl/json5/...)可通过 fastconf.RegisterCodec 在仓库外注入。

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownCodec = errors.New("decoder: unknown codec")

ErrUnknownCodec 表示不识别的 codec 名。

Functions

func CodecFromExt

func CodecFromExt(ext string) string

CodecFromExt infers a codec name from a file extension (with or without the leading dot). Returns empty string when the extension is unrecognised. Queries only codecs registered via RegisterCodec/RegisterCodecExt.

func DecodeAny

func DecodeAny(codec string, data []byte) (any, error)

DecodeAny decodes data as either an object or an array (used for RFC 6902 patch layers whose top-level node is an array).

Returns either map[string]any or []any. Returns nil on empty input.

func Lookup

func Lookup(name string) (contracts.Codec, bool)

Lookup returns the codec for name (case-insensitive) along with an "exists" flag. Used by tests to assert registration ordering.

func LookupExt

func LookupExt(ext string) string

LookupExt returns the codec name for a given extension, or "" if the extension is unknown.

func Register

func Register(name string, c contracts.Codec)

Register installs c under the given name (case-insensitive). It is safe for concurrent use. Registering nil panics; that signals a bug at init time and we prefer to fail loudly rather than silently dropping the codec on first use. Re-registering an existing name overwrites it, which makes test helpers and feature toggles ergonomic.

func RegisterExt

func RegisterExt(ext, codec string)

RegisterExt maps a file extension (with or without leading dot, case insensitive) to a codec name. It does NOT register the codec itself — the caller should also call Register if the codec is custom.

Types

type Decoder

type Decoder = contracts.Codec

Decoder 把字节流解码为通用 map。

Decoder 等价于公共契约 contracts.Codec — 内部包以别名形式持有, 任何实现 contracts.Codec 的类型都可直接喂进 internal/decoder 的注册表。

func For

func For(codec string) (Decoder, error)

For 按 codec 名返回 Decoder。codec 不区分大小写。 不在注册表中时返回 ErrUnknownCodec。

Jump to

Keyboard shortcuts

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