gocloudurls

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2019 License: Apache-2.0 Imports: 7 Imported by: 3

README

gocloudurls

GoDoc

gocloudurls package is a helper for gocloud.dev.

Now it provides three functions for

  • PubSub
  • DocStore
  • Blob

Purpose

It makes configuration easy for gocloud.dev. gocloud.dev requires special form of URLs to specify cloud resources. This package normalize more human readable/writable config values into gocloud.dev ones.

Functions

func NormalizeBlobURL(srcUrl string) (string, error)

It normalize shorter version of blob URLs into gocloud.dev acceptable URLs.

Examples:

  • memmem://
  • folderfile://folder
  • s3://my-buckets3://my-bucket?region=us-west-1

It gets AWS region name form AWS_REGION environment variable that is acceptable in AWS Lambda.

MustNormalizeBlobURL raise panic if there is error.

func NormalizePubSubURL(srcUrl string) (string, error)

It normalizes shorter version of PubSub/SQS/SNS identifier into gocloud.dev acceptable URLs.

Examples:

gocloudurls.NormalizePubSubURL("arn:aws:sns:us-east-2:123456789012:mytopic")
// "awssns:///arn:aws:sns:us-east-2:123456789012:mytopic?region=us-east-2"
gocloudurls.NormalizePubSubURL("https://sqs.us-east-2.amazonaws.com/123456789012/myqueue")
// "awssqs://https://sqs.us-east-2.amazonaws.com/123456789012/myqueue?region=us-east-2"
gocloudurls.NormalizePubSubURL("gcppubsub://myproject/mytopic")
// "gcppubsub://projects/myproject/topics/mytopic"

MustNormalizePubSubURL raise panic if there is error.

func NormalizeDocStoreURL(srcUrl string, opt Option) (string, error)
type Option struct {
	KeyName      string
	PartitionKey string
	Collection   string
}

Usually, application uses multiple document collections (≒ table in RDB). So it provides API to replace collection name by application code (config specify until DB location).

Default KeyName is "_id" as same as MongoDB.

If PartitionKey is specified for DynamoDB, KeyName is specified as sort_key. This config is ignored for other DocStores.

Examples:

goclodurls.NormalizePubSubURL("mem://", goclodurls.Option{
    Collection: "addresses",
})
// "mem://addresses/_id"
goclodurls.NormalizePubSubURL("firestore://my-project", goclodurls.Option{
    Collection: "addresses",
})
// "firestore://projects/my-project/databases/(default)/documents/addresses?name_field=_id"
goclodurls.NormalizePubSubURL("firestore://my-project/my-documents/addresses", goclodurls.Option{})
// "firestore://projects/my-project/databases/my-documents/documents/addresses?name_field=_id"
goclodurls.NormalizePubSubURL("dynamodb://", goclodurls.Option{
    Collection: "tasks",
})
// "dynamodb://tasks?partition_key=_id"
goclodurls.NormalizePubSubURL("dynamodb://", goclodurls.Option{
    Collection:   "tasks",
    PartitionKey: "job_id"
})
// "dynamodb://tasks?partition_key=job_id&sort_key=_id"

MustNormalizeDocStoreURL raise panic if there is error.

Struct

DynamoDBSchema creates AWS CLI command to create table.

For example you makes the following struct to handle DynamoDB record:

type Person struct {
   Name string `docstore:"name"`
   Age  int
}

You can get AWS command options:

ds, err := NewDynamoDBSchema(&Person{}, MustMustNormalizeDocStoreURL("dynamodb://persons"))
ds.CreateTableCommand()
// It returns slice of string.
// "aws", "dynamodb", "create-table", "--table-name", "persons",
// "--attribute-definitions", "AttributeName=name,AttributeType=S",
// "--key-schema", "AttributeName=name,KeyType=HASH",
// "--provisioned-throughput", "ReadCapacityUnits=5,WriteCapacityUnits=5",

If you can modify Read/Write capacity unis, you can use SchemaOption:

ds.CreateTableCommand(SchemaOption{
    ReadCapacityUnits: 10,
    WriteCapacityUnits: 10,
})

License

Apache 2

Documentation

Overview

package gocloudurls helps gocloud.dev packages.

gocloud.dev uses URL to initialize cloud resources like blob, docstore, pubsub and so on. This package normalize URLs of that:

snsSrcPath := "arn:aws:sns:us-east-2:123456789012:mytopic"
snsPath, err := gocloudurls.NormalizePubSubURL(snsSrcPath)
// -> "awssns:///arn:aws:sns:us-east-2:123456789012:mytopic?region=us-east-2"
topic, err := pubsub.OpenTopic(ctx, snsPath)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustNormalizeBlobURL

func MustNormalizeBlobURL(srcUrl string, environ []string) string

MustNormalizeBlobURL is similar to NormalizeBlobURL but raise panic if there is error

func MustNormalizeDocStoreURL

func MustNormalizeDocStoreURL(srcUrl string, opt ...Option) string

MustNormalizeDocStoreURL is similar to NormalizeDocStoreURL but raise panic if there is error

func MustNormalizePubSubURL

func MustNormalizePubSubURL(srcUrl string) string

MustNormalizePubSubURL is similar to NormalizePubSubURL but raise panic if there is error

func NormalizeBlobURL

func NormalizeBlobURL(srcUrl string, environ []string) (string, error)

NormalizeBlobURL normalize blob URL. environ assumes os.Environ().

When region is not specified for S3, this function gets region information from AWS_REGION environment variable.

If "mem" is specified, it returns "memblob" URL. It other names specified, it returns fileblob URL.

Example:

  • “mem“ → “mem://“
  • “folder“ → “file://folder
  • “s3://my-bucket“ → “s3://my-bucket?region=us-west-1“

func NormalizeDocStoreURL

func NormalizeDocStoreURL(srcUrl string, opt ...Option) (string, error)

NormalizeDocStoreURL normalizes Document Store URL

Usually, application uses multiple document collections (≒ table in RDB). So it provides API to replace Collection name by application code (config specify until DB location).

Default “KeyName“ is “"_id"“ as same as MongoDB.

If “PartitionKey“ is specified for DynamoDB, “KeyName“ is specified as “sort_key“. This config is ignored for other DocStores.

Examples:

goclodurls.NormalizePubSubURL("mem://", goclodurls.Option{
    Collection: "addresses",
})
// "mem://addresses/_id"

goclodurls.NormalizePubSubURL("firestore://my-project", goclodurls.Option{
    Collection: "addresses",
})
// "firestore://projects/my-project/databases/(default)/documents/addresses?name_field=_id"

goclodurls.NormalizePubSubURL("firestore://my-project/my-documents/addresses", goclodurls.Option{})
// "firestore://projects/my-project/databases/my-documents/documents/addresses?name_field=_id"

goclodurls.NormalizePubSubURL("dynamodb://", goclodurls.Option{
    Collection: "tasks",
})
// "dynamodb://tasks?partition_key=_id"

goclodurls.NormalizePubSubURL("dynamodb://", goclodurls.Option{
    Collection:   "tasks",
    PartitionKey: "job_id"
})
// "dynamodb://tasks?partition_key=job_id&sort_key=_id"

func NormalizePubSubURL

func NormalizePubSubURL(srcUrl string) (string, error)

NormalizePubSubURL normalize URL for PubSub.

Examples:

Types

type DynamoDBSchema

type DynamoDBSchema struct {
	Collection        string
	PartitionKeyField *Field
	SortKeyField      *Field
}

DynamoDBSchema creates AWS CLI command to create table.

For example you makes the following struct to handle DynamoDB record:

  type Person struct {
	     Name string `docstore:"name"`
	     Age  int
  }

  ds, err := NewDynamoDBSchema(&Person{}, MustMustNormalizeDocStoreURL("dynamodb://persons"))
  ds.CreateTableCommand()
  // returns slice of string.
  // "aws", "dynamodb", "create-table", "--table-name", "persons",
	 // "--attribute-definitions", "AttributeName=name,AttributeType=S",
	 // "--key-schema", "AttributeName=name,KeyType=HASH",
	 // "--provisioned-throughput", "ReadCapacityUnits=5,WriteCapacityUnits=5",

func NewDynamoDBSchema

func NewDynamoDBSchema(collectionEntity interface{}, urlString string) (*DynamoDBSchema, error)

NewDynamoDBSchema creates DynamoDBSchema from urlString(it should be a return of NormalizeDocStoreURL), CollectionEntity struct.

func (DynamoDBSchema) CreateTableCommand

func (d DynamoDBSchema) CreateTableCommand(opt ...SchemaOption) []string

CreateTableCommand returns command line to create table.

type Field

type Field struct {
	Name string
	Type string
}

type Option

type Option struct {
	KeyName       string
	PartitionKey  string
	Collection    string
	FileName      string
	RevisionField string
}

Option is a option for NormalizeDocStoreURL

KeyName is a primary key of document store. Default value is _id.

PartitionKey is only for DynamoDB. If this parameter is specified, KeyName is used as a sort key. If PartitionKey is not specified, KeyName is used as a partitionKey.

If Collection is specified, it returns URL for the Collection. It is good for applications that uses multiple collections.

type SchemaOption

type SchemaOption struct {
	ReadCapacityUnits  int
	WriteCapacityUnits int
}

Jump to

Keyboard shortcuts

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