Skip to content

Commit

Permalink
atg. quiet with json schema output too
Browse files Browse the repository at this point in the history
  • Loading branch information
glycerine committed Dec 25, 2016
1 parent 8746c9f commit 5826372
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 91 deletions.
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package main

import (
"bytes"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -101,7 +102,7 @@ func Run(mode gen.Method, c *cfg.ZebraConfig) error {
return nil
} else {
if c.WriteSchema != "" { // saveSchemaAsMsgpackToFile
err := saveMsgpackFile(c.WriteSchema, fs)
err := saveMsgpackFile(c.GoFile, c.WriteSchema, fs)
if err != nil {
panic(err)
}
Expand All @@ -128,7 +129,7 @@ func newFilename(out, old, pkg string) string {
return strings.TrimSuffix(old, ".go") + "_gen.go"
}

func saveMsgpackFile(path string, fs *parse.FileSet) error {
func saveMsgpackFile(parsedPath, path string, fs *parse.FileSet) error {
fmt.Printf("\n saveMsgpackFile saving to: '%v'", path)

var f *os.File
Expand All @@ -145,14 +146,28 @@ func saveMsgpackFile(path string, fs *parse.FileSet) error {
w := msgp.NewWriter(f)
defer w.Flush()

tr, err := parse.TranslateToZebraSchema(fs)
tr, err := parse.TranslateToZebraSchema(parsedPath, fs)
if err != nil {
return err
}
err = tr.EncodeMsg(w)
if err != nil {
return err
}
if path != "-" {
by, err := tr.MarshalMsg(nil)
if err != nil {
return err
}
fjson, err := os.Create(path + ".json")
if err != nil {
return err
}
defer fjson.Close()

buf := bytes.NewBuffer(by)
_, err = msgp.CopyToJSON(fjson, buf)
return err
}
return nil
}
19 changes: 10 additions & 9 deletions parse/toschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"github.com/glycerine/zebrapack/zebra"
)

func TranslateToZebraSchema(fs *FileSet) (*zebra.Schema, error) {
func TranslateToZebraSchema(path string, fs *FileSet) (*zebra.Schema, error) {

structs := []zebra.Struct{}

for k, ele := range fs.Identities {
fmt.Printf("\n on k = %v\n", k)
for _, ele := range fs.Identities {
//fmt.Printf("\n on k = %v\n", k)
switch x := ele.(type) {
case *gen.Struct:
tr := zebra.Struct{
Expand All @@ -26,10 +26,10 @@ func TranslateToZebraSchema(fs *FileSet) (*zebra.Schema, error) {
FieldName: f.FieldTag,
}
if !fld.Skip {
fld.TypStr = f.FieldElem.TypeName()
fld.FieldTypeStr = f.FieldElem.TypeName()
}
fmt.Printf("\n in %v, on field %#v ... fld='%#v'\n", tr.StructName, f, fld)
tr.Fld = append(tr.Fld, fld)
//fmt.Printf("\n in %v, on field %#v ... fld='%#v'\n", tr.StructName, f, fld)
tr.Fields = append(tr.Fields, fld)
}
structs = append(structs, tr)
/*
Expand All @@ -44,10 +44,11 @@ func TranslateToZebraSchema(fs *FileSet) (*zebra.Schema, error) {
}
}
sch := &zebra.Schema{
Structs: structs,
SourcePath: path,
Structs: structs,
}
fmt.Printf("total number of structs: %v\n", len(structs))
fmt.Printf("total number of fields in first struct: %v\n", len(structs[0].Fld))
//fmt.Printf("total number of structs: %v\n", len(structs))
//fmt.Printf("total number of fields in first struct: %v\n", len(structs[0].Fields))
return sch, nil

}
14 changes: 7 additions & 7 deletions zebra/zebra.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ const (

// ZebraSchema is the top level container
type Schema struct {
PkgPath string // reflect.TypeOf().PkgPath()
SourcePath string // reflect.TypeOf().PkgPath()

Structs []Struct
}

// Struct represents a single message or struct.
type Struct struct {
StructName string // name of struct
Fld []Field // fields
Fields []Field // fields
}

// Field represents fields within a struct.
Expand All @@ -64,10 +64,10 @@ type Field struct {
// just mark it as deprecated with the `deprecated:"true"`
// tag, and change its Go type to struct{}.
//
Zid int64
FieldName string
TypStr string
OmitEmpty bool
Skip bool
Zid int64
FieldName string
FieldTypeStr string
OmitEmpty bool
Skip bool
// Tag map[string]string `msg:",omitempty"`
}
Loading

0 comments on commit 5826372

Please sign in to comment.