This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
protoc-gen-jsonschema | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// protoc plugin which converts .proto to JSON schema | ||
// It is spawned by protoc and generates JSON-schema files. | ||
// "Heavily influenced" by Google's "protog-gen-bq-schema" | ||
// | ||
// usage: | ||
// $ bin/protoc --jsonschema_out=path/to/outdir foo.proto | ||
// | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/chrusty/protoc-gen-jsonschema/internal/converter" | ||
"github.com/golang/protobuf/proto" | ||
plugin "github.com/golang/protobuf/protoc-gen-go/plugin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func main() { | ||
|
||
// Make a Logrus logger (default to INFO): | ||
logger := logrus.New() | ||
logger.SetLevel(logrus.InfoLevel) | ||
logger.SetOutput(os.Stderr) | ||
|
||
// Use the logger to make a Converter: | ||
protoConverter := converter.New(logger) | ||
|
||
// Convert the generator request: | ||
var ok = true | ||
logger.Debug("Processing code generator request") | ||
res, err := protoConverter.ConvertFrom(os.Stdin) | ||
if err != nil { | ||
ok = false | ||
if res == nil { | ||
message := fmt.Sprintf("Failed to read input: %v", err) | ||
res = &plugin.CodeGeneratorResponse{ | ||
Error: &message, | ||
} | ||
} | ||
} | ||
|
||
logger.Debug("Serializing code generator response") | ||
data, err := proto.Marshal(res) | ||
if err != nil { | ||
logger.WithError(err).Fatal("Cannot marshal response") | ||
} | ||
_, err = os.Stdout.Write(data) | ||
if err != nil { | ||
logger.WithError(err).Fatal("Failed to write response") | ||
} | ||
|
||
if ok { | ||
logger.Debug("Succeeded to process code generator request") | ||
} else { | ||
logger.Warn("Failed to process code generator but successfully sent the error to protoc") | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters