Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add source code info in descriptor #131

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ name: Go

on:
push:
branches: [ main ]
branches: [main]
pull_request:

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.18
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./...
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.18-alpine
FROM golang:1.22-alpine

ENV GOLANG_PROTOBUF_VERSION=1.28.1
ENV GOLANG_PROTOBUF_VERSION=1.32.0

ARG PROTOC_VERSION="3.20.0"
# add dependency
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ proto-gen:
(cd proto; buf generate)
mv proto/cosmos_proto/cosmos.pb.go .

install:
go install ./cmd/protoc-gen-go-pulsar

.PHONY: proto_gen pulsar
4 changes: 2 additions & 2 deletions cosmos.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 30 additions & 29 deletions features/protoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ package protoc

import (
"fmt"
"github.com/cosmos/cosmos-proto/features/protoc/genid"
"github.com/cosmos/cosmos-proto/generator"
"go/ast"
"go/parser"
"go/token"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"
"math"
"strconv"
"strings"
"unicode"
"unicode/utf8"

pref "google.golang.org/protobuf/reflect/protoreflect"
"github.com/cosmos/cosmos-proto/features/protoc/genid"
"github.com/cosmos/cosmos-proto/generator"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"

"github.com/cosmos/cosmos-proto/features/protoc/version"
"google.golang.org/protobuf/compiler/protogen"
Expand Down Expand Up @@ -130,7 +129,9 @@ func initFuncName(f *protogen.File) string {

func genFileDescriptor(gen *protogen.Plugin, g *generator.GeneratedFile, f *fileInfo) {
descProto := proto.Clone(f.Proto).(*descriptorpb.FileDescriptorProto)
descProto.SourceCodeInfo = nil // drop source code information
// We do not want to drop the source code info because the comments are important for cosmos-sdk client/v2
// ref: https://github.com/golang/protobuf/issues/1134, https://go-review.googlesource.com/c/protobuf/+/235099
// descProto.SourceCodeInfo = nil // drop source code information

b, err := proto.MarshalOptions{AllowPartial: true, Deterministic: true}.Marshal(descProto)
if err != nil {
Expand Down Expand Up @@ -1944,38 +1945,38 @@ func (c trailingComment) String() string {
// Depending on the context on how Marshal is called, there are different ways
// through which that information is determined. As such it is the caller's
// responsibility to provide a function to obtain that information.
func TagMarshal(fd pref.FieldDescriptor, enumName string) string {
func TagMarshal(fd protoreflect.FieldDescriptor, enumName string) string {
var tag []string
switch fd.Kind() {
case pref.BoolKind, pref.EnumKind, pref.Int32Kind, pref.Uint32Kind, pref.Int64Kind, pref.Uint64Kind:
case protoreflect.BoolKind, protoreflect.EnumKind, protoreflect.Int32Kind, protoreflect.Uint32Kind, protoreflect.Int64Kind, protoreflect.Uint64Kind:
tag = append(tag, "varint")
case pref.Sint32Kind:
case protoreflect.Sint32Kind:
tag = append(tag, "zigzag32")
case pref.Sint64Kind:
case protoreflect.Sint64Kind:
tag = append(tag, "zigzag64")
case pref.Sfixed32Kind, pref.Fixed32Kind, pref.FloatKind:
case protoreflect.Sfixed32Kind, protoreflect.Fixed32Kind, protoreflect.FloatKind:
tag = append(tag, "fixed32")
case pref.Sfixed64Kind, pref.Fixed64Kind, pref.DoubleKind:
case protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind, protoreflect.DoubleKind:
tag = append(tag, "fixed64")
case pref.StringKind, pref.BytesKind, pref.MessageKind:
case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind:
tag = append(tag, "bytes")
case pref.GroupKind:
case protoreflect.GroupKind:
tag = append(tag, "group")
}
tag = append(tag, strconv.Itoa(int(fd.Number())))
switch fd.Cardinality() {
case pref.Optional:
case protoreflect.Optional:
tag = append(tag, "opt")
case pref.Required:
case protoreflect.Required:
tag = append(tag, "req")
case pref.Repeated:
case protoreflect.Repeated:
tag = append(tag, "rep")
}
if fd.IsPacked() {
tag = append(tag, "packed")
}
name := string(fd.Name())
if fd.Kind() == pref.GroupKind {
if fd.Kind() == protoreflect.GroupKind {
// The name of the FieldDescriptor for a group field is
// lowercased. To find the original capitalization, we
// look in the field's MessageType.
Expand All @@ -1993,10 +1994,10 @@ func TagMarshal(fd pref.FieldDescriptor, enumName string) string {
// The previous implementation does not tag extension fields as proto3,
// even when the field is defined in a proto3 file. Match that behavior
// for consistency.
if fd.Syntax() == pref.Proto3 && !fd.IsExtension() {
if fd.Syntax() == protoreflect.Proto3 && !fd.IsExtension() {
tag = append(tag, "proto3")
}
if fd.Kind() == pref.EnumKind && enumName != "" {
if fd.Kind() == protoreflect.EnumKind && enumName != "" {
tag = append(tag, "enum="+enumName)
}
if fd.ContainingOneof() != nil {
Expand Down Expand Up @@ -2034,9 +2035,9 @@ func genMessageReflectMethods(g *generator.GeneratedFile, f *fileInfo, m *messag
// DefValMarshal serializes v as the default string according to the given kind k.
// When specifying the Descriptor format for an enum kind, the associated
// enum value descriptor must be provided.
func DefValMarshal(v pref.Value, ev pref.EnumValueDescriptor, k pref.Kind, f Format) (string, error) {
func DefValMarshal(v protoreflect.Value, ev protoreflect.EnumValueDescriptor, k protoreflect.Kind, f Format) (string, error) {
switch k {
case pref.BoolKind:
case protoreflect.BoolKind:
if f == GoTag {
if v.Bool() {
return "1", nil
Expand All @@ -2050,17 +2051,17 @@ func DefValMarshal(v pref.Value, ev pref.EnumValueDescriptor, k pref.Kind, f For
return "false", nil
}
}
case pref.EnumKind:
case protoreflect.EnumKind:
if f == GoTag {
return strconv.FormatInt(int64(v.Enum()), 10), nil
} else {
return string(ev.Name()), nil
}
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind, pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind, protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
return strconv.FormatInt(v.Int(), 10), nil
case pref.Uint32Kind, pref.Fixed32Kind, pref.Uint64Kind, pref.Fixed64Kind:
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind, protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return strconv.FormatUint(v.Uint(), 10), nil
case pref.FloatKind, pref.DoubleKind:
case protoreflect.FloatKind, protoreflect.DoubleKind:
f := v.Float()
switch {
case math.IsInf(f, -1):
Expand All @@ -2070,16 +2071,16 @@ func DefValMarshal(v pref.Value, ev pref.EnumValueDescriptor, k pref.Kind, f For
case math.IsNaN(f):
return "nan", nil
default:
if k == pref.FloatKind {
if k == protoreflect.FloatKind {
return strconv.FormatFloat(f, 'g', -1, 32), nil
} else {
return strconv.FormatFloat(f, 'g', -1, 64), nil
}
}
case pref.StringKind:
case protoreflect.StringKind:
// String values are serialized as is without any escaping.
return v.String(), nil
case pref.BytesKind:
case protoreflect.BytesKind:
if s, ok := marshalBytes(v.Bytes()); ok {
return s, nil
}
Expand Down
17 changes: 16 additions & 1 deletion internal/testprotos/test3/compliance_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package test3

import (
"google.golang.org/protobuf/testing/prototest"
"strings"
"testing"

"google.golang.org/protobuf/testing/prototest"
)

func TestCompliance(t *testing.T) {
prototest.Message{}.Test(t, (&TestAllTypes{}).ProtoReflect().Type())
}

func TestSourceCodeInfo(t *testing.T) {
descriptor := (&ForeignMessage{}).ProtoReflect().Descriptor()
sourceInfos := descriptor.ParentFile().SourceLocations()

if sourceInfos.Len() == 0 {
t.Errorf("No source location found")
}

if !strings.Contains(sourceInfos.ByDescriptor(descriptor).LeadingComments, "This comment is for testing source code info comments") {
t.Errorf("LeadingComments not found in source info")
}
}
1 change: 1 addition & 0 deletions internal/testprotos/test3/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ message TestAllTypes {
}
}

// This comment is for testing source code info comments.
message ForeignMessage {
int32 c = 1;
int32 d = 2;
Expand Down
Loading
Loading