vendor: update buildkit to v0.17.0-rc2

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2024-10-28 14:56:43 -07:00
parent 202c390fca
commit 6fcc6853d9
195 changed files with 3522 additions and 5281 deletions

View File

@ -41,6 +41,7 @@ var GenerateVersionMarkers = true
// Standard library dependencies.
const (
base64Package = protogen.GoImportPath("encoding/base64")
jsonPackage = protogen.GoImportPath("encoding/json")
mathPackage = protogen.GoImportPath("math")
reflectPackage = protogen.GoImportPath("reflect")
sortPackage = protogen.GoImportPath("sort")
@ -75,11 +76,14 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
g := gen.NewGeneratedFile(filename, file.GoImportPath)
f := newFileInfo(file)
genStandaloneComments(g, f, int32(genid.FileDescriptorProto_Syntax_field_number))
genGeneratedHeader(gen, g, f)
genStandaloneComments(g, f, int32(genid.FileDescriptorProto_Package_field_number))
var packageDoc protogen.Comments
if !gen.InternalStripForEditionsDiff() {
genStandaloneComments(g, f, int32(genid.FileDescriptorProto_Syntax_field_number))
genGeneratedHeader(gen, g, f)
genStandaloneComments(g, f, int32(genid.FileDescriptorProto_Package_field_number))
packageDoc := genPackageKnownComment(f)
packageDoc = genPackageKnownComment(f)
}
g.P(packageDoc, "package ", f.GoPackageName)
g.P()
@ -105,7 +109,23 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
}
genExtensions(g, f)
genReflectFileDescriptor(gen, g, f)
// The descriptor contains a lot of information about the syntax which is
// quite different between the proto2/3 version of a file and the equivalent
// editions version. For example, when a proto3 file is translated from
// proto3 to editions every field in that file that is marked optional in
// proto3 will have a features.field_presence option set.
// Another problem is that the descriptor contains implementation details
// that are not relevant for the semantic. For example, proto3 optional
// fields are implemented as oneof fields with one case. The descriptor
// contains different information about oneofs. If the file is translated
// to editions it no longer is treated as a oneof with one case and thus
// none of the oneof specific information is generated.
// To be able to compare the descriptor before and after translation of the
// associated proto file, we would need to trim many parts. This would lead
// to a brittle implementation in case the translation ever changes.
if !g.InternalStripForEditionsDiff() {
genReflectFileDescriptor(gen, g, f)
}
return g
}
@ -517,12 +537,10 @@ func genMessageBaseMethods(g *protogen.GeneratedFile, f *fileInfo, m *messageInf
// Reset method.
g.P("func (x *", m.GoIdent, ") Reset() {")
g.P("*x = ", m.GoIdent, "{}")
g.P("if ", protoimplPackage.Ident("UnsafeEnabled"), " {")
g.P("mi := &", messageTypesVarName(f), "[", f.allMessagesByPtr[m], "]")
g.P("ms := ", protoimplPackage.Ident("X"), ".MessageStateOf(", protoimplPackage.Ident("Pointer"), "(x))")
g.P("ms.StoreMessageInfo(mi)")
g.P("}")
g.P("}")
g.P()
// String method.

View File

@ -132,7 +132,7 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f
panic("too many dependencies") // sanity check
}
g.P("var ", goTypesVarName(f), " = []interface{}{")
g.P("var ", goTypesVarName(f), " = []any{")
for _, s := range goTypes {
g.P(s)
}
@ -163,27 +163,6 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f
}
if len(f.allMessages) > 0 {
// Populate MessageInfo.Exporters.
g.P("if !", protoimplPackage.Ident("UnsafeEnabled"), " {")
for _, message := range f.allMessages {
if sf := f.allMessageFieldsByPtr[message]; len(sf.unexported) > 0 {
idx := f.allMessagesByPtr[message]
typesVar := messageTypesVarName(f)
g.P(typesVar, "[", idx, "].Exporter = func(v interface{}, i int) interface{} {")
g.P("switch v := v.(*", message.GoIdent, "); i {")
for i := 0; i < sf.count; i++ {
if name := sf.unexported[i]; name != "" {
g.P("case ", i, ": return &v.", name)
}
}
g.P("default: return nil")
g.P("}")
g.P("}")
}
}
g.P("}")
// Populate MessageInfo.OneofWrappers.
for _, message := range f.allMessages {
if len(message.Oneofs) > 0 {
@ -191,7 +170,7 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f
typesVar := messageTypesVarName(f)
// Associate the wrapper types by directly passing them to the MessageInfo.
g.P(typesVar, "[", idx, "].OneofWrappers = []interface{} {")
g.P(typesVar, "[", idx, "].OneofWrappers = []any {")
for _, oneof := range message.Oneofs {
if !oneof.Desc.IsSynthetic() {
for _, field := range oneof.Fields {
@ -331,7 +310,7 @@ func genMessageReflectMethods(g *protogen.GeneratedFile, f *fileInfo, m *message
// ProtoReflect method.
g.P("func (x *", m.GoIdent, ") ProtoReflect() ", protoreflectPackage.Ident("Message"), " {")
g.P("mi := &", typesVar, "[", idx, "]")
g.P("if ", protoimplPackage.Ident("UnsafeEnabled"), " && x != nil {")
g.P("if x != nil {")
g.P("ms := ", protoimplPackage.Ident("X"), ".MessageStateOf(", protoimplPackage.Ident("Pointer"), "(x))")
g.P("if ms.LoadMessageInfo() == nil {")
g.P("ms.StoreMessageInfo(mi)")

View File

@ -213,11 +213,11 @@ func genPackageKnownComment(f *fileInfo) protogen.Comments {
The standard Go "encoding/json" package has functionality to serialize
arbitrary types to a large degree. The Value.AsInterface, Struct.AsMap, and
ListValue.AsSlice methods can convert the protobuf message representation into
a form represented by interface{}, map[string]interface{}, and []interface{}.
a form represented by any, map[string]any, and []any.
This form can be used with other packages that operate on such data structures
and also directly with the standard json package.
In order to convert the interface{}, map[string]interface{}, and []interface{}
In order to convert the any, map[string]any, and []any
forms back as Value, Struct, and ListValue messages, use the NewStruct,
NewList, and NewValue constructor functions.
@ -252,28 +252,28 @@ func genPackageKnownComment(f *fileInfo) protogen.Comments {
To construct a Value message representing the above JSON object:
m, err := structpb.NewValue(map[string]interface{}{
m, err := structpb.NewValue(map[string]any{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": map[string]interface{}{
"address": map[string]any{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100",
},
"phoneNumbers": []interface{}{
map[string]interface{}{
"phoneNumbers": []any{
map[string]any{
"type": "home",
"number": "212 555-1234",
},
map[string]interface{}{
map[string]any{
"type": "office",
"number": "646 555-4567",
},
},
"children": []interface{}{},
"children": []any{},
"spouse": nil,
})
if err != nil {
@ -634,7 +634,7 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("// NewStruct constructs a Struct from a general-purpose Go map.")
g.P("// The map keys must be valid UTF-8.")
g.P("// The map values are converted using NewValue.")
g.P("func NewStruct(v map[string]interface{}) (*Struct, error) {")
g.P("func NewStruct(v map[string]any) (*Struct, error) {")
g.P(" x := &Struct{Fields: make(map[string]*Value, len(v))}")
g.P(" for k, v := range v {")
g.P(" if !", utf8Package.Ident("ValidString"), "(k) {")
@ -652,9 +652,9 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("// AsMap converts x to a general-purpose Go map.")
g.P("// The map values are converted by calling Value.AsInterface.")
g.P("func (x *Struct) AsMap() map[string]interface{} {")
g.P("func (x *Struct) AsMap() map[string]any {")
g.P(" f := x.GetFields()")
g.P(" vs := make(map[string]interface{}, len(f))")
g.P(" vs := make(map[string]any, len(f))")
g.P(" for k, v := range f {")
g.P(" vs[k] = v.AsInterface()")
g.P(" }")
@ -675,7 +675,7 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
case genid.ListValue_message_fullname:
g.P("// NewList constructs a ListValue from a general-purpose Go slice.")
g.P("// The slice elements are converted using NewValue.")
g.P("func NewList(v []interface{}) (*ListValue, error) {")
g.P("func NewList(v []any) (*ListValue, error) {")
g.P(" x := &ListValue{Values: make([]*Value, len(v))}")
g.P(" for i, v := range v {")
g.P(" var err error")
@ -690,9 +690,9 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("// AsSlice converts x to a general-purpose Go slice.")
g.P("// The slice elements are converted by calling Value.AsInterface.")
g.P("func (x *ListValue) AsSlice() []interface{} {")
g.P("func (x *ListValue) AsSlice() []any {")
g.P(" vals := x.GetValues()")
g.P(" vs := make([]interface{}, len(vals))")
g.P(" vs := make([]any, len(vals))")
g.P(" for i, v := range vals {")
g.P(" vs[i] = v.AsInterface()")
g.P(" }")
@ -713,23 +713,24 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
case genid.Value_message_fullname:
g.P("// NewValue constructs a Value from a general-purpose Go interface.")
g.P("//")
g.P("// ╔════════════════════════╤════════════════════════════════════════════╗")
g.P("// ║ Go type │ Conversion ║")
g.P("// ╠════════════════════════╪════════════════════════════════════════════╣")
g.P("// ║ nil │ stored as NullValue ║")
g.P("// ║ bool │ stored as BoolValue ║")
g.P("// ║ int, int32, int64 │ stored as NumberValue ║")
g.P("// ║ uint, uint32, uint64 │ stored as NumberValue ║")
g.P("// ║ float32, float64 │ stored as NumberValue ║")
g.P("// ║ string │ stored as StringValue; must be valid UTF-8 ║")
g.P("// ║ []byte │ stored as StringValue; base64-encoded ║")
g.P("// ║ map[string]interface{} │ stored as StructValue ║")
g.P("// ║ []interface{} │ stored as ListValue ║")
g.P("// ╚════════════════════════╧════════════════════════════════════════════╝")
g.P("// ╔═══════════════════════════════════════╤════════════════════════════════════════════╗")
g.P("// ║ Go type │ Conversion ║")
g.P("// ╠═══════════════════════════════════════╪════════════════════════════════════════════╣")
g.P("// ║ nil │ stored as NullValue ║")
g.P("// ║ bool │ stored as BoolValue ║")
g.P("// ║ int, int8, int16, int32, int64 │ stored as NumberValue ║")
g.P("// ║ uint, uint8, uint16, uint32, uint64 │ stored as NumberValue ║")
g.P("// ║ float32, float64 │ stored as NumberValue ║")
g.P("// ║ json.Number │ stored as NumberValue ║")
g.P("// ║ string │ stored as StringValue; must be valid UTF-8 ║")
g.P("// ║ []byte │ stored as StringValue; base64-encoded ║")
g.P("// ║ map[string]any │ stored as StructValue ║")
g.P("// ║ []any │ stored as ListValue ║")
g.P("// ╚═══════════════════════════════════════╧════════════════════════════════════════════╝")
g.P("//")
g.P("// When converting an int64 or uint64 to a NumberValue, numeric precision loss")
g.P("// is possible since they are stored as a float64.")
g.P("func NewValue(v interface{}) (*Value, error) {")
g.P("func NewValue(v any) (*Value, error) {")
g.P(" switch v := v.(type) {")
g.P(" case nil:")
g.P(" return NewNullValue(), nil")
@ -737,12 +738,20 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P(" return NewBoolValue(v), nil")
g.P(" case int:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case int8:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case int16:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case int32:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case int64:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case uint:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case uint8:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case uint16:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case uint32:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case uint64:")
@ -751,6 +760,12 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case float64:")
g.P(" return NewNumberValue(float64(v)), nil")
g.P(" case ", jsonPackage.Ident("Number"), ":")
g.P(" n, err := v.Float64()")
g.P(" if err != nil {")
g.P(" return nil, ", protoimplPackage.Ident("X"), ".NewError(\"invalid number format %q, expected a float64: %v\", v, err)")
g.P(" }")
g.P(" return NewNumberValue(n), nil")
g.P(" case string:")
g.P(" if !", utf8Package.Ident("ValidString"), "(v) {")
g.P(" return nil, ", protoimplPackage.Ident("X"), ".NewError(\"invalid UTF-8 in string: %q\", v)")
@ -759,13 +774,13 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P(" case []byte:")
g.P(" s := ", base64Package.Ident("StdEncoding"), ".EncodeToString(v)")
g.P(" return NewStringValue(s), nil")
g.P(" case map[string]interface{}:")
g.P(" case map[string]any:")
g.P(" v2, err := NewStruct(v)")
g.P(" if err != nil {")
g.P(" return nil, err")
g.P(" }")
g.P(" return NewStructValue(v2), nil")
g.P(" case []interface{}:")
g.P(" case []any:")
g.P(" v2, err := NewList(v)")
g.P(" if err != nil {")
g.P(" return nil, err")
@ -820,7 +835,7 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("//")
g.P("// Floating-point values (i.e., \"NaN\", \"Infinity\", and \"-Infinity\") are")
g.P("// converted as strings to remain compatible with MarshalJSON.")
g.P("func (x *Value) AsInterface() interface{} {")
g.P("func (x *Value) AsInterface() any {")
g.P(" switch v := x.GetKind().(type) {")
g.P(" case *Value_NumberValue:")
g.P(" if v != nil {")

View File

@ -35,11 +35,13 @@ func main() {
}
var (
flags flag.FlagSet
plugins = flags.String("plugins", "", "deprecated option")
flags flag.FlagSet
plugins = flags.String("plugins", "", "deprecated option")
experimentalStripNonFunctionalCodegen = flags.Bool("experimental_strip_nonfunctional_codegen", false, "experimental_strip_nonfunctional_codegen true means that the plugin will not emit certain parts of the generated code in order to make it possible to compare a proto2/proto3 file with its equivalent (according to proto spec) editions file. Primarily, this is the encoded descriptor.")
)
protogen.Options{
ParamFunc: flags.Set,
ParamFunc: flags.Set,
InternalStripForEditionsDiff: experimentalStripNonFunctionalCodegen,
}.Run(func(gen *protogen.Plugin) error {
if *plugins != "" {
return errors.New("protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC\n\n" +