Exemplo n.º 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 !bytes.Equal(goStr, 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")
		}
	}
}
Exemplo n.º 2
0
func run(ctx *cli.Context) {
	if len(ctx.Args()) != 2 {
		cli.ShowAppHelp(ctx)
		os.Exit(1)
	}

	sourceDir := ctx.Args()[0]
	if sourceDir == "" {
		sourceDir = "."
	}

	outputDir := ctx.Args()[1]
	if outputDir == "" {
		outputDir = "output"
	}

	if err := generators.ParseTemplates(); err != nil {
		panic(err)
	}

	var schema *Schema

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

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

		fmt.Printf("Parsing file: %q\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)
	}

	if schema == nil {
		log.Fatal("Could not find schema, aborting.")
	}

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

	outputs := map[string][]byte{}
	funcs := map[string]func() ([]byte, error){
		".go":       schema.GenerateGo,
		".js":       schema.GenerateJs,
		"Client.go": schema.GenerateClient,
		"Client.py": schema.GeneratePythonClient,
	}

	for ext, fun := range funcs {
		str, err := fun()
		if err != nil {
			log.Fatalf("Error generating output for target %q: %v", ext, err)
		}

		outputs[schema.Name+ext] = []byte(str)
	}

	for fn, content := range outputs {
		target := path.Join(outputDir, fn)
		fmt.Printf("Generating file: %q\n", target)

		if err := ioutil.WriteFile(target, content, 0666); err != nil {
			log.Fatal(err)
		}
	}
}