Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request emicklei#11 from ziflex/feature/proto2gql
Browse files Browse the repository at this point in the history
Added command for GraphQL Schema conversion
  • Loading branch information
emicklei authored Nov 22, 2017
2 parents e86a0d9 + 493c4ea commit 23424ce
Show file tree
Hide file tree
Showing 11 changed files with 1,612 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
cmd/proto2xsd/sample.proto
cmd/proto2xsd/proto2xsd
cmd/protofmt/protofmt
cmd/proto2gql/proto2gql
debug.test
.idea/
bin/
.DS_Store
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ This repository also includes 2 commands. The `protofmt` tool is for formatting

See folder `cmd/proto2xsd/README.md` for more details.

### usage of proto2gql command

> proto2gql -help
Usage of proto2gql [flags] [path ...]

-std_out
Writes transformed files to stdout
-txt_out string
Writes transformed files to .graphql file
-go_out string
Writes transformed files to .go file
-js_out string
Writes transformed files to .js file
-package_alias value
Renames packages using given aliases
-resolve_import value
Resolves given external packages
-no_prefix
Disables package prefix for type names

See folder `cmd/proto2gql/README.md` for more details.

### usage of protofmt command

> protofmt -help
Expand Down
9 changes: 9 additions & 0 deletions cmd/proto2gql/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.PHONY: build install generate vet compile test doc fmt lint docker

default: test compile

compile:
go build -v -o ./proto2gql ./*.go

test:
go test ./*.go
24 changes: 24 additions & 0 deletions cmd/proto2gql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# proto2gql

GraphQL Schema conversion tool for Google Protocol Buffers version 3

> proto2gql -help
Usage of proto2gql [flags] [path ...]

-std_out
Writes transformed files to stdout
-txt_out string
Writes transformed files to .graphql file
-go_out string
Writes transformed files to .go file
-js_out string
Writes transformed files to .js file
-package_alias value
Renames packages using given aliases
-resolve_import value
Resolves given external packages
-no_prefix
Disables package prefix for type names

### build
make
45 changes: 45 additions & 0 deletions cmd/proto2gql/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import "strings"

type Converter struct {
noPrefix bool
pkgAliases map[string]string
}

func (c *Converter) NewTypeName(scope *Scope, name string) string {
return scope.packageName + strings.Join(scope.path, "") + name
}

func (c *Converter) OriginalTypeName(scope *Scope, name string) string {
switch len(scope.path) {
case 0:
return name
case 1:
return scope.path[0] + "." + name
default:
return strings.Join(scope.path, ".") + "." + name
}
}

func (c *Converter) PackageName(parts []string) string {
var name string

if c.noPrefix == true {
return name
}

if len(c.pkgAliases) > 0 {
alias, exists := c.pkgAliases[strings.Join(parts, ".")]

if exists == true {
return alias
}
}

for _, segment := range parts {
name += strings.Title(segment)
}

return name
}
78 changes: 78 additions & 0 deletions cmd/proto2gql/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"os"
"io/ioutil"
"path"
)

type FileWriter struct {
filename string
file *os.File
closeTag string
isClosed bool
}

func NewFileWriter(filename string, openTag, closeTag string) (*FileWriter, error) {
file, err := ioutil.TempFile(os.TempDir(), "proto2gql")

if err != nil {
return nil, err
}

file.Write([]byte(openTag))

return &FileWriter{filename, file, closeTag, false}, nil
}

func (fw *FileWriter) IsClosed() bool {
return fw.isClosed
}

func (fw *FileWriter) Write(p []byte) (n int, err error) {
return fw.file.Write(p)
}

func (fw *FileWriter) Save() error {
if fw.isClosed == true {
return nil // return an error?
}

tmpName := fw.file.Name()

fw.file.Write([]byte(fw.closeTag))

if err := fw.Close(); err != nil {
return err
}

if err := os.MkdirAll(path.Dir(fw.filename), os.ModePerm); err != nil {
return err
}

return os.Rename(tmpName, fw.filename)
}

func (fw *FileWriter) Remove() error {
if fw.isClosed == true {
return nil // return an error?
}

tmpName := fw.file.Name()

if err := fw.Close(); err != nil {
return err
}

return os.Remove(tmpName)
}

func (fw *FileWriter) Close() error {
if fw.isClosed == false {
fw.isClosed = true

return fw.file.Close()
}

return nil
}
Loading

0 comments on commit 23424ce

Please sign in to comment.