Goa v3 では design に使うプリミティブ型が v1 から変更になっています。この記事では Swagger データ型も交えながら v1 と v3 のプリミティブ型を比較してみます。
v1 のプリミティブ型
出典: https://godoc.org/gopkg.in/goadesign/goa.v1/design#pkg-constants
const ( // Boolean is the type for a JSON boolean. Boolean = Primitive(BooleanKind) // Integer is the type for a JSON number without a fraction or exponent part. Integer = Primitive(IntegerKind) // Number is the type for any JSON number, including integers. Number = Primitive(NumberKind) // String is the type for a JSON string. String = Primitive(StringKind) // DateTime is the type for a JSON string parsed as a Go time.Time // DateTime expects an RFC3339 formatted value. DateTime = Primitive(DateTimeKind) // UUID is the type for a JSON string parsed as a Go uuid.UUID // UUID expects an RFC4122 formatted value. UUID = Primitive(UUIDKind) // Any is the type for an arbitrary JSON value (interface{} in Go). Any = Primitive(AnyKind) // File is the type for a file. This type can only be used in a multipart definition. File = Primitive(FileKind) )
v3 のプリミティブ型
出典: https://godoc.org/goa.design/goa/expr#pkg-constants
const ( // Boolean is the type for a JSON boolean. Boolean = Primitive(BooleanKind) // Int is the type for a signed integer. Int = Primitive(IntKind) // Int32 is the type for a signed 32-bit integer. Int32 = Primitive(Int32Kind) // Int64 is the type for a signed 64-bit integer. Int64 = Primitive(Int64Kind) // UInt is the type for an unsigned integer. UInt = Primitive(UIntKind) // UInt32 is the type for an unsigned 32-bit integer. UInt32 = Primitive(UInt32Kind) // UInt64 is the type for an unsigned 64-bit integer. UInt64 = Primitive(UInt64Kind) // Float32 is the type for a 32-bit floating number. Float32 = Primitive(Float32Kind) // Float64 is the type for a 64-bit floating number. Float64 = Primitive(Float64Kind) // String is the type for a JSON string. String = Primitive(StringKind) // Bytes is the type for binary data. Bytes = Primitive(BytesKind) // Any is the type for an arbitrary JSON value (interface{} in Go). Any = Primitive(AnyKind) )
Swagger データ型
出典: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types
Common Name | type |
format |
Comments |
---|---|---|---|
integer | integer |
int32 |
signed 32 bits |
long | integer |
int64 |
signed 64 bits |
float | number |
float |
|
double | number |
double |
|
string | string |
||
byte | string |
byte |
base64 encoded characters |
binary | string |
binary |
any sequence of octets |
boolean | boolean |
||
date | string |
date |
As defined by full-date - RFC3339 |
dateTime | string |
date-time |
As defined by date-time - RFC3339 |
password | string |
password |
Used to hint UIs the input needs to be obscured. |
比較
v3 は gRPC 対応のため v1 より多くの型を用意しています。 DateTime
と UUID
は廃止されましたが Format
DSL を使用して同等の定義を記述できますし、より多くのフォーマットも用意されています。
v1 | v3 | Swagger Common Name | Swagger type |
Swagger format |
---|---|---|---|---|
Boolean |
Boolean |
boolean | boolean |
|
Integer |
Int |
long | integer |
int64 |
Int32 |
integer | integer |
int32 |
|
Int64 |
long | integer |
int64 |
|
UInt |
long | integer |
int64 |
|
UInt32 |
integer | integer |
int32 |
|
UInt64 |
long | integer |
int64 |
|
Number |
Float32 |
float | number |
float |
Float64 |
double | number |
double |
|
String |
String |
string | string |
|
DateTime |
String + Format(FormatDateTime) |
dateTime | string |
date-time |
UUID |
String + Format(FormatUUID) |
string |
uuid |
|
String + Format(FormatDate) |
date | string |
date |
|
String + Format(FormatEmail) |
string |
email |
||
String + Format(FormatHostname) |
string |
hostname |
||
String + Format(FormatIPv4) |
string |
ipv4 |
||
String + Format(FormatIPv6) |
string |
ipv6 |
||
String + Format(FormatIP) |
string |
ip |
||
String + Format(FormatURI) |
string |
uri |
||
String + Format(FormatMAC) |
string |
mac |
||
String + Format(FormatCIDR) |
string |
cidr |
||
String + Format(FormatRegexp) |
string |
regexp |
||
String + Format(FormatJSON) |
string |
json |
||
String + Format(FormatRFC1123) |
string |
rfc1123 |
||
Bytes |
byte | string |
byte |
|
Any |
Any |
any |
||
File |
file |