protobuf-es icon indicating copy to clipboard operation
protobuf-es copied to clipboard

Feature: Path and FieldMask operations

Open Zetanova opened this issue 7 months ago • 3 comments

The utility class Path and PathBuilder from https://github.com/bufbuild/protobuf-es/pull/1092 looks very useful. But some helper functions for intersect and exclude some paths is missing.

The merge, mergeFromBinary, toBinary could accept the Path and/or FieldMask to include only the selected fields in the operation.

Example intersect:

const allAllowedPath = parsePath('active,locations')
const wantedPath = parsePath('first_name,locations[0]')

const applyPath = intersectPaths(allAllowedPath,  wantedPath)

Example exclude:

const excludedPath = parsePath('first_name,last_name')
const wantedPaths = parsePath('first_name,locations[0]')

const applyPath = excludePath(wantedPath,  excludedPath)

Zetanova avatar Jul 18 '25 20:07 Zetanova

This is currently the simplest conversion from Path to FieldMask

    const path:Path
    const updateMask = create(FieldMaskSchema)

    for(const m of path) {
      updateMask.paths.push(pathToString([m]))
    }

Zetanova avatar Jul 20 '25 08:07 Zetanova

FieldMasks have a few additional restrictions. Namely:

A repeated field is not allowed except at the last position of a paths string.

Meaning you cannot specify indices of repeated fields in the paths, or anything after a repeated field.

jcready avatar Jul 20 '25 10:07 jcready

I only use path to fieldmask for now to get some validation. But still some complient functions to Merge messages and/or validate the update mask would be very helpful.

Zetanova avatar Jul 20 '25 17:07 Zetanova