fieldpath

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTopLevelField

func ExtractTopLevelField(fieldPath string) string

ExtractTopLevelField 从嵌套字段路径中提取顶级字段名 例如:"device.info.name" 返回 "device"

"data[0].name" 返回 "data"
"a.b[0].c['key']" 返回 "a"

func GetAllReferencedFields

func GetAllReferencedFields(fieldPaths []string) []string

GetAllReferencedFields 获取嵌套字段路径中引用的所有顶级字段 例如:["device.info.name", "sensor.temperature", "data[0].value"] 返回 ["device", "sensor", "data"]

func GetFieldPathDepth

func GetFieldPathDepth(fieldPath string) int

GetFieldPathDepth 获取字段路径的深度

func GetNestedField

func GetNestedField(data interface{}, fieldPath string) (interface{}, bool)

GetNestedField 从嵌套的map或结构体中获取字段值 支持点号分隔的字段路径、数组索引、Map键等复杂操作 支持的格式: - "device.info.name" (嵌套字段) - "data[0]" (数组索引) - "users[0].name" (数组元素的字段) - "config['key']" (字符串键) - "items[0][1]" (多维数组) - "nested.data[0].field['key']" (混合访问)

func IsNestedField

func IsNestedField(fieldName string) bool

IsNestedField 检查字段名是否包含点号或数组索引(嵌套字段)

func NormalizeFieldPath

func NormalizeFieldPath(fieldPath string) string

NormalizeFieldPath 规范化字段路径格式

func SetNestedField

func SetNestedField(data map[string]interface{}, fieldPath string, value interface{}) error

SetNestedField 在嵌套的map中设置字段值,支持复杂路径 如果路径中的某些层级不存在,会自动创建

func ValidateFieldPath

func ValidateFieldPath(fieldPath string) error

ValidateFieldPath 验证字段路径的格式是否正确

Types

type FieldAccessError

type FieldAccessError struct {
	Path    string
	Message string
}

FieldAccessError 字段访问错误

func (*FieldAccessError) Error

func (e *FieldAccessError) Error() string

type FieldAccessor

type FieldAccessor struct {
	Parts []FieldPart
}

FieldAccessor 字段访问器结构,用于解析复杂的字段路径

func ParseFieldPath

func ParseFieldPath(fieldPath string) (*FieldAccessor, error)

ParseFieldPath 解析字段路径,支持点号、数组索引、Map键等复杂访问 支持的格式: - a.b.c (嵌套字段) - a.b[0] (数组索引) - a.b[0].c (数组元素的字段) - a.b["key"] (字符串键) - a.b['key'] (字符串键) - a.b[123] (数字键或数组索引) - a[0].b[1].c["key"] (混合访问)

type FieldPart

type FieldPart struct {
	Type    string // "field", "array_index", "map_key"
	Name    string // 字段名或键名
	Index   int    // 数组索引(当Type为"array_index"时)
	Key     string // Map键(当Type为"map_key"时)
	KeyType string // 键类型:"string", "number"
}

FieldPart 字段路径的单个部分

Jump to

Keyboard shortcuts

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