Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Including the cmd directory
Browse files Browse the repository at this point in the history
  • Loading branch information
chrusty committed Aug 27, 2019
1 parent b08dd80 commit acf4d0e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
protoc-gen-jsonschema
/.idea
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
default: darwin linux windows
default: build

build:
@echo "Generating binary (protoc-gen-jsonschema) ..."
@mkdir -p bin
@go build -o bin/protoc-gen-jsonschema cmd/protoc-gen-jsonschema/main.go

install:
@go install github.com/chrusty/protoc-gen-jsonschema/cmd/protoc-gen-jsonschema

build_linux:
@echo "Generating Linux-amd64 binary (protoc-gen-jsonschema.linux-amd64) ..."
@GOOS=linux GOARCH=amd64 go build -o protoc-gen-jsonschema.linux-amd64
Expand Down
60 changes: 60 additions & 0 deletions cmd/protoc-gen-jsonschema/main.go
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)
}
}
13 changes: 13 additions & 0 deletions jsonschemas/ArrayOfPrimitives.jsonschema
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"big_number": {
"oneOf": [
{
"type": "integer"
},
{
"type": "string"
},
{
"type": "null"
}
]
},
"description": {
"oneOf": [
{
Expand Down

0 comments on commit acf4d0e

Please sign in to comment.