예제 #1
0
// GenerateCode takes the objects loaded into memory in LoadAPIs
// and writes them out as go code.
func GenerateCode(goOutputDir, modelData string) {
	for i := range apiDefs {
		apiDefs[i].PackageName = strings.ToLower(apiDefs[i].Name)
		apiDefs[i].PackagePath = filepath.Join(goOutputDir, "src", "main", "java", "org", "mozilla", "taskcluster", "client", apiDefs[i].PackageName)
		err = os.MkdirAll(apiDefs[i].PackagePath, 0755)
		utils.ExitOnFail(err)
		content := `// The following code is AUTO-GENERATED. Please DO NOT edit.
//
// This package was generated from the schema defined at
// ` + apiDefs[i].URL + `
`
		generatePayloadTypes(&apiDefs[i])
		content += apiDefs[i].generateAPICode()
		className := strings.Title(apiDefs[i].Name)
		sourceFile := filepath.Join(apiDefs[i].PackagePath, className+".java")
		fmt.Println("Generating source code " + sourceFile + "...")
		utils.WriteStringToFile(content, sourceFile)
		utils.ExitOnFail(err)
	}

	content := "The following file is an auto-generated static dump of the API models at time of code generation.\n"
	content += "It is provided here for reference purposes, but is not used by any code.\n"
	content += "\n"
	for i := range apiDefs {
		content += utils.Underline(apiDefs[i].URL)
		content += apiDefs[i].Data.String() + "\n\n"
		for _, url := range apiDefs[i].schemas.SortedSanitizedURLs() {
			content += (utils.Underline(url))
			content += apiDefs[i].schemas.SubSchema(url).String() + "\n\n"
		}
	}
	utils.WriteStringToFile(content, modelData)
}
예제 #2
0
// This is where we generate nested and compoound types in go to represent json payloads
// which are used as inputs and outputs for the REST API endpoints, and also for Pulse
// message bodies for the Exchange APIs.
// Returns the generated code content, and a map of keys of extra packages to import, e.g.
// a generated type might use time.Time, so if not imported, this would have to be added.
// using a map of strings -> bool to simulate a set - true => include
func generatePayloadTypes(apiDef *APIDefinition) {

	job := &jsonschema2go.Job{
		Package:     apiDef.PackageName,
		URLs:        apiDef.schemaURLs,
		SkipCodeGen: true,
		TypeNameGenerator: func(name string, exported bool, blacklist map[string]bool) (identifier string) {
			return utils.Normalise(name, blacklist)
		},
		MemberNameGenerator: func(name string, exported bool, blacklist map[string]bool) (identifier string) {
			return utils.NormaliseLower(name, blacklist)
		},
	}
	result, err := job.Execute()
	utils.ExitOnFail(err)

	apiDef.schemas = result.SchemaSet

	for _, i := range apiDef.schemas.SortedSanitizedURLs() {
		extraPackages := make(map[string]bool)
		content := "package org.mozilla.taskcluster.client." + strings.ToLower(apiDef.PackageName) + ";\n"
		content += "\n"
		s := JsonSubSchema(*apiDef.schemas.SubSchema(i))
		typeClass, typeComment, typ := (&s).TypeDefinition(0, extraPackages)
		if typeClass != "" {
			className := typ
			if strings.HasSuffix(typ, "[]") {
				className = typ[:len(typ)-2]
				apiDef.schemas.SubSchema(i).TypeName = typ
			}
			if len(extraPackages) > 0 {
				for pckage := range extraPackages {
					content += "import " + pckage + ";\n"
				}
				content += "\n"
			}
			content += typeComment
			content += typeClass[1:]
			utils.WriteStringToFile(content, filepath.Join(apiDef.PackagePath, className+".java"))
		} else {
			apiDef.schemas.SubSchema(i).TypeName = typ
		}
	}
}