Skip to content
Merged
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
32 changes: 31 additions & 1 deletion cmd/protoc-gen-elixir-grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ This generates:

```elixir
defmodule Greeter.Server do
use GRPC.Server, service: Greeter.Service, http_transcode: true
use GRPC.Server,
service: Greeter.Service,
http_transcode: true

# ... method delegates
end
Expand All @@ -79,6 +81,33 @@ plugins:
- handler_module_prefix=MyApp.Handlers
```

#### Custom Codecs

Specify custom codec modules for your gRPC server:

```yaml
version: v2
plugins:
- local: protoc-gen-elixir
out: lib
- local: protoc-gen-elixir-grpc
out: lib
opt:
- codecs=GRPC.Codec.Proto,GRPC.Codec.WebText,GRPC.Codec.JSON
```

This generates:

```elixir
defmodule Greeter.Server do
use GRPC.Server,
service: Greeter.Service,
codecs: [GRPC.Codec.Proto, GRPC.Codec.WebText, GRPC.Codec.JSON]

# ... method delegates
end
```

#### Combining Options

You can combine multiple options:
Expand All @@ -93,6 +122,7 @@ plugins:
opt:
- http_transcode=true
- handler_module_prefix=MyApp.Handlers
- codecs=GRPC.Codec.Proto,GRPC.Codec.WebText,GRPC.Codec.JSON
```

### Basic Service Implementation
Expand Down
108 changes: 91 additions & 17 deletions cmd/protoc-gen-elixir-grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,89 @@ const (
packagePrefixFlag = "package_prefix"
handlerModulePrefixFlag = "handler_module_prefix"
httpTranscodeFlag = "http_transcode"
codecsFlag = "codecs"

usage = "\n\nFlags:\n -h, --help\tPrint this help and exit.\n --version\tPrint the version and exit.\n --handler_module_prefix\tCustom Elixir module prefix for handler modules instead of protobuf package.\n --http_transcode\tEnable HTTP transcoding support (adds http_transcode: true to use GRPC.Server)."
usage = "\n\nFlags:\n -h, --help\tPrint this help and exit.\n --version\tPrint the version and exit.\n --handler_module_prefix\tCustom Elixir module prefix for handler modules instead of protobuf package.\n --http_transcode\tEnable HTTP transcoding support (adds http_transcode: true to use GRPC.Server).\n --codecs\tComma-separated list of codec modules (e.g., 'GRPC.Codec.Proto,GRPC.Codec.WebText,GRPC.Codec.JSON')."
)

func parsePluginParameters(paramStr string, flagSet *flag.FlagSet) error {
if paramStr == "" {
return nil
}

params := strings.Split(paramStr, ",")
for _, param := range params {
param = strings.TrimSpace(param)
if param == "" {
// Parse key=value pairs, handling values that may contain commas
params := parseKeyValuePairs(paramStr)
for key, value := range params {
if err := flagSet.Set(key, value); err != nil {
return err
}
}

return nil
}

// parseKeyValuePairs parses a comma-separated list of key=value pairs.
// It handles the special case where a value may contain commas (e.g., codecs=A,B,C).
// The parser works by splitting on commas, then checking if each segment is a valid key=value pair.
// If not, it's assumed to be a continuation of the previous value.
func parseKeyValuePairs(paramStr string) map[string]string {
result := make(map[string]string)

segments := strings.Split(paramStr, ",")
var currentKey string
var currentValue strings.Builder

for _, segment := range segments {
segment = strings.TrimSpace(segment)
if segment == "" {
continue
}

parts := strings.SplitN(param, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid parameter format: %q, expected key=value", param)
// Check if this segment contains an '=' sign
if idx := strings.Index(segment, "="); idx > 0 {
// This is a new key=value pair
// Save the previous key-value if exists
if currentKey != "" {
result[currentKey] = currentValue.String()
}

// Start new key-value
currentKey = strings.TrimSpace(segment[:idx])
valueStart := strings.TrimSpace(segment[idx+1:])
currentValue.Reset()
currentValue.WriteString(valueStart)
} else {
// This is a continuation of the current value
if currentKey != "" {
currentValue.WriteString(",")
currentValue.WriteString(segment)
}
}
}

if err := flagSet.Set(parts[0], parts[1]); err != nil {
return err
// Save the final key-value pair if it exists
if currentKey != "" {
result[currentKey] = currentValue.String()
}

return result
}

func parseCodecs(codecsStr string) []string {
if codecsStr == "" {
return nil
}

codecs := strings.Split(codecsStr, ",")
var result []string
for _, codec := range codecs {
codec = strings.TrimSpace(codec)
if codec != "" {
result = append(result, codec)
}
}

return nil
return result
}

func main() {
Expand Down Expand Up @@ -117,6 +173,11 @@ func main() {
false,
"Enable HTTP transcoding support (adds http_transcode: true to use GRPC.Server).",
)
codecs := flagSet.String(
codecsFlag,
"",
"Comma-separated list of codec modules (e.g., 'GRPC.Codec.Proto,GRPC.Codec.WebText,GRPC.Codec.JSON').",
)

input, err := io.ReadAll(os.Stdin)
if err != nil {
Expand Down Expand Up @@ -152,6 +213,8 @@ func main() {
SupportedFeatures: proto.Uint64(uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)),
}

codecsList := parseCodecs(*codecs)

for _, fileName := range req.FileToGenerate {
var protoFile *descriptorpb.FileDescriptorProto
for _, file := range req.ProtoFile {
Expand All @@ -168,7 +231,7 @@ func main() {
continue
}

generateElixirFile(resp, protoFile, *packagePrefix, *handlerModulePrefix, *httpTranscode)
generateElixirFile(resp, protoFile, *packagePrefix, *handlerModulePrefix, *httpTranscode, codecsList)
}

output, err := proto.Marshal(resp)
Expand All @@ -183,7 +246,7 @@ func main() {
}
}

func generateElixirFile(resp *pluginpb.CodeGeneratorResponse, file *descriptorpb.FileDescriptorProto, packagePrefix, handlerModulePrefix string, httpTranscode bool) {
func generateElixirFile(resp *pluginpb.CodeGeneratorResponse, file *descriptorpb.FileDescriptorProto, packagePrefix, handlerModulePrefix string, httpTranscode bool, codecs []string) {
if len(file.Service) == 0 {
return
}
Expand All @@ -197,7 +260,7 @@ func generateElixirFile(resp *pluginpb.CodeGeneratorResponse, file *descriptorpb
content.WriteString("\n")

for _, service := range file.Service {
generateServiceModule(&content, file, service, handlerModulePrefix, httpTranscode)
generateServiceModule(&content, file, service, handlerModulePrefix, httpTranscode, codecs)
content.WriteString("\n")
}

Expand All @@ -207,14 +270,25 @@ func generateElixirFile(resp *pluginpb.CodeGeneratorResponse, file *descriptorpb
})
}

func generateServiceModule(content *strings.Builder, file *descriptorpb.FileDescriptorProto, service *descriptorpb.ServiceDescriptorProto, handlerModulePrefix string, httpTranscode bool) {
func generateServiceModule(content *strings.Builder, file *descriptorpb.FileDescriptorProto, service *descriptorpb.ServiceDescriptorProto, handlerModulePrefix string, httpTranscode bool, codecs []string) {
serverModuleName := generateServerModuleName(file, service)
serviceModuleName := generateServiceModuleName(file, service)

content.WriteString("defmodule " + serverModuleName + " do\n")
content.WriteString(" use GRPC.Server, service: " + serviceModuleName)
content.WriteString(" use GRPC.Server,\n")
content.WriteString(" service: " + serviceModuleName)
if httpTranscode {
content.WriteString(", http_transcode: true")
content.WriteString(",\n http_transcode: true")
}
if len(codecs) > 0 {
content.WriteString(",\n codecs: [")
for i, codec := range codecs {
if i > 0 {
content.WriteString(", ")
}
content.WriteString(codec)
}
content.WriteString("]")
}
content.WriteString("\n\n")

Expand Down
Loading