chore(parsers.xpath): Migrate away from deprecated protoreflect functions (#16608)

This commit is contained in:
Sven Rebhan 2025-03-11 17:04:57 +01:00 committed by GitHub
parent 9605623f57
commit 6539e6039b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 19 deletions

View File

@ -241,7 +241,6 @@ following works:
- github.com/jcmturner/rpc [Apache License 2.0](https://github.com/jcmturner/rpc/blob/master/LICENSE) - github.com/jcmturner/rpc [Apache License 2.0](https://github.com/jcmturner/rpc/blob/master/LICENSE)
- github.com/jedib0t/go-pretty [MIT License](https://github.com/jedib0t/go-pretty/blob/main/LICENSE) - github.com/jedib0t/go-pretty [MIT License](https://github.com/jedib0t/go-pretty/blob/main/LICENSE)
- github.com/jeremywohl/flatten [MIT License](https://github.com/jeremywohl/flatten/blob/master/LICENSE) - github.com/jeremywohl/flatten [MIT License](https://github.com/jeremywohl/flatten/blob/master/LICENSE)
- github.com/jhump/protoreflect [Apache License 2.0](https://github.com/jhump/protoreflect/blob/master/LICENSE)
- github.com/jmespath/go-jmespath [Apache License 2.0](https://github.com/jmespath/go-jmespath/blob/master/LICENSE) - github.com/jmespath/go-jmespath [Apache License 2.0](https://github.com/jmespath/go-jmespath/blob/master/LICENSE)
- github.com/jmhodges/clock [MIT License](https://github.com/jmhodges/clock/blob/main/LICENSE) - github.com/jmhodges/clock [MIT License](https://github.com/jmhodges/clock/blob/main/LICENSE)
- github.com/josharian/intern [MIT License](https://github.com/josharian/intern/blob/master/LICENSE.md) - github.com/josharian/intern [MIT License](https://github.com/josharian/intern/blob/master/LICENSE.md)

3
go.mod
View File

@ -129,7 +129,6 @@ require (
github.com/jackc/pgx/v4 v4.18.3 github.com/jackc/pgx/v4 v4.18.3
github.com/jedib0t/go-pretty/v6 v6.6.5 github.com/jedib0t/go-pretty/v6 v6.6.5
github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4 github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4
github.com/jhump/protoreflect v1.16.0
github.com/jmespath/go-jmespath v0.4.0 github.com/jmespath/go-jmespath v0.4.0
github.com/karrick/godirwalk v1.16.2 github.com/karrick/godirwalk v1.16.2
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
@ -307,7 +306,7 @@ require (
github.com/bitly/go-hostpool v0.1.0 // indirect github.com/bitly/go-hostpool v0.1.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/brutella/dnssd v1.2.14 // indirect github.com/brutella/dnssd v1.2.14 // indirect
github.com/bufbuild/protocompile v0.10.0 // indirect github.com/bufbuild/protocompile v0.14.1
github.com/caio/go-tdigest/v4 v4.0.1 // indirect github.com/caio/go-tdigest/v4 v4.0.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect

6
go.sum
View File

@ -976,8 +976,8 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/buengese/sgzip v0.1.1 h1:ry+T8l1mlmiWEsDrH/YHZnCVWD2S3im1KLsyO+8ZmTU= github.com/buengese/sgzip v0.1.1 h1:ry+T8l1mlmiWEsDrH/YHZnCVWD2S3im1KLsyO+8ZmTU=
github.com/buengese/sgzip v0.1.1/go.mod h1:i5ZiXGF3fhV7gL1xaRRL1nDnmpNj0X061FQzOS8VMas= github.com/buengese/sgzip v0.1.1/go.mod h1:i5ZiXGF3fhV7gL1xaRRL1nDnmpNj0X061FQzOS8VMas=
github.com/bufbuild/protocompile v0.10.0 h1:+jW/wnLMLxaCEG8AX9lD0bQ5v9h1RUiMKOBOT5ll9dM= github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw=
github.com/bufbuild/protocompile v0.10.0/go.mod h1:G9qQIQo0xZ6Uyj6CMNz0saGmx2so+KONo8/KrELABiY= github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c=
github.com/caio/go-tdigest v3.1.0+incompatible h1:uoVMJ3Q5lXmVLCCqaMGHLBWnbGoN6Lpu7OAUPR60cds= github.com/caio/go-tdigest v3.1.0+incompatible h1:uoVMJ3Q5lXmVLCCqaMGHLBWnbGoN6Lpu7OAUPR60cds=
github.com/caio/go-tdigest v3.1.0+incompatible/go.mod h1:sHQM/ubZStBUmF1WbB8FAm8q9GjDajLC5T7ydxE3JHI= github.com/caio/go-tdigest v3.1.0+incompatible/go.mod h1:sHQM/ubZStBUmF1WbB8FAm8q9GjDajLC5T7ydxE3JHI=
github.com/caio/go-tdigest/v4 v4.0.1 h1:sx4ZxjmIEcLROUPs2j1BGe2WhOtHD6VSe6NNbBdKYh4= github.com/caio/go-tdigest/v4 v4.0.1 h1:sx4ZxjmIEcLROUPs2j1BGe2WhOtHD6VSe6NNbBdKYh4=
@ -1666,8 +1666,6 @@ github.com/jeremija/gosubmit v0.2.8 h1:mmSITBz9JxVtu8eqbN+zmmwX7Ij2RidQxhcwRVI4w
github.com/jeremija/gosubmit v0.2.8/go.mod h1:Ui+HS073lCFREXBbdfrJzMB57OI/bdxTiLtrDHHhFPI= github.com/jeremija/gosubmit v0.2.8/go.mod h1:Ui+HS073lCFREXBbdfrJzMB57OI/bdxTiLtrDHHhFPI=
github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4 h1:eA9wi6ZzpIRobvXkn/S2Lyw1hr2pc71zxzOPl7Xjs4w= github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4 h1:eA9wi6ZzpIRobvXkn/S2Lyw1hr2pc71zxzOPl7Xjs4w=
github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4/go.mod h1:s9g9Dfls+aEgucKXKW+i8MRZuLXT2MrD/WjYpMnWfOw= github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4/go.mod h1:s9g9Dfls+aEgucKXKW+i8MRZuLXT2MrD/WjYpMnWfOw=
github.com/jhump/protoreflect v1.16.0 h1:54fZg+49widqXYQ0b+usAFHbMkBGR4PpXrsHc8+TBDg=
github.com/jhump/protoreflect v1.16.0/go.mod h1:oYPd7nPvcBw/5wlDfm/AVmU9zH9BgqGCI469pGxfj/8=
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg= github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI= github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=

View File

@ -1,6 +1,7 @@
package xpath package xpath
import ( import (
"context"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
@ -10,13 +11,12 @@ import (
"strings" "strings"
path "github.com/antchfx/xpath" path "github.com/antchfx/xpath"
"github.com/jhump/protoreflect/desc" "github.com/bufbuild/protocompile"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/srebhan/protobufquery" "github.com/srebhan/protobufquery"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/dynamicpb" "google.golang.org/protobuf/types/dynamicpb"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -43,26 +43,30 @@ func (d *protobufDocument) Init() error {
} }
// Load the file descriptors from the given protocol-buffer definition // Load the file descriptors from the given protocol-buffer definition
parser := protoparse.Parser{ ctx := context.Background()
ImportPaths: d.ImportPaths, resolver := &protocompile.SourceResolver{ImportPaths: d.ImportPaths}
InferImportPaths: true, compiler := &protocompile.Compiler{
Resolver: protocompile.WithStandardImports(resolver),
} }
fds, err := parser.ParseFiles(d.MessageFiles...) files, err := compiler.Compile(ctx, d.MessageFiles...)
if err != nil { if err != nil {
return fmt.Errorf("parsing protocol-buffer definition failed: %w", err) return fmt.Errorf("parsing protocol-buffer definition failed: %w", err)
} }
if len(fds) < 1 { if len(files) < 1 {
return errors.New("files do not contain a file descriptor") return errors.New("files do not contain a file descriptor")
} }
// Register all definitions in the file in the global registry // Register all definitions in the file in the global registry
registry, err := protodesc.NewFiles(desc.ToFileDescriptorSet(fds...)) var registry protoregistry.Files
if err != nil { for _, f := range files {
return fmt.Errorf("constructing registry failed: %w", err) if err := registry.RegisterFile(f); err != nil {
return fmt.Errorf("adding file %q to registry failed: %w", f.Path(), err)
}
} }
d.unmarshaller = proto.UnmarshalOptions{ d.unmarshaller = proto.UnmarshalOptions{
RecursionLimit: protowire.DefaultRecursionLimit, RecursionLimit: protowire.DefaultRecursionLimit,
Resolver: dynamicpb.NewTypes(registry), Resolver: dynamicpb.NewTypes(&registry),
} }
// Lookup given type in the loaded file descriptors // Lookup given type in the loaded file descriptors