mirror of
https://github.com/arkadiyt/protodump.git
synced 2026-10-21 01:36:52 +02:00
Add editions
This commit is contained in:
1 parent
0e1bc2418e
commit
3bb63dd071
2 files changed
+50
-20
No files matched your search
@@ -0,0 +1,12 @@
|
|||||||
|
edition = "2023";
|
||||||
|
|
||||||
|
package demo;
|
||||||
|
|
||||||
|
option go_package = "./;editions";
|
||||||
|
|
||||||
|
message Sample {
|
||||||
|
int32 id = 1;
|
||||||
|
string name = 2;
|
||||||
|
repeated int32 values = 3;
|
||||||
|
}
|
||||||
|
|
||||||
+38
-20
@@ -23,23 +23,6 @@ type ProtoDefinition struct {
|
|||||||
filename string
|
filename string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pd *ProtoDefinition) indent() {
|
|
||||||
pd.indendation += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pd *ProtoDefinition) dedent() {
|
|
||||||
pd.indendation -= 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pd *ProtoDefinition) writeIndented(s string) {
|
|
||||||
pd.builder.WriteString(strings.Repeat(" ", pd.indendation))
|
|
||||||
pd.write(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pd *ProtoDefinition) write(s string) {
|
|
||||||
pd.builder.WriteString(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func escapeProtoString(s string) string {
|
func escapeProtoString(s string) string {
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
@@ -63,6 +46,34 @@ func escapeProtoString(s string) string {
|
|||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func editionString(d protoreflect.FileDescriptor) (string, error) {
|
||||||
|
edition := protodesc.ToFileDescriptorProto(d).GetEdition().String()
|
||||||
|
if strings.HasPrefix(edition, "EDITION_") {
|
||||||
|
rest := strings.TrimPrefix(edition, "EDITION_")
|
||||||
|
if _, err := strconv.Atoi(rest); err == nil {
|
||||||
|
return rest, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "unknown", fmt.Errorf("not a source edition: %v", edition)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pd *ProtoDefinition) indent() {
|
||||||
|
pd.indendation += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pd *ProtoDefinition) dedent() {
|
||||||
|
pd.indendation -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pd *ProtoDefinition) writeIndented(s string) {
|
||||||
|
pd.builder.WriteString(strings.Repeat(" ", pd.indendation))
|
||||||
|
pd.write(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pd *ProtoDefinition) write(s string) {
|
||||||
|
pd.builder.WriteString(s)
|
||||||
|
}
|
||||||
|
|
||||||
func (pd *ProtoDefinition) writeQuoted(s string) {
|
func (pd *ProtoDefinition) writeQuoted(s string) {
|
||||||
pd.write("\"")
|
pd.write("\"")
|
||||||
pd.write(escapeProtoString(s))
|
pd.write(escapeProtoString(s))
|
||||||
@@ -334,9 +345,16 @@ func (pd *ProtoDefinition) writeFileOptions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pd *ProtoDefinition) writeFileDescriptor() {
|
func (pd *ProtoDefinition) writeFileDescriptor() {
|
||||||
pd.write("syntax = \"")
|
if pd.descriptor.Syntax() == protoreflect.Editions {
|
||||||
pd.write(pd.descriptor.Syntax().String())
|
pd.write("edition = \"")
|
||||||
pd.write("\";\n\n")
|
edition, _ := editionString(pd.descriptor)
|
||||||
|
pd.write(edition)
|
||||||
|
pd.write("\";\n\n")
|
||||||
|
} else {
|
||||||
|
pd.write("syntax = \"")
|
||||||
|
pd.write(pd.descriptor.Syntax().String())
|
||||||
|
pd.write("\";\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
packageName := pd.descriptor.FullName()
|
packageName := pd.descriptor.FullName()
|
||||||
if packageName != "" {
|
if packageName != "" {
|
||||||
|
|||||||
Reference in new issue
Block a user