github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/ref/workspace_ref.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ref
    16  
    17  import "strings"
    18  
    19  type WorkspaceRef struct {
    20  	workspace string
    21  }
    22  
    23  var _ DoltRef = WorkspaceRef{}
    24  
    25  // NewWorkspaceRef creates a reference to a local workspace from a workspace
    26  // name or a workspace ref e.g. my-workspace, or refs/workspace/my-workspace
    27  func NewWorkspaceRef(workspace string) WorkspaceRef {
    28  	if IsRef(workspace) {
    29  		prefix := PrefixForType(WorkspaceRefType)
    30  		if strings.HasPrefix(workspace, prefix) {
    31  			workspace = workspace[len(prefix):]
    32  		} else {
    33  			panic(workspace + " is a ref that is not of type " + prefix)
    34  		}
    35  	}
    36  
    37  	return WorkspaceRef{workspace}
    38  }
    39  
    40  // GetType will return WorkspaceRefType
    41  func (br WorkspaceRef) GetType() RefType {
    42  	return WorkspaceRefType
    43  }
    44  
    45  // GetPath returns the name of the workspace
    46  func (br WorkspaceRef) GetPath() string {
    47  	return br.workspace
    48  }
    49  
    50  // String returns the fully qualified reference name e.g.
    51  // refs/workspace/my-workspace
    52  func (br WorkspaceRef) String() string {
    53  	return String(br)
    54  }
    55  
    56  // MarshalJSON serializes a WorkspaceRef to JSON.
    57  func (br WorkspaceRef) MarshalJSON() ([]byte, error) {
    58  	return MarshalJSON(br)
    59  }