Ejemplo n.º 1
0
func (api *API) postPopulate(apiDef *APIDefinition) {

	// make sure each entry defined for this API has a unique generated method name
	methods := make(map[string]bool)

	for i := range api.Entries {
		api.Entries[i].Parent = api
		api.Entries[i].MethodName = utils.NormaliseLower(api.Entries[i].Name, methods)
		api.Entries[i].postPopulate(apiDef)
	}
}
Ejemplo n.º 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
		}
	}
}