示例#1
0
// Simple test to parse json schema
func TestParseJsonSchema(t *testing.T) {
	if err := generators.ParseTemplates(); err != nil {
		t.Fatal(err)
	}

	dir, err := os.Open("testdata")
	if err != nil {
		t.Fatal(err)
	}

	dirnames, err := dir.Readdirnames(0)
	if err != nil {
		t.Fatal(err)
	}

	for _, name := range dirnames {
		t.Logf("Parsing suite %s", name)
		basepath := filepath.Join("testdata", name)
		input, err := ioutil.ReadFile(filepath.Join(basepath, "input.json"))
		if err != nil {
			t.Fatal(err)
		}

		// Parse the input json string
		schema, err := ParseSchema(input)
		if err != nil {
			t.Fatalf("Error parsing json schema. Err: %v", err)
		}

		// Generate the code
		goStr, err := schema.GenerateGo()
		if err != nil {
			t.Fatalf("Error generating go code. Err: %v", err)
		}

		output, err := ioutil.ReadFile(filepath.Join(basepath, "output.go"))
		if err != nil {
			t.Fatal(err)
		}

		if goStr != string(output) {
			if err := ioutil.WriteFile("/tmp/generated.go", []byte(goStr), 0666); err != nil {
				t.Fatalf("Err writing debug file `/tmp/generated.go` during failed test: %v", err)
			}
			fmt.Println("Generated code written to /tmp/generated.go")
			t.Fatal("Generated string from input was not equal to output string")
		}
	}
}
示例#2
0
func main() {
	flag.Parse()
	if err := generators.ParseTemplates(); err != nil {
		panic(err)
	}

	var schema *Schema

	// Parse all files in input directory
	err := filepath.Walk(*source, func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		// Ignore non-json files
		if filepath.Ext(path) != ".json" || filepath.Dir(path) != filepath.Dir(*source) {
			return nil
		}

		fmt.Printf("Parsing file: %s\n", path)

		b, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		// Parse the schema
		sch, err := ParseSchema(b)
		if err != nil {
			return err
		}

		// Append to global schema
		schema = MergeSchema(schema, sch)

		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

	outPath := "./"
	if *output != "" {
		outPath = *output
		if err := os.MkdirAll(outPath, 0755); err != nil {
			log.Fatalf("Error creating output directory: %v", err)
		}
	}

	// Generate Go code
	outStr, err := schema.GenerateGo()
	if err != nil {
		log.Errorf("Error generating go structs. Err: %v", err)
		// XXX fallthrough so we can write the files
	}

	// Write the Go file output
	goFileName := path.Join(outPath, schema.Name+".go")
	fmt.Printf("Writing to file: %s\n", goFileName)
	err = ioutil.WriteFile(goFileName, []byte(outStr), 0666)
	if err != nil {
		log.Fatal(err)
	}

	// Generate javascript
	outStr, err = schema.GenerateJs()
	if err != nil {
		log.Fatalf("Error generating javascript. Err: %v", err)
	}

	// Write javascript file
	jsFileName := path.Join(outPath, schema.Name+".js")
	fmt.Printf("Writing to file: %s\n", jsFileName)
	err = ioutil.WriteFile(jsFileName, []byte(outStr), 0666)
	if err != nil {
		log.Fatal(err)
	}

	// Generate go client
	outStr, err = schema.GenerateClient()
	if err != nil {
		log.Fatalf("Error generating go client. Err: %v", err)
	}

	// Write go client file
	goClientFileName := path.Join(outPath, schema.Name+"Client.go")
	fmt.Printf("Writing to file: %s\n", goClientFileName)
	err = ioutil.WriteFile(goClientFileName, []byte(outStr), 0666)
	if err != nil {
		log.Fatal(err)
	}

	// Generate python client
	outStr, err = schema.GeneratePythonClient()
	if err != nil {
		log.Fatalf("Error generating go client. Err: %v", err)
	}

	// Write python file
	pyClientFileName := path.Join(outPath, schema.Name+"Client.py")
	fmt.Printf("Writing to file: %s\n", pyClientFileName)
	err = ioutil.WriteFile(pyClientFileName, []byte(outStr), 0666)
	if err != nil {
		log.Fatal(err)
	}
}