Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aaharu committed Jan 7, 2017
1 parent 7be049b commit 06041b5
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 73 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Change Log

## [Unreleased]
## [0.2.0] - 2017-01-08
### Fixed
- fix many bugs

### Changed
- gofmt-ed output

### Added
- support unix pipe input


## [0.1.0] - 2017-01-06
### Added
- pre-release
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ go get github.com/aaharu/schemarshal

```
SYNOPSIS
schemarshal [options] <json_shcema_file>
schemarshal [options] [<json_schema_file>]
OPTIONS
-h, -help
Show help message.
Show this help message.
-o <file>, -output <file>
Write output to <file> instead of stdout.
Write output to file instead of stdout.
-p <package>, -package <package>
Package name for output. (default `main`)
-version
-t <package>, -type <package>
Set default Type name.
-v, -version
Show version.
```

Expand All @@ -38,6 +40,9 @@ OPTIONS
* https://github.com/lestrrat/go-jsschema
- JSON Schema parser
- MIT License
* golang.org/x/crypto/ssh/terminal
- https://godoc.org/golang.org/x/crypto/ssh/terminal
- https://golang.org/LICENSE

## Similar Projects

Expand Down
64 changes: 64 additions & 0 deletions cui/cui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2017 aaharu All rights reserved.
// This source code is licensed under the BSD-style license found in
// the LICENSE file in the root directory of this source tree.

// This source code use following software(s):
// - go-jsschema https://github.com/lestrrat/go-jsschema
// Copyright (c) 2016 lestrrat

package cui

import (
"flag"
"fmt"
"os"
"path"
"strings"
)

type Arguments struct {
OutputFileName string
PackageName string
TypeName string
ShowVersion bool
}

// Usage show help message.
func Usage() {
fmt.Fprint(os.Stderr, "SYNOPSIS\n")
fmt.Fprint(os.Stderr, " schemarshal [options] [<json_schema_file>]\n")
fmt.Fprint(os.Stderr, "OPTIONS\n")
fmt.Fprint(os.Stderr, " -h, -help\n")
fmt.Fprint(os.Stderr, " Show this help message.\n")
fmt.Fprint(os.Stderr, " -o <file>, -output <file>\n")
fmt.Fprintf(os.Stderr, " %s\n", flag.Lookup("o").Usage)
fmt.Fprint(os.Stderr, " -p <package>, -package <package>\n")
fmt.Fprintf(os.Stderr, " %s\n", flag.Lookup("p").Usage)
fmt.Fprint(os.Stderr, " -t <package>, -type <package>\n")
fmt.Fprintf(os.Stderr, " %s\n", flag.Lookup("t").Usage)
fmt.Fprint(os.Stderr, " -v, -version\n")
fmt.Fprintf(os.Stderr, " %s\n", flag.Lookup("v").Usage)
}

// ParseArguments parse command-line arguments
func ParseArguments() *Arguments {
args := new(Arguments)
flag.Usage = Usage
flag.StringVar(&args.OutputFileName, "o", "", "Write output to file instead of stdout.")
flag.StringVar(&args.OutputFileName, "output", "", "Write output to file instead of stdout.")
flag.StringVar(&args.PackageName, "p", "main", "Package name for output. (default `main`)")
flag.StringVar(&args.PackageName, "package", "main", "Package name for output. (default `main`)")
flag.StringVar(&args.TypeName, "t", "", "Set default Type name.")
flag.StringVar(&args.TypeName, "type", "", "Set default Type name.")
flag.BoolVar(&args.ShowVersion, "v", false, "Show version.")
flag.BoolVar(&args.ShowVersion, "version", false, "Show version.")
flag.Parse()
return args
}

// FileName return file-name without ext
func FileName(file *os.File) string {
name := path.Base(file.Name())
ext := path.Ext(name)
return strings.TrimRight(name, ext)
}
8 changes: 6 additions & 2 deletions glide.lock

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

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package: github.com/aaharu/schemarshal
import:
- package: github.com/lestrrat/go-jsschema
- package: golang.org/x/crypto
subpackages:
- ssh/terminal
20 changes: 13 additions & 7 deletions jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ package jsonschema

import (
"fmt"
"go/format"

schema "github.com/lestrrat/go-jsschema"

utils "github.com/aaharu/schemarshal/utils"
"github.com/aaharu/schemarshal/utils"
)

// JSONSchema is JSON Schema interface
Expand All @@ -31,17 +32,17 @@ func New(s *schema.Schema) *JSONSchema {
}

// SetCommand set command
func (js JSONSchema) SetCommand(command string) {
func (js *JSONSchema) SetCommand(command string) {
js.command = command
}

// SetPackageName set package name
func (js JSONSchema) SetPackageName(packageName string) {
func (js *JSONSchema) SetPackageName(packageName string) {
js.packageName = packageName
}

// Typedef is a generator that define struct from JSON Schema
func (js JSONSchema) Typedef(defaultTypeName string) (string, error) {
func (js *JSONSchema) Typedef(defaultTypeName string) (string, error) {
var str string
str += utils.GeneratedByComment(js.command)
str += "\n"
Expand All @@ -62,10 +63,15 @@ func (js JSONSchema) Typedef(defaultTypeName string) (string, error) {
str += genarated
str += "\n"

return str, err
src, err := format.Source([]byte(str))
if err != nil {
return str, err
}

return string(src), err
}

func (js JSONSchema) structor(name string, nestLevel int, required bool) (string, error) {
func (js *JSONSchema) structor(name string, nestLevel int, required bool) (string, error) {
var str string

for i := 1; i <= nestLevel; i++ {
Expand All @@ -87,7 +93,7 @@ func (js JSONSchema) structor(name string, nestLevel int, required bool) (string
return str, nil
}

func (js JSONSchema) typer(nestLevel int) (string, error) {
func (js *JSONSchema) typer(nestLevel int) (string, error) {
var str string
if inPrimitiveTypes(schema.IntegerType, js.schema.Type) {
if inPrimitiveTypes(schema.NullType, js.schema.Type) {
Expand Down
94 changes: 45 additions & 49 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,80 @@ package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"syscall"

"golang.org/x/crypto/ssh/terminal"

schema "github.com/lestrrat/go-jsschema"

jsonschema "github.com/aaharu/schemarshal/jsonschema"
version "github.com/aaharu/schemarshal/version"
"github.com/aaharu/schemarshal/cui"
"github.com/aaharu/schemarshal/jsonschema"
"github.com/aaharu/schemarshal/version"
)

func main() {
var (
outputFileName string
packageName string
)
flag.StringVar(&outputFileName, "o", "", "Write output to file instead of stdout.")
flag.StringVar(&outputFileName, "output", "", "Write output to file instead of stdout.")
flag.StringVar(&packageName, "p", "main", "Package name for output.")
flag.StringVar(&packageName, "package", "main", "Package name for output.")
showVersion := flag.Bool("version", false, "Show version.")
flag.Parse()

if len(os.Args) > 1 && *showVersion {
args := cui.ParseArguments()

if args.ShowVersion {
fmt.Printf("schemarshal %s\n", version.Version)
os.Exit(0)
}

if len(flag.Args()) < 1 {
usage()
os.Exit(1)
}
var input io.Reader
typeName := args.TypeName
if terminal.IsTerminal(syscall.Stdin) {
if len(flag.Args()) < 1 {
cui.Usage()
os.Exit(1)
}

inputFile, err := os.Open(flag.Args()[0])
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open schema: %s\n", err)
os.Exit(1)
inputFile, err := os.Open(flag.Args()[0])
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open schema: %s\n", err)
os.Exit(1)
}
defer inputFile.Close()

if typeName == "" {
typeName = cui.FileName(inputFile)
}
input = inputFile
} else {
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
os.Exit(1)
}

if typeName == "" {
typeName = "T"
}
input = strings.NewReader(string(stdin))
}
defer inputFile.Close()

jsschema, err := schema.Read(inputFile)
jsschema, err := schema.Read(input)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read schema: %s\n", err)
os.Exit(1)
}

js := jsonschema.New(jsschema)
js.SetCommand(strings.Trim(fmt.Sprintf("%v", os.Args), "[]"))
js.SetPackageName(packageName)
output, err := js.Typedef(fileName(inputFile))
js.SetPackageName(args.PackageName)
output, err := js.Typedef(typeName)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate: %s\n", err)
os.Exit(1)
}

if outputFileName == "" {
if args.OutputFileName == "" {
fmt.Printf("%s\n", output)
} else {
outputFile, err := os.Create(outputFileName)
outputFile, err := os.Create(args.OutputFileName)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write file: %s\n", err)
os.Exit(1)
Expand All @@ -78,23 +94,3 @@ func main() {
outputFile.Write([]byte(output))
}
}

func usage() {
fmt.Println("SYNOPSIS")
fmt.Println(" schemarshal [options] <json_shcema_file>")
fmt.Println("OPTIONS")
fmt.Println(" -h, -help")
fmt.Println(" Show help message.")
fmt.Println(" -o <file>, -output <file>")
fmt.Println(" Write output to <file> instead of stdout.")
fmt.Println(" -p <package>, -package <package>")
fmt.Println(" Package name for output. (default `main`)")
fmt.Println(" -version")
fmt.Println(" Show version.")
}

func fileName(file *os.File) string {
name := path.Base(file.Name())
ext := path.Ext(name)
return strings.TrimRight(name, ext)
}
2 changes: 1 addition & 1 deletion utils/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"strings"

version "github.com/aaharu/schemarshal/version"
"github.com/aaharu/schemarshal/version"
)

// MarshalTag return tag for MarshalJSON
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
package version

// Version of schemarshal
const Version = "0.1.0"
const Version = "0.2.0"
17 changes: 9 additions & 8 deletions wercker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ build:
# corros compile
- tcnksm/gox:
os: "darwin linux windows freebsd netbsd"
arch: "amd64"

- tcnksm/zip:
input: ${WERCKER_OUTPUT_DIR}/pkg
output: ${WERCKER_OUTPUT_DIR}/dist
input: ${WERCKER_OUTPUT_DIR}/pkg
output: ${WERCKER_OUTPUT_DIR}/dist

- script:
name: output release tag
Expand All @@ -51,10 +52,10 @@ build:
deploy:
steps:
- script:
name: restore release tag
code: |
export RELEASE_TAG=$(cat tag.out)
name: restore release tag
code: |
export RELEASE_TAG=$(cat tag.out)
- tcnksm/ghr:
token: ${GITHUB_TOKEN}
input: dist
version: ${RELEASE_TAG}
token: ${GITHUB_TOKEN}
input: dist
version: ${RELEASE_TAG}

0 comments on commit 06041b5

Please sign in to comment.