Skip to content

Commit 87928f8

Browse files
committed
feat(decoder): ✨ add proto descriptor decoding tool
Added a new Go module "decoder" that reconstructs .proto files from a protobuf FileDescriptorSet binary. The tool outputs the recovered proto files to a specified directory. Updated solution structure to reference the new decoder module.
1 parent a29e10a commit 87928f8

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

EcoFlow.Mqtt.Api.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
<Project Path="src/EcoFlow.Mqtt.Api/EcoFlow.Mqtt.Api.csproj" />
77
<Project Path="src/EcoFlow.Proto.Exporter/EcoFlow.Proto.Exporter.csproj" />
88
</Folder>
9+
<Folder Name="/src/EcoFlow.Proto.Decoder/" />
910
</Solution>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"google.golang.org/protobuf/proto"
8+
"google.golang.org/protobuf/types/descriptorpb"
9+
10+
"github.com/jhump/protoreflect/desc"
11+
"github.com/jhump/protoreflect/desc/protoprint"
12+
)
13+
14+
func main() {
15+
if len(os.Args) != 3 {
16+
os.Stderr.WriteString("usage: decoder <file_descriptor_set.pb> <out_directory>\n")
17+
os.Exit(2)
18+
}
19+
20+
descriptorSetPath := os.Args[1]
21+
outputDirectoryPath := os.Args[2]
22+
23+
descriptorSetBytes, readError := os.ReadFile(descriptorSetPath)
24+
if readError != nil {
25+
panic(readError)
26+
}
27+
28+
var fileDescriptorSet descriptorpb.FileDescriptorSet
29+
if unmarshalError := proto.Unmarshal(descriptorSetBytes, &fileDescriptorSet); unmarshalError != nil {
30+
panic(unmarshalError)
31+
}
32+
33+
fileDescriptors, buildError := desc.CreateFileDescriptorsFromSet(&fileDescriptorSet)
34+
if buildError != nil {
35+
panic(buildError)
36+
}
37+
38+
protoPrinter := protoprint.Printer{}
39+
40+
for _, fileDescriptor := range fileDescriptors {
41+
outputFilePath := filepath.Join(outputDirectoryPath, fileDescriptor.GetName())
42+
if mkdirError := os.MkdirAll(filepath.Dir(outputFilePath), 0o755); mkdirError != nil {
43+
panic(mkdirError)
44+
}
45+
46+
outputFile, createError := os.Create(outputFilePath)
47+
if createError != nil {
48+
panic(createError)
49+
}
50+
51+
printError := protoPrinter.PrintProtoFile(fileDescriptor, outputFile)
52+
closeError := outputFile.Close()
53+
if printError != nil {
54+
panic(printError)
55+
}
56+
if closeError != nil {
57+
panic(closeError)
58+
}
59+
}
60+
}

src/EcoFlow.Proto.Decoder/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module decoder
2+
3+
go 1.25.6
4+
5+
require (
6+
github.com/golang/protobuf v1.5.4 // indirect
7+
github.com/jhump/protoreflect v1.18.0 // indirect
8+
github.com/jhump/protoreflect/v2 v2.0.0-beta.1 // indirect
9+
google.golang.org/protobuf v1.36.11 // indirect
10+
)

src/EcoFlow.Proto.Decoder/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
2+
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
3+
github.com/jhump/protoreflect v1.18.0 h1:TOz0MSR/0JOZ5kECB/0ufGnC2jdsgZ123Rd/k4Z5/2w=
4+
github.com/jhump/protoreflect v1.18.0/go.mod h1:ezWcltJIVF4zYdIFM+D/sHV4Oh5LNU08ORzCGfwvTz8=
5+
github.com/jhump/protoreflect/v2 v2.0.0-beta.1 h1:Dw1rslK/VotaUGYsv53XVWITr+5RCPXfvvlGrM/+B6w=
6+
github.com/jhump/protoreflect/v2 v2.0.0-beta.1/go.mod h1:D9LBEowZyv8/iSu97FU2zmXG3JxVTmNw21mu63niFzU=
7+
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
8+
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=

0 commit comments

Comments
 (0)