This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
forked from emicklei/proto
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request emicklei#11 from ziflex/feature/proto2gql
Added command for GraphQL Schema conversion
- Loading branch information
Showing
11 changed files
with
1,612 additions
and
0 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,4 +1,8 @@ | ||
cmd/proto2xsd/sample.proto | ||
cmd/proto2xsd/proto2xsd | ||
cmd/protofmt/protofmt | ||
cmd/proto2gql/proto2gql | ||
debug.test | ||
.idea/ | ||
bin/ | ||
.DS_Store |
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,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 |
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,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 |
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,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 | ||
} |
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,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 | ||
} |
Oops, something went wrong.