github.com/Azure/draft-classic@v0.16.0/pkg/storage/storage.go (about)

     1  package storage
     2  
     3  // To regenerate the protocol buffer types for this package, run:
     4  //		go generate
     5  
     6  //go:generate make proto
     7  
     8  import (
     9  	"context"
    10  	b64 "encoding/base64"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  )
    14  
    15  // Deleter represents the delete APIs of the storage engine.
    16  type Deleter interface {
    17  	// DeleteBuilds deletes all draft builds for the application specified by appName.
    18  	DeleteBuilds(ctx context.Context, appName string) ([]*Object, error)
    19  	// DeleteBuild deletes the draft build given by buildID for the application specified by appName.
    20  	DeleteBuild(ctx context.Context, appName, buildID string) (*Object, error)
    21  }
    22  
    23  // Creator represents the create APIs of the storage engine.
    24  type Creator interface {
    25  	// CreateBuild creates and stores a new build.
    26  	CreateBuild(ctx context.Context, appName string, build *Object) error
    27  }
    28  
    29  // Updater represents the update APIs of the storage engine.
    30  type Updater interface {
    31  	// UpdateBuild creates and stores a new build.
    32  	UpdateBuild(ctx context.Context, appName string, build *Object) error
    33  }
    34  
    35  // Getter represents the retrieval APIs of the storage engine.
    36  type Getter interface {
    37  	// GetBuilds retrieves all draft builds from storage.
    38  	GetBuilds(ctx context.Context, appName string) ([]*Object, error)
    39  	// GetBuild retrieves the draft build by id from storage.
    40  	GetBuild(ctx context.Context, appName, buildID string) (*Object, error)
    41  }
    42  
    43  // Store represents a storage engine for application state stored by Draft.
    44  type Store interface {
    45  	Creator
    46  	Deleter
    47  	Updater
    48  	Getter
    49  }
    50  
    51  // EncodeToString returns the base64 encoding of a protobuf encoded storage.Object.
    52  //
    53  // err != nil if the protobuf marshaling fails; otherwise nil.
    54  func EncodeToString(obj *Object) (string, error) {
    55  	b, err := proto.Marshal(obj)
    56  	if err != nil {
    57  		return "", err
    58  	}
    59  	return b64.StdEncoding.EncodeToString(b), nil
    60  }
    61  
    62  // DecodeString returns the storage.Object decoded from a base64 encoded protobuf string.
    63  //
    64  // err != nil if decoding fails.
    65  func DecodeString(str string) (*Object, error) {
    66  	b, err := b64.StdEncoding.DecodeString(str)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	var obj Object
    71  	if err := proto.Unmarshal(b, &obj); err != nil {
    72  		return nil, err
    73  	}
    74  	return &obj, nil
    75  }