Skip to content

Commit

Permalink
GOCBC-715: Updated all public structures to not be tagged.
Browse files Browse the repository at this point in the history
Motivation
----------
The SDK should not arbitrarily tag its exported structures
with JSON tags, but instead use internal structures to
decode.

Changes
-------
Updated all public structures to have no tags.

Change-Id: I505d38021cd1a4bf20a83df226aad42fe9e06086
Reviewed-on: http://review.couchbase.org/120366
Reviewed-by: Brett Lawson <brett19@gmail.com>
Tested-by: Brett Lawson <brett19@gmail.com>
  • Loading branch information
brett19 committed Jan 19, 2020
1 parent 2ebb423 commit d44b5ae
Show file tree
Hide file tree
Showing 13 changed files with 618 additions and 500 deletions.
5 changes: 2 additions & 3 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ type Bucket struct {
sb stateBlock
}

// BucketOptions are the options available when connecting to a Bucket.
type BucketOptions struct {
type bucketOptions struct {
DisableMutationTokens bool
}

func newBucket(sb *stateBlock, bucketName string, opts BucketOptions) *Bucket {
func newBucket(sb *stateBlock, bucketName string, opts bucketOptions) *Bucket {
return &Bucket{
sb: stateBlock{
clientStateBlock: clientStateBlock{
Expand Down
36 changes: 18 additions & 18 deletions bucket_collectionsmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ import (
"github.com/couchbase/gocbcore/v8"
)

// CollectionManager provides methods for performing collections management.
type CollectionManager struct {
httpClient httpProvider
bucketName string
globalTimeout time.Duration
defaultRetryStrategy *retryStrategyWrapper
tracer requestTracer
}

// CollectionSpec describes the specification of a collection.
type CollectionSpec struct {
Name string
Expand All @@ -34,21 +25,30 @@ type ScopeSpec struct {
}

// These 3 types are temporary. They are necessary for now as the server beta was released with ns_server returning
// a different manifest format to what it will return in the future.
type manifest struct {
UID uint64 `json:"uid"`
Scopes map[string]manifestScope `json:"scopes"`
// a different jsonManifest format to what it will return in the future.
type jsonManifest struct {
UID uint64 `json:"uid"`
Scopes map[string]jsonManifestScope `json:"scopes"`
}

type manifestScope struct {
UID uint32 `json:"uid"`
Collections map[string]manifestCollection `json:"collections"`
type jsonManifestScope struct {
UID uint32 `json:"uid"`
Collections map[string]jsonManifestCollection `json:"collections"`
}

type manifestCollection struct {
type jsonManifestCollection struct {
UID uint32 `json:"uid"`
}

// CollectionManager provides methods for performing collections management.
type CollectionManager struct {
httpClient httpProvider
bucketName string
globalTimeout time.Duration
defaultRetryStrategy *retryStrategyWrapper
tracer requestTracer
}

// GetAllScopesOptions is the set of options available to the GetAllScopes operation.
type GetAllScopesOptions struct {
Timeout time.Duration
Expand Down Expand Up @@ -117,7 +117,7 @@ func (cm *CollectionManager) GetAllScopes(opts *GetAllScopesOptions) ([]ScopeSpe
}
} else {
// Temporary support for older server version
var oldMfest manifest
var oldMfest jsonManifest
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&oldMfest)
if err != nil {
Expand Down
154 changes: 117 additions & 37 deletions bucket_viewindexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,80 @@ const (
DevelopmentDesignDocumentNamespace = false
)

// View represents a Couchbase view within a design document.
type jsonView struct {
Map string `json:"map,omitempty"`
Reduce string `json:"reduce,omitempty"`
}

// DesignDocument represents a Couchbase design document containing multiple views.
type jsonDesignDocument struct {
Views map[string]jsonView `json:"views,omitempty"`
}

// View represents a Couchbase view within a design document.
type View struct {
Map string
Reduce string
}

func (v *View) fromData(data jsonView) error {
v.Map = data.Map
v.Reduce = data.Reduce

return nil
}

func (v *View) toData() (jsonView, error) {
var data jsonView

data.Map = v.Map
data.Reduce = v.Reduce

return data, nil
}

// DesignDocument represents a Couchbase design document containing multiple views.
type DesignDocument struct {
Name string
Views map[string]View
}

func (dd *DesignDocument) fromData(data jsonDesignDocument, name string) error {
dd.Name = name

views := make(map[string]View)
for viewName, viewData := range data.Views {
var view View
err := view.fromData(viewData)
if err != nil {
return err
}

views[viewName] = view
}
dd.Views = views

return nil
}

func (dd *DesignDocument) toData() (jsonDesignDocument, string, error) {
var data jsonDesignDocument

views := make(map[string]jsonView)
for viewName, view := range dd.Views {
viewData, err := view.toData()
if err != nil {
return jsonDesignDocument{}, "", err
}

views[viewName] = viewData
}
data.Views = views

return data, dd.Name, nil
}

// ViewIndexManager provides methods for performing View management.
// Volatile: This API is subject to change at any time.
type ViewIndexManager struct {
Expand All @@ -35,22 +109,6 @@ func (vm *ViewIndexManager) doMgmtRequest(req mgmtRequest) (*mgmtResponse, error
return resp, nil
}

// View represents a Couchbase view within a design document.
type View struct {
Map string `json:"map,omitempty"`
Reduce string `json:"reduce,omitempty"`
}

func (v View) hasReduce() bool {
return v.Reduce != ""
}

// DesignDocument represents a Couchbase design document containing multiple views.
type DesignDocument struct {
Name string `json:"-"`
Views map[string]View `json:"views,omitempty"`
}

// GetDesignDocumentOptions is the set of options available to the ViewIndexManager GetDesignDocument operation.
type GetDesignDocumentOptions struct {
Timeout time.Duration
Expand Down Expand Up @@ -109,15 +167,27 @@ func (vm *ViewIndexManager) getDesignDocument(tracectx requestSpanContext, name
return nil, makeMgmtBadStatusError("failed to get design document", &req, resp)
}

ddocObj := DesignDocument{}
var ddocData jsonDesignDocument
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&ddocObj)
err = jsonDec.Decode(&ddocData)
if err != nil {
return nil, err
}

ddocObj.Name = strings.TrimPrefix(name, "dev_")
return &ddocObj, nil
err = resp.Body.Close()
if err != nil {
logDebugf("Failed to close socket (%s)", err)
}

ddocName := strings.TrimPrefix(name, "dev_")

var ddoc DesignDocument
err = ddoc.fromData(ddocData, ddocName)
if err != nil {
return nil, err
}

return &ddoc, nil
}

// GetAllDesignDocumentsOptions is the set of options available to the ViewIndexManager GetAllDesignDocuments operation.
Expand All @@ -127,7 +197,7 @@ type GetAllDesignDocumentsOptions struct {
}

// GetAllDesignDocuments will retrieve all design documents for the given bucket.
func (vm *ViewIndexManager) GetAllDesignDocuments(namespace DesignDocumentNamespace, opts *GetAllDesignDocumentsOptions) ([]*DesignDocument, error) {
func (vm *ViewIndexManager) GetAllDesignDocuments(namespace DesignDocumentNamespace, opts *GetAllDesignDocumentsOptions) ([]DesignDocument, error) {
if opts == nil {
opts = &GetAllDesignDocumentsOptions{}
}
Expand All @@ -152,29 +222,34 @@ func (vm *ViewIndexManager) GetAllDesignDocuments(namespace DesignDocumentNamesp
return nil, makeMgmtBadStatusError("failed to get all design documents", &req, resp)
}

var ddocsObj struct {
var ddocsResp struct {
Rows []struct {
Doc struct {
Meta struct {
ID string
ID string `json:"id"`
}
JSON DesignDocument
}
}
JSON jsonDesignDocument `json:"json"`
} `json:"doc"`
} `json:"rows"`
}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&ddocsObj)
err = jsonDec.Decode(&ddocsResp)
if err != nil {
return nil, err
}

var ddocs []*DesignDocument
for index, ddocData := range ddocsObj.Rows {
ddoc := &ddocsObj.Rows[index].Doc.JSON
isProd := !strings.HasPrefix(ddoc.Name, "dev_")
if isProd == bool(namespace) {
ddoc.Name = strings.TrimPrefix(ddocData.Doc.Meta.ID[8:], "dev_")
ddocs = append(ddocs, ddoc)
err = resp.Body.Close()
if err != nil {
logDebugf("Failed to close socket (%s)", err)
}

ddocs := make([]DesignDocument, len(ddocsResp.Rows))
for ddocIdx, ddocData := range ddocsResp.Rows {
ddocName := strings.TrimPrefix(ddocData.Doc.Meta.ID[8:], "dev_")

err := ddocs[ddocIdx].fromData(ddocData.Doc.JSON, ddocName)
if err != nil {
return nil, err
}
}

Expand Down Expand Up @@ -207,18 +282,23 @@ func (vm *ViewIndexManager) upsertDesignDocument(
startTime time.Time,
opts *UpsertDesignDocumentOptions,
) error {
ddocData, ddocName, err := ddoc.toData()
if err != nil {
return err
}

espan := vm.tracer.StartSpan("encode", tracectx)
data, err := json.Marshal(&ddoc)
data, err := json.Marshal(&ddocData)
espan.Finish()
if err != nil {
return err
}

ddoc.Name = vm.ddocName(ddoc.Name, namespace)
ddocName = vm.ddocName(ddocName, namespace)

req := mgmtRequest{
Service: CapiService,
Path: fmt.Sprintf("/_design/%s", ddoc.Name),
Path: fmt.Sprintf("/_design/%s", ddocName),
Method: "PUT",
Body: data,
Timeout: opts.Timeout,
Expand Down
11 changes: 7 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ type Cluster struct {

// ClusterOptions is the set of options available for creating a Cluster.
type ClusterOptions struct {
Authenticator Authenticator
Authenticator Authenticator

ConnectTimeout time.Duration
KVTimeout time.Duration
ViewTimeout time.Duration
QueryTimeout time.Duration
AnalyticsTimeout time.Duration
SearchTimeout time.Duration
ManagementTimeout time.Duration

// Transcoder is used for trancoding data used in KV operations.
Transcoder Transcoder

DisableMutationTokens bool
RetryStrategy RetryStrategy

RetryStrategy RetryStrategy

// Orphan logging records when the SDK receives responses for requests that are no longer in the system (usually
// due to being timed out).
Expand Down Expand Up @@ -242,9 +245,9 @@ func (c *Cluster) parseExtraConnStrOptions(spec gocbconnstr.ConnSpec) error {
}

// Bucket connects the cluster to server(s) and returns a new Bucket instance.
func (c *Cluster) Bucket(bucketName string, opts *BucketOptions) *Bucket {
func (c *Cluster) Bucket(bucketName string, opts *bucketOptions) *Bucket {
if opts == nil {
opts = &BucketOptions{}
opts = &bucketOptions{}
}
b := newBucket(&c.sb, bucketName, *opts)
cli := c.takeClusterClient()
Expand Down
Loading

0 comments on commit d44b5ae

Please sign in to comment.