func writeObject()

in mmv1/openapi_generate/parser.go [335:438]


func writeObject(name string, obj *openapi3.SchemaRef, objType openapi3.Types, urlParam bool) api.Type {
	var field api.Type

	switch name {
	case "projectsId", "project":
		// projectsId and project are omitted in MMv1 as they are inferred from
		// the presence of {{project}} in the URL
		return field
	case "locationsId":
		name = "location"
	}
	additionalDescription := ""

	if len(obj.Value.AllOf) > 0 {
		obj = obj.Value.AllOf[0]
		objType = *obj.Value.Type
	}

	field.Name = name
	switch objType[0] {
	case "string":
		field.Type = "String"
		if len(obj.Value.Enum) > 0 {
			var enums []string
			for _, enum := range obj.Value.Enum {
				enums = append(enums, fmt.Sprintf("%v", enum))
			}
			additionalDescription = fmt.Sprintf("\n Possible values:\n %s", strings.Join(enums, "\n"))
		}
	case "integer":
		field.Type = "Integer"
	case "number":
		field.Type = "Double"
	case "boolean":
		field.Type = "Boolean"
	case "object":
		if field.Name == "labels" {
			// Standard labels implementation
			field.Type = "KeyValueLabels"
			break
		}

		if obj.Value.AdditionalProperties.Schema != nil && obj.Value.AdditionalProperties.Schema.Value.Type.Is("string") {
			// AdditionalProperties with type string is a string -> string map
			field.Type = "KeyValuePairs"
			break
		}

		field.Type = "NestedObject"

		field.Properties = buildProperties(obj.Value.Properties, obj.Value.Required)
	case "array":
		field.Type = "Array"
		var subField api.Type
		typ := *obj.Value.Items.Value.Type
		switch typ[0] {
		case "string":
			subField.Type = "String"
		case "integer":
			subField.Type = "Integer"
		case "number":
			subField.Type = "Double"
		case "boolean":
			subField.Type = "Boolean"
		case "object":
			subField.Type = "NestedObject"
			subField.Properties = buildProperties(obj.Value.Items.Value.Properties, obj.Value.Items.Value.Required)
		}
		field.ItemType = &subField
	default:
		panic(fmt.Sprintf("Failed to identify field type for %s %s", field.Name, objType[0]))
	}

	description := fmt.Sprintf("%s %s", obj.Value.Description, additionalDescription)
	if strings.TrimSpace(description) == "" {
		description = "No description"
	}

	field.Description = trimDescription(description)

	if urlParam {
		field.UrlParamOnly = true
		field.Required = true
	}

	// These methods are only available when the field is set
	if obj.Value.ReadOnly {
		field.Output = true
	}

	// x-google-identifier fields are described by AIP 203 and are represented
	// as output only in Terraform.
	xGoogleId, err := obj.JSONLookup("x-google-identifier")
	if err == nil && xGoogleId != nil {
		field.Output = true
	}

	xGoogleImmutable, err := obj.JSONLookup("x-google-immutable")
	if err == nil && xGoogleImmutable != nil {
		field.Immutable = true
	}

	return field
}