An open API service indexing awesome lists of open source software.

https://github.com/nithanim/protoreverse


https://github.com/nithanim/protoreverse

Last synced: 11 months ago
JSON representation

Awesome Lists containing this project

README

          

# ProtobufReverter

A Java library that converts Protocol Buffer FileDescriptor objects back to .proto file format. This allows for reverse engineering compiled protobuf definitions back to their source format.

## Overview

Protocol Buffers (protobuf) is a language-neutral, platform-neutral, extensible mechanism for serializing structured data. When working with protobuf in Java, the protoc compiler generates Java classes from .proto files. These generated classes contain a `getDescriptor()` method that returns a `FileDescriptor` object, which contains all the information about the original .proto file.

This library provides a way to convert that `FileDescriptor` back to a .proto file format, effectively reversing the compilation process.

## Features

- Converts a FileDescriptor to a proto file string
- Handles all protobuf elements:
- Messages
- Fields (including repeated and map fields)
- Enums
- Nested types
- Oneofs
- Services and methods
- Options
- Imports

## Usage

```java
import org.example.protobuf.reverter.ProtobufReverter;
import com.google.protobuf.Descriptors.FileDescriptor;

// Get the FileDescriptor from a generated protobuf class
FileDescriptor fileDescriptor = YourGeneratedProtoClass.getDescriptor();

// Convert the FileDescriptor to a proto file
String protoFile = ProtobufReverter.toProtoFile(fileDescriptor);

// Use the proto file content as needed
System.out.println(protoFile);
// or save to a file
Files.writeString(Path.of("reversed.proto"), protoFile);
```

## Example

If you have a generated protobuf class called `Person.java` from a `person.proto` file, you can reverse it back to a .proto file like this:

```java
import org.example.protobuf.reverter.ProtobufReverter;
import com.example.protos.Person;

// Get the FileDescriptor from the Person class
FileDescriptor fileDescriptor = Person.getDescriptor();

// Convert it back to a proto file
String protoFile = ProtobufReverter.toProtoFile(fileDescriptor);

// Print the proto file content
System.out.println(protoFile);
```

## Requirements

- Java 21 or higher
- Protocol Buffers Java library (com.google.protobuf:protobuf-java)

## Installation

Add the following dependency to your Maven project:

```xml

org.example
ProtobufReverter
1.0-SNAPSHOT

```

## License

This project is licensed under the MIT License - see the LICENSE file for details.