types

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2020 License: Apache-2.0 Imports: 9 Imported by: 59

Documentation

Overview

Package types implements the CloudEvents type system.

CloudEvents defines a set of abstract types for event context attributes. Each type has a corresponding native Go type and a canonical string encoding. The native Go types used to represent the CloudEvents types are: bool, int32, string, []byte, *url.URL, time.Time

+----------------+----------------+-----------------------------------+
|CloudEvents Type|Native Type     |Convertible From                   |
+================+================+===================================+
|Bool            |bool            |bool                               |
+----------------+----------------+-----------------------------------+
|Integer         |int32           |Any numeric type with value in     |
|                |                |range of int32                     |
+----------------+----------------+-----------------------------------+
|String          |string          |string                             |
+----------------+----------------+-----------------------------------+
|Binary          |[]byte          |[]byte                             |
+----------------+----------------+-----------------------------------+
|URI-Reference   |*url.URL        |url.URL, types.URIRef, types.URI   |
+----------------+----------------+-----------------------------------+
|URI             |*url.URL        |url.URL, types.URIRef, types.URI   |
|                |                |Must be an absolute URI.           |
+----------------+----------------+-----------------------------------+
|Timestamp       |time.Time       |time.Time, types.Timestamp         |
+----------------+----------------+-----------------------------------+

Extension attributes may be stored as a native type or a canonical string. The To<Type> functions will convert to the desired <Type> from any convertible type or from the canonical string form.

The Parse<Type> and Format<Type> functions convert native types to/from canonical strings.

Note are no Parse or Format functions for URL or string. For URL use the standard url.Parse() and url.URL.String(). The canonical string format of a string is the string itself.

Example
package main

import (
	"fmt"
	"math"
	"time"

	"github.com/cloudevents/sdk-go/pkg/cloudevents/types"
)

func main() {
	// Handle a time value that may be in native or canonical string form.
	printTime := func(v interface{}) {
		t, err := types.ToTime(v)
		fmt.Printf("%v %v\n", t, err)
	}
	printTime(time.Date(1969, 3, 21, 12, 24, 0, 0, time.UTC))
	printTime("2020-03-21T12:34:56.78Z")

	// Convert numeric values to common 32-bit integer form
	printInt := func(v interface{}) {
		i, err := types.ToInteger(v)
		fmt.Printf("%v %v\n", i, err)
	}
	printInt(123.456)
	printInt("456")
	printInt(int64(99999))
	// But not illegal or out-of-range values
	printInt(math.MaxInt32 + 1)
	printInt("not an int")

}
Output:

1969-03-21 12:24:00 +0000 UTC <nil>
2020-03-21 12:34:56.78 +0000 UTC <nil>
123 <nil>
456 <nil>
99999 <nil>
0 cannot convert 2147483648 to int32: out of range
0 strconv.ParseFloat: parsing "not an int": invalid syntax

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Allocate

func Allocate(obj interface{}) (asPtr interface{}, asValue reflect.Value)

Allocate allocates a new instance of type t and returns: asPtr is of type t if t is a pointer type and of type &t otherwise asValue is a Value of type t pointing to the same data as asPtr

func Format added in v0.10.0

func Format(v interface{}) (string, error)

Format returns the canonical string format of v, where v can be any type that is convertible to a CloudEvents type.

func FormatBinary added in v0.10.0

func FormatBinary(v []byte) string

FormatBinary returns canonical string format: standard base64 encoding

func FormatBool added in v0.10.0

func FormatBool(v bool) string

FormatBool returns canonical string format: "true" or "false"

func FormatInteger added in v0.10.0

func FormatInteger(v int32) string

FormatInteger returns canonical string format: decimal notation.

func FormatTime added in v0.10.0

func FormatTime(v time.Time) string

FormatTime returns canonical string format: RFC3339 with nanoseconds

func ParseBinary added in v0.10.0

func ParseBinary(v string) ([]byte, error)

ParseBinary parse canonical string format: standard base64 encoding

func ParseBool added in v0.10.0

func ParseBool(v string) (bool, error)

ParseBool parse canonical string format: "true" or "false"

func ParseInteger added in v0.10.0

func ParseInteger(v string) (int32, error)

ParseInteger parse canonical string format: decimal notation.

func ParseTime added in v0.10.0

func ParseTime(v string) (time.Time, error)

ParseTime parse canonical string format: RFC3339 with nanoseconds

func ToBinary added in v0.10.0

func ToBinary(v interface{}) ([]byte, error)

ToBinary returns a []byte value, decoding from base64 string if necessary.

func ToBool added in v0.10.0

func ToBool(v interface{}) (bool, error)

ToBool accepts a bool value or canonical "true"/"false" string.

func ToInteger added in v0.10.0

func ToInteger(v interface{}) (int32, error)

ToInteger accepts any numeric value in int32 range, or canonical string.

func ToString added in v0.10.0

func ToString(v interface{}) (string, error)

ToString returns a string value unaltered.

This function does not perform canonical string encoding, use one of the Format functions for that.

func ToTime added in v0.10.0

func ToTime(v interface{}) (time.Time, error)

ToTime returns a time.Time value, parsing from RFC3339 string if necessary.

func ToURL added in v0.10.0

func ToURL(v interface{}) (*url.URL, error)

ToURL returns a *url.URL value, parsing from string if necessary.

func Validate added in v0.10.0

func Validate(v interface{}) (interface{}, error)

Validate v is a valid CloudEvents attribute value, convert it to one of:

bool, int32, string, []byte, types.URI, types.URIRef, types.Timestamp

Types

type ConvertErr added in v0.10.0

type ConvertErr struct {
	// Value being converted
	Value interface{}
	// Type of attempted conversion
	Type reflect.Type
	// contains filtered or unexported fields
}

func (*ConvertErr) Error added in v0.10.0

func (e *ConvertErr) Error() string

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp wraps time.Time to normalize the time layout to RFC3339. It is intended to enforce compliance with the CloudEvents spec for their definition of Timestamp. Custom marshal methods are implemented to ensure the outbound Timestamp is a string in the RFC3339 layout.

func ParseTimestamp

func ParseTimestamp(s string) (*Timestamp, error)

ParseTimestamp attempts to parse the given time assuming RFC3339 layout

func (*Timestamp) MarshalJSON

func (t *Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements a custom json marshal method used when this type is marshaled using json.Marshal.

func (*Timestamp) MarshalXML

func (t *Timestamp) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements a custom xml marshal method used when this type is marshaled using xml.Marshal.

func (Timestamp) String

func (t Timestamp) String() string

String outputs the time using RFC3339 format.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json unmarshal method used when this type is unmarshaled using json.Unmarshal.

func (*Timestamp) UnmarshalXML

func (t *Timestamp) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml unmarshal method used when this type is unmarshaled using xml.Unmarshal.

type URI added in v0.10.0

type URI struct {
	url.URL
}

URI is a wrapper to url.URL. It is intended to enforce compliance with the CloudEvents spec for their definition of URI. Custom marshal methods are implemented to ensure the outbound URI object is a flat string.

func ParseURI added in v0.10.0

func ParseURI(u string) *URI

ParseURI attempts to parse the given string as a URI.

func (URI) MarshalJSON added in v0.10.0

func (u URI) MarshalJSON() ([]byte, error)

MarshalJSON implements a custom json marshal method used when this type is marshaled using json.Marshal.

func (URI) MarshalXML added in v0.10.0

func (u URI) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements a custom xml marshal method used when this type is marshaled using xml.Marshal.

func (*URI) String added in v0.10.0

func (u *URI) String() string

String returns the full string representation of the URI-Reference.

func (*URI) UnmarshalJSON added in v0.10.0

func (u *URI) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json unmarshal method used when this type is unmarshaled using json.Unmarshal.

func (*URI) UnmarshalXML added in v0.10.0

func (u *URI) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml unmarshal method used when this type is unmarshaled using xml.Unmarshal.

type URIRef added in v0.10.0

type URIRef struct {
	url.URL
}

URIRef is a wrapper to url.URL. It is intended to enforce compliance with the CloudEvents spec for their definition of URI-Reference. Custom marshal methods are implemented to ensure the outbound URIRef object is is a flat string.

func ParseURIRef added in v0.10.0

func ParseURIRef(u string) *URIRef

ParseURIRef attempts to parse the given string as a URI-Reference.

func (URIRef) MarshalJSON added in v0.10.0

func (u URIRef) MarshalJSON() ([]byte, error)

MarshalJSON implements a custom json marshal method used when this type is marshaled using json.Marshal.

func (URIRef) MarshalXML added in v0.10.0

func (u URIRef) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements a custom xml marshal method used when this type is marshaled using xml.Marshal.

func (*URIRef) String added in v0.10.0

func (u *URIRef) String() string

String returns the full string representation of the URI-Reference.

func (*URIRef) UnmarshalJSON added in v0.10.0

func (u *URIRef) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json unmarshal method used when this type is unmarshaled using json.Unmarshal.

func (*URIRef) UnmarshalXML added in v0.10.0

func (u *URIRef) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml unmarshal method used when this type is unmarshaled using xml.Unmarshal.

type URLRef

type URLRef struct {
	url.URL
}

URLRef is a wrapper to url.URL. It is intended to enforce compliance with the CloudEvents spec for their definition of URI-Reference. Custom marshal methods are implemented to ensure the outbound URLRef object is is a flat string.

deprecated: use URIRef.

func ParseURLRef

func ParseURLRef(u string) *URLRef

ParseURLRef attempts to parse the given string as a URI-Reference.

func (URLRef) MarshalJSON

func (u URLRef) MarshalJSON() ([]byte, error)

MarshalJSON implements a custom json marshal method used when this type is marshaled using json.Marshal.

func (URLRef) MarshalXML

func (u URLRef) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements a custom xml marshal method used when this type is marshaled using xml.Marshal.

func (*URLRef) String

func (u *URLRef) String() string

String returns the full string representation of the URI-Reference.

func (*URLRef) UnmarshalJSON

func (u *URLRef) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json unmarshal method used when this type is unmarshaled using json.Unmarshal.

func (*URLRef) UnmarshalXML

func (u *URLRef) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml unmarshal method used when this type is unmarshaled using xml.Unmarshal.

Jump to

Keyboard shortcuts

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