fieldmaskutil

package
v1.1.34 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 6 Imported by: 0

README

Protobuf FieldMask Utility

Filter

保留 msg 中 paths 列表指定的字段,清除所有其他字段。

// 假设有消息: {Name: "Alice", Age: 30, Email: "alice@example.com"}
Filter(msg, []string{"name", "email"})
// 结果: {Name: "Alice", Age: 0, Email: "alice@example.com"}
// Age 字段被清除

Prune

清除 msg 中 paths 列表指定的字段,保留所有其他字段。

// 假设有消息: {Name: "Alice", Age: 30, Email: "alice@example.com"}
Prune(msg, []string{"age", "email"})
// 结果: {Name: "Alice", Age: 0, Email: ""}
// Age 和 Email 字段被清除,Name 保留

Overwrite

使用 src 消息中的值覆盖 dest 消息中 paths 列表指定的字段。

// src: {Name: "Alice", Age: 30, Email: "alice@example.com"}
// dest: {Name: "Bob", Age: 25, Email: "bob@example.com"}
Overwrite(src, dest, []string{"name", "age"})
// 结果 dest: {Name: "Alice", Age: 30, Email: "bob@example.com"}
// Name 和 Age 被 src 的值覆盖,Email 保持不变

Validate

检查所有 paths 对于指定的 validationModel 消息是否有效。

// 验证字段路径是否存在
err := Validate(userMsg, []string{"name", "profile.bio"})
// 如果 userMsg 没有 profile.bio 字段,返回错误
// 如果所有字段都存在,返回 nil

PathsFromFieldNumbers

从给定的字段编号列表中,返回对应的字段路径列表。

// 假设消息定义: message User {
//   string name = 1;
//   int32 age = 2;
//   string email = 3;
// }
paths := PathsFromFieldNumbers(userMsg, []int{1, 3})
// 返回: ["name", "email"]

NilValuePaths

从给定的字段路径列表中,返回那些在消息中不存在或未设置(nil)的字段路径。

// 假设消息: {Name: "Alice", Email: "alice@example.com"}
// Age 字段未设置(为零值)
nilPaths := NilValuePaths(msg, []string{"name", "age", "email"})
// 返回: ["age"]
// 因为只有 age 字段未设置

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter(msg proto.Message, paths []string)

Filter keeps the msg fields that are listed in the paths and clears all the rest.

This is a handy wrapper for NestedMask.Filter method. If the same paths are used to process multiple proto messages use NestedMask.Filter method directly.

func FilterByFieldMask

func FilterByFieldMask(msg *proto.Message, fm *fieldmaskpb.FieldMask) error

FilterByFieldMask keeps the msg fields that are listed in the given FieldMask and clears all the rest.

func NilValuePaths

func NilValuePaths(msg proto.Message, paths []string) []string

func NormalizeFieldMaskPaths

func NormalizeFieldMaskPaths(fm *fieldmaskpb.FieldMask)

NormalizeFieldMaskPaths normalizes the paths in the given FieldMask to snake_case

func NormalizePaths

func NormalizePaths(paths []string) []string

func Overwrite

func Overwrite(src, dest proto.Message, paths []string)

Overwrite overwrites all the fields listed in paths in the dest msg using values from src msg.

This is a handy wrapper for NestedMask.Overwrite method. If the same paths are used to process multiple proto messages use NestedMask.Overwrite method directly.

func OverwriteByFieldMask

func OverwriteByFieldMask(msg *proto.Message, fm *fieldmaskpb.FieldMask) error

OverwriteByFieldMask overwrites all the fields listed in the given FieldMask in the dest msg using values from src msg.

func PathsFromFieldNumbers

func PathsFromFieldNumbers(msg proto.Message, fieldNumbers ...int) []string

PathsFromFieldNumbers converts protobuf field numbers to field paths for the given message.

This function takes a protobuf message and a list of field numbers, and returns a slice of field paths (field names) corresponding to those numbers.

Field numbers that don't exist in the message descriptor are skipped.

If no field numbers are provided, returns nil.

Example:

// For a message with fields: name (field 1), age (field 2), address (field 3)
paths := PathsFromFieldNumbers(msg, 1, 2)
// Returns: ["name", "age"]

func Prune

func Prune(msg proto.Message, paths []string)

Prune clears all the fields listed in paths from the given msg.

This is a handy wrapper for NestedMask.Prune method. If the same paths are used to process multiple proto messages use NestedMask.Filter method directly.

func PruneByFieldMask

func PruneByFieldMask(msg *proto.Message, fm *fieldmaskpb.FieldMask) error

PruneByFieldMask clears all the fields listed in the given FieldMask from the msg.

func Validate

func Validate(validationModel proto.Message, paths []string) error

Validate checks if all paths are valid for specified message

This is a handy wrapper for NestedMask.Validate method. If the same paths are used to process multiple proto messages use NestedMask.Validate method directly.

func ValidateFieldMask

func ValidateFieldMask(msg proto.Message, fm *fieldmaskpb.FieldMask) error

ValidateFieldMask checks if all paths in the given FieldMask are valid for the specified message.

Types

type NestedMask

type NestedMask map[string]NestedMask

NestedMask represents a field mask as a recursive map.

func NestedMaskFromPaths

func NestedMaskFromPaths(paths []string) NestedMask

NestedMaskFromPaths creates an instance of NestedMask for the given paths.

For example ["foo.bar", "foo.baz"] becomes {"foo": {"bar": nil, "baz": nil}}.

func (NestedMask) Filter

func (mask NestedMask) Filter(msg proto.Message)

Filter keeps the msg fields that are listed in the paths and clears all the rest.

If the mask is empty then all the fields are kept. Paths are assumed to be valid and normalized otherwise the function may panic. See google.golang.org/protobuf/types/known/fieldmaskpb for details.

func (NestedMask) Overwrite

func (mask NestedMask) Overwrite(src, dest proto.Message)

Overwrite overwrites all the fields listed in paths in the dest msg using values from src msg.

All other fields are kept untouched. If the mask is empty, no fields are overwritten. Supports scalars, messages, repeated fields, and maps. If the parent of the field is nil message, the parent is initiated before overwriting the field If the field in src is empty value, the field in dest is cleared. Paths are assumed to be valid and normalized otherwise the function may panic.

func (NestedMask) Prune

func (mask NestedMask) Prune(msg proto.Message)

Prune clears all the fields listed in paths from the given msg.

All other fields are kept untouched. If the mask is empty no fields are cleared. This operation is the opposite of NestedMask.Filter. Paths are assumed to be valid and normalized otherwise the function may panic. See google.golang.org/protobuf/types/known/fieldmaskpb for details.

func (NestedMask) Validate

func (mask NestedMask) Validate(validationModel proto.Message) error

Validate checks if all paths are valid for specified message.

Supports scalars, messages, repeated fields, and maps.

Jump to

Keyboard shortcuts

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