Skip to content

Commit

Permalink
examples and snapshot generation
Browse files Browse the repository at this point in the history
  • Loading branch information
advdv committed Mar 8, 2024
1 parent d15f197 commit cb8f5cf
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 37 deletions.
6 changes: 5 additions & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ plugins:
- name: go
out: .
opt: paths=source_relative
path: ["go", "run", "google.golang.org/protobuf/cmd/protoc-gen-go"]
path: ["go", "run", "google.golang.org/protobuf/cmd/protoc-gen-go"]
- name: protohtml-go
out: .
opt: paths=source_relative,snapshots=true
path: ["go", "run", "./cmd/protoc-gen-protohtml-go"]
38 changes: 37 additions & 1 deletion cmd/protoc-gen-protohtml-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)

// snapshot flag.
var snapshots = flag.Bool("snapshots", false, "enable snapshots to be saved for testing")

// programs entrypoint.
func main() {
protogen.Options{ParamFunc: flag.CommandLine.Set}.Run(run)
Expand All @@ -18,7 +25,36 @@ func main() {
func run(plugin *protogen.Plugin) error {
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)

log.Println("hello, world")
// write snapshots if enabled (for testing)
if *snapshots {
if err := snapshot(plugin.Request); err != nil {
return fmt.Errorf("failed to snapshot: %w", err)
}
}

return nil
}

// snapshot requests in the directories the generate for.
func snapshot(req *pluginpb.CodeGeneratorRequest) error {
const perms = 0o600

dir := filepath.Dir(req.GetFileToGenerate()[0])
if dir == "phtml/v1" {
return nil // skip for our own plugin options
}

fname := filepath.Join(dir, "phtml_request.bin")
log.Printf("writing request snapshot to: %s", fname)

bin, err := proto.Marshal(req)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}

if err := os.WriteFile(fname, bin, perms); err != nil {
return fmt.Errorf("failed to write snapshot file: %w", err)
}

return nil
}
150 changes: 150 additions & 0 deletions examples/example1/v1/example.pb.go

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

10 changes: 10 additions & 0 deletions examples/example1/v1/example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package examples.example1.v1;

import "phtml/v1/options.proto";

// Index route
message Index {
option (phtml.v1.route).pattern = "/{$}";
}
Binary file added examples/example1/v1/phtml_request.bin
Binary file not shown.
150 changes: 150 additions & 0 deletions examples/example_empty/v1/example.pb.go

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

6 changes: 6 additions & 0 deletions examples/example_empty/v1/example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

package examples.example_empty.v1;

// example that actually doesn't use protohtml at all.
message SomeMessage {}
Binary file added examples/example_empty/v1/phtml_request.bin
Binary file not shown.
Loading

0 comments on commit cb8f5cf

Please sign in to comment.