vendor: update buildkit to master@31c870e82a48

Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
Justin Chadwell
2023-05-15 18:32:31 +01:00
parent 167cd16acb
commit e61a8cf637
269 changed files with 25798 additions and 3371 deletions

View File

@ -0,0 +1,122 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package protopath provides functionality for
// representing a sequence of protobuf reflection operations on a message.
package protopath
import (
"fmt"
"google.golang.org/protobuf/internal/msgfmt"
"google.golang.org/protobuf/reflect/protoreflect"
)
// NOTE: The Path and Values are separate types here since there are use cases
// where you would like to "address" some value in a message with just the path
// and don't have the value information available.
//
// This is different from how "github.com/google/go-cmp/cmp".Path operates,
// which combines both path and value information together.
// Since the cmp package itself is the only one ever constructing a cmp.Path,
// it will always have the value available.
// Path is a sequence of protobuf reflection steps applied to some root
// protobuf message value to arrive at the current value.
// The first step must be a Root step.
type Path []Step
// TODO: Provide a Parse function that parses something similar to or
// perhaps identical to the output of Path.String.
// Index returns the ith step in the path and supports negative indexing.
// A negative index starts counting from the tail of the Path such that -1
// refers to the last step, -2 refers to the second-to-last step, and so on.
// It returns a zero Step value if the index is out-of-bounds.
func (p Path) Index(i int) Step {
if i < 0 {
i = len(p) + i
}
if i < 0 || i >= len(p) {
return Step{}
}
return p[i]
}
// String returns a structured representation of the path
// by concatenating the string representation of every path step.
func (p Path) String() string {
var b []byte
for _, s := range p {
b = s.appendString(b)
}
return string(b)
}
// Values is a Path paired with a sequence of values at each step.
// The lengths of Path and Values must be identical.
// The first step must be a Root step and
// the first value must be a concrete message value.
type Values struct {
Path Path
Values []protoreflect.Value
}
// Len reports the length of the path and values.
// If the path and values have differing length, it returns the minimum length.
func (p Values) Len() int {
n := len(p.Path)
if n > len(p.Values) {
n = len(p.Values)
}
return n
}
// Index returns the ith step and value and supports negative indexing.
// A negative index starts counting from the tail of the Values such that -1
// refers to the last pair, -2 refers to the second-to-last pair, and so on.
func (p Values) Index(i int) (out struct {
Step Step
Value protoreflect.Value
}) {
// NOTE: This returns a single struct instead of two return values so that
// callers can make use of the the value in an expression:
// vs.Index(i).Value.Interface()
n := p.Len()
if i < 0 {
i = n + i
}
if i < 0 || i >= n {
return out
}
out.Step = p.Path[i]
out.Value = p.Values[i]
return out
}
// String returns a humanly readable representation of the path and last value.
// Do not depend on the output being stable.
//
// For example:
//
// (path.to.MyMessage).list_field[5].map_field["hello"] = {hello: "world"}
func (p Values) String() string {
n := p.Len()
if n == 0 {
return ""
}
// Determine the field descriptor associated with the last step.
var fd protoreflect.FieldDescriptor
last := p.Index(-1)
switch last.Step.kind {
case FieldAccessStep:
fd = last.Step.FieldDescriptor()
case MapIndexStep, ListIndexStep:
fd = p.Index(-2).Step.FieldDescriptor()
}
// Format the full path with the last value.
return fmt.Sprintf("%v = %v", p.Path[:n], msgfmt.FormatValue(last.Value, fd))
}

View File

@ -0,0 +1,241 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protopath
import (
"fmt"
"strconv"
"strings"
"google.golang.org/protobuf/internal/encoding/text"
"google.golang.org/protobuf/reflect/protoreflect"
)
// StepKind identifies the kind of step operation.
// Each kind of step corresponds with some protobuf reflection operation.
type StepKind int
const (
invalidStep StepKind = iota
// RootStep identifies a step as the Root step operation.
RootStep
// FieldAccessStep identifies a step as the FieldAccess step operation.
FieldAccessStep
// UnknownAccessStep identifies a step as the UnknownAccess step operation.
UnknownAccessStep
// ListIndexStep identifies a step as the ListIndex step operation.
ListIndexStep
// MapIndexStep identifies a step as the MapIndex step operation.
MapIndexStep
// AnyExpandStep identifies a step as the AnyExpand step operation.
AnyExpandStep
)
func (k StepKind) String() string {
switch k {
case invalidStep:
return "<invalid>"
case RootStep:
return "Root"
case FieldAccessStep:
return "FieldAccess"
case UnknownAccessStep:
return "UnknownAccess"
case ListIndexStep:
return "ListIndex"
case MapIndexStep:
return "MapIndex"
case AnyExpandStep:
return "AnyExpand"
default:
return fmt.Sprintf("<unknown:%d>", k)
}
}
// Step is a union where only one step operation may be specified at a time.
// The different kinds of steps are specified by the constants defined for
// the StepKind type.
type Step struct {
kind StepKind
desc protoreflect.Descriptor
key protoreflect.Value
}
// Root indicates the root message that a path is relative to.
// It should always (and only ever) be the first step in a path.
func Root(md protoreflect.MessageDescriptor) Step {
if md == nil {
panic("nil message descriptor")
}
return Step{kind: RootStep, desc: md}
}
// FieldAccess describes access of a field within a message.
// Extension field accesses are also represented using a FieldAccess and
// must be provided with a protoreflect.FieldDescriptor
//
// Within the context of Values,
// the type of the previous step value is always a message, and
// the type of the current step value is determined by the field descriptor.
func FieldAccess(fd protoreflect.FieldDescriptor) Step {
if fd == nil {
panic("nil field descriptor")
} else if _, ok := fd.(protoreflect.ExtensionTypeDescriptor); !ok && fd.IsExtension() {
panic(fmt.Sprintf("extension field %q must implement protoreflect.ExtensionTypeDescriptor", fd.FullName()))
}
return Step{kind: FieldAccessStep, desc: fd}
}
// UnknownAccess describes access to the unknown fields within a message.
//
// Within the context of Values,
// the type of the previous step value is always a message, and
// the type of the current step value is always a bytes type.
func UnknownAccess() Step {
return Step{kind: UnknownAccessStep}
}
// ListIndex describes index of an element within a list.
//
// Within the context of Values,
// the type of the previous, previous step value is always a message,
// the type of the previous step value is always a list, and
// the type of the current step value is determined by the field descriptor.
func ListIndex(i int) Step {
if i < 0 {
panic(fmt.Sprintf("invalid list index: %v", i))
}
return Step{kind: ListIndexStep, key: protoreflect.ValueOfInt64(int64(i))}
}
// MapIndex describes index of an entry within a map.
// The key type is determined by field descriptor that the map belongs to.
//
// Within the context of Values,
// the type of the previous previous step value is always a message,
// the type of the previous step value is always a map, and
// the type of the current step value is determined by the field descriptor.
func MapIndex(k protoreflect.MapKey) Step {
if !k.IsValid() {
panic("invalid map index")
}
return Step{kind: MapIndexStep, key: k.Value()}
}
// AnyExpand describes expansion of a google.protobuf.Any message into
// a structured representation of the underlying message.
//
// Within the context of Values,
// the type of the previous step value is always a google.protobuf.Any message, and
// the type of the current step value is always a message.
func AnyExpand(md protoreflect.MessageDescriptor) Step {
if md == nil {
panic("nil message descriptor")
}
return Step{kind: AnyExpandStep, desc: md}
}
// MessageDescriptor returns the message descriptor for Root or AnyExpand steps,
// otherwise it returns nil.
func (s Step) MessageDescriptor() protoreflect.MessageDescriptor {
switch s.kind {
case RootStep, AnyExpandStep:
return s.desc.(protoreflect.MessageDescriptor)
default:
return nil
}
}
// FieldDescriptor returns the field descriptor for FieldAccess steps,
// otherwise it returns nil.
func (s Step) FieldDescriptor() protoreflect.FieldDescriptor {
switch s.kind {
case FieldAccessStep:
return s.desc.(protoreflect.FieldDescriptor)
default:
return nil
}
}
// ListIndex returns the list index for ListIndex steps,
// otherwise it returns 0.
func (s Step) ListIndex() int {
switch s.kind {
case ListIndexStep:
return int(s.key.Int())
default:
return 0
}
}
// MapIndex returns the map key for MapIndex steps,
// otherwise it returns an invalid map key.
func (s Step) MapIndex() protoreflect.MapKey {
switch s.kind {
case MapIndexStep:
return s.key.MapKey()
default:
return protoreflect.MapKey{}
}
}
// Kind reports which kind of step this is.
func (s Step) Kind() StepKind {
return s.kind
}
func (s Step) String() string {
return string(s.appendString(nil))
}
func (s Step) appendString(b []byte) []byte {
switch s.kind {
case RootStep:
b = append(b, '(')
b = append(b, s.desc.FullName()...)
b = append(b, ')')
case FieldAccessStep:
b = append(b, '.')
if fd := s.desc.(protoreflect.FieldDescriptor); fd.IsExtension() {
b = append(b, '(')
b = append(b, strings.Trim(fd.TextName(), "[]")...)
b = append(b, ')')
} else {
b = append(b, fd.TextName()...)
}
case UnknownAccessStep:
b = append(b, '.')
b = append(b, '?')
case ListIndexStep:
b = append(b, '[')
b = strconv.AppendInt(b, s.key.Int(), 10)
b = append(b, ']')
case MapIndexStep:
b = append(b, '[')
switch k := s.key.Interface().(type) {
case bool:
b = strconv.AppendBool(b, bool(k)) // e.g., "true" or "false"
case int32:
b = strconv.AppendInt(b, int64(k), 10) // e.g., "-32"
case int64:
b = strconv.AppendInt(b, int64(k), 10) // e.g., "-64"
case uint32:
b = strconv.AppendUint(b, uint64(k), 10) // e.g., "32"
case uint64:
b = strconv.AppendUint(b, uint64(k), 10) // e.g., "64"
case string:
b = text.AppendString(b, k) // e.g., `"hello, world"`
}
b = append(b, ']')
case AnyExpandStep:
b = append(b, '.')
b = append(b, '(')
b = append(b, s.desc.FullName()...)
b = append(b, ')')
default:
b = append(b, "<invalid>"...)
}
return b
}