gcp

package
v1.29.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package gcp provides Google Cloud helpers for mygoto.me service.

This file contains helpers for interacting with BigQuery. It includes functions to authenticate using the default service account, create datasets and tables when they do not exist and stream rows using the BigQuery streaming API. All operations log errors via the common logging helpers and streaming inserts are retried once when failures occur.

Package gcp provides Google Cloud helpers. This file defines helpers for retrieving results from Cloud Datastore queries.

Package gcp contains helpers backed by Google Cloud services. This file implements datastore-backed storage of authenticated users and their roles.

Index

Constants

This section is empty.

Variables

View Source
var VERSION string

VERSION stores the deployed application version retrieved from App Engine. It is set by the Version helper in appengine.go.

Functions

func B2S

func B2S(b []byte) string

b2s converts a byte slice possibly containing a null terminator into a string. It mirrors common.B2S but is duplicated here to avoid a package dependency.

func CreateDatasetIfNotExists

func CreateDatasetIfNotExists(c context.Context, projectID, datasetID string) error

CreateDatasetIfNotExists ensures the given dataset exists. It first attempts to retrieve the dataset and if it receives a 404 error, a new dataset is created. All errors are logged and returned to the caller.

func CreateTableInBigQuery

func CreateTableInBigQuery(c context.Context, newTable *bigquery.Table) error

CreateTableInBigQuery deletes any existing table with the same ID and then creates the provided table definition. Deletion errors are logged but do not stop table creation. The caller is expected to provide a table with a valid TableReference and Schema.

func Debug

func Debug(format string, v ...interface{})

Debug writes a formatted debug message. This is a lightweight replacement for the helpers in the parent package to avoid an import cycle.

func DeleteMemCache

func DeleteMemCache(c context.Context, key string) (err error)

DeleteMemCache removes a key from memcache. memcache.ErrCacheMiss is ignored so callers can treat missing keys as a no-op. Any other error is logged and returned.

func Error

func Error(format string, v ...interface{})

Error writes a formatted error message prefixed with "ERROR:".

func GetBQServiceAccountClient

func GetBQServiceAccountClient(c context.Context) (*bigquery.Service, error)

GetBQServiceAccountClient returns an authenticated BigQuery service using the default service account credentials. Errors encountered while creating the client are logged and returned to the caller.

func GetFirst

func GetFirst(c context.Context, q *datastore.Query, dst interface{}) (*datastore.Key, error)

GetFirst executes the query and loads the first entity into dst.

Parameters:

c - context used for datastore calls.
q - query to run.
dst - pointer that will receive the entity data.

Returns the entity key if one was found. When the query yields no results, the key is nil and the error is datastore.ErrNoSuchEntity. Any other error encountered when fetching the first result is returned as-is. The datastore.Done error from t.Next is converted to datastore.ErrNoSuchEntity so callers can easily detect the no-results case.

func GetMemCache

func GetMemCache(c context.Context, key string) ([]byte, error)

GetMemCache retrieves raw bytes from memcache. When the key is missing, memcache.ErrCacheMiss is returned with an empty slice. Other errors are logged and returned with an empty slice.

func GetMemCacheString

func GetMemCacheString(c context.Context, key string) string

GetMemCacheString returns the cached string for the provided key. An empty string is returned when the key is missing or an error occurs.

Example:

name := GetMemCacheString(ctx, "user-name")

func GetObjMemCache

func GetObjMemCache(c context.Context, key string, v interface{}) error

GetObjMemCache retrieves an object stored with SetObjMemCache. memcache.ErrCacheMiss is returned when the key is missing. Any other error is returned as-is.

func GetUserRole

func GetUserRole(c context.Context, email string) (string, error)

GetUserRole fetches and returns the role for the user with the given email. It returns datastore.ErrNoSuchEntity if the user is not found.

func Info

func Info(format string, v ...interface{})

Info writes a formatted informational message.

func SetMemCache

func SetMemCache(c context.Context, key string, item []byte, hours int32)

SetMemCache stores bytes in memcache for the specified number of hours. If the key already exists it is overwritten. Errors are logged.

func SetMemCacheString

func SetMemCacheString(c context.Context, key string, item string, hours int32)

SetMemCacheString stores the provided string in memcache for the given hours. Existing values are overwritten.

Example:

SetMemCacheString(ctx, "user-name", "gopher", 2)

func SetObjMemCache

func SetObjMemCache(c context.Context, key string, v interface{}, hours int32) error

SetObjMemCache stores an arbitrary object in memcache for the given duration. Do not store sensitive data here unless it is encrypted as the cache is accessible by other applications running in the same environment.

func StreamDataInBigquery

func StreamDataInBigquery(c context.Context, projectId, datasetId, tableId string, req *bigquery.TableDataInsertAllRequest) error

StreamDataInBigquery inserts rows into a BigQuery table using the streaming API. If the first attempt fails, the function waits 10 seconds and retries once. Errors from each attempt are logged and the error from the second attempt is returned.

func Version

func Version(c context.Context) string

Version retrieves the App Engine version ID from context and stores the major component in the exported `common.VERSION` variable. This helper should be called once during initialization so callers can read `common.VERSION` throughout the application.

Types

type User

type User struct {
	Email   string    // unique user identifier and datastore key
	Role    string    // current user role, e.g. "admin" or "organizer"
	Created time.Time // timestamp when the entity was first stored
}

User represents an authenticated user. User represents an authenticated user stored in the datastore.

func EnsureUserExists

func EnsureUserExists(c context.Context, email, role string) (*User, error)

EnsureUserExists retrieves a user from the datastore by email. If the user does not exist a new entity is created with the provided role. If the user exists but the role differs, the stored role is updated. The resulting user entity is returned.

Jump to

Keyboard shortcuts

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