types

package
v3.19.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 6 Imported by: 0

README

Types Plugin

The types plugin is a Goa plugin that generates Go data types and validation functions for all user types defined in a Goa design package.

Given the following design:

var _ = Type("MyType", func() {
        Description("My type")
        Attribute("age", Int, "Age", func() {
                Minimum(0)
        })
        Attribute("name", String, "Name")
        Required("age", "name")
})

The plugin generates the file gen/types/types.go with the following code:

package types

import 	goa "goa.design/goa/v3/pkg"

type (
        // My type
	MyType struct {
        	// Age
        	Age int
        	// Name
        	Name string
        }
)

// ValidateMyType runs the validations defined on MyType
func ValidateMyType(v *MyType) (err error) {
	if v.Age < 0 {
                err = goa.MergeErrors(err, goa.InvalidRangeError("v.age", v.Age, 0, true))
        }
	return
}

Usage Pattern

This plugin makes it possible to share data types between Goa microservices and other Go based processes such as daemons or adapters. A prototypical use case is a data pipeline where messages are sent to queues (AWS SQS, AWS Kinesis, Kafka, etc.) to be consumed by an adapter. The adapter can use the data structures generated by the types plugin to decode and validate messages. The adapter can then make requests to a Goa microservice whose design package imports the original design package defining the data types thus guaranteeing compatibility.

Enabling the Plugin

To enable the plugin simply import both the types package as follows:

import (
  . "goa.design/goa/v3/dsl"
  _ "goa.design/plugins/v3/types"
)

Note the use of blank identifier to import the types package which is necessary as the package is imported solely for its side-effects (initialization).

Effects on Code Generation

Enabling the plugin changes the behavior of the gen command of the goa tool. The command generates an additional types/types.go file under the gen folder containing the type definitions and validations.

Documentation

Index

Constants

View Source
const Gendir = "types"

Name of directory that contains generated types.

Variables

This section is empty.

Functions

func Generate

func Generate(genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error)

Generate produces the documentation JSON file.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL