github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/type.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package dosa
    22  
    23  import (
    24  	"github.com/pkg/errors"
    25  	"github.com/satori/go.uuid"
    26  )
    27  
    28  //go:generate stringer -type=Type
    29  
    30  // Type defines a data type for an entity field
    31  type Type int
    32  
    33  const (
    34  	// Invalid type to be used as zero value for Type
    35  	Invalid Type = iota
    36  
    37  	// TUUID is different from dosa.UUID
    38  	TUUID
    39  
    40  	// String represents a string
    41  	String
    42  
    43  	// Int32 represents an int32
    44  	Int32
    45  
    46  	// Int64 represents either an int64 or a regular int
    47  	Int64
    48  
    49  	// Double is a float64
    50  	Double
    51  
    52  	// Blob is a byte slice
    53  	Blob
    54  
    55  	// Timestamp is a time.Time
    56  	Timestamp
    57  
    58  	// Bool is a bool type
    59  	Bool
    60  )
    61  
    62  // UUID stores a string format of uuid.
    63  // Validation is done before saving to datastore.
    64  // The format of uuid used in datastore is orthogonal to the string format here.
    65  type UUID string
    66  
    67  // NewUUID is a helper for returning a new dosa.UUID value
    68  func NewUUID() UUID {
    69  	return UUID(uuid.NewV4().String())
    70  }
    71  
    72  // Bytes gets the bytes from a UUID
    73  func (u UUID) Bytes() ([]byte, error) {
    74  	id, err := uuid.FromString(string(u))
    75  	if err != nil {
    76  		return nil, errors.Wrap(err, "invalid uuid string")
    77  	}
    78  	return id.Bytes(), nil
    79  }
    80  
    81  // BytesToUUID creates a UUID from a byte slice
    82  func BytesToUUID(bs []byte) (UUID, error) {
    83  	id, err := uuid.FromBytes(bs)
    84  	if err != nil {
    85  		return "", errors.Wrap(err, "invalid uuid bytes")
    86  	}
    87  	return UUID(id.String()), nil
    88  }
    89  
    90  // FromString converts string to dosa Type
    91  func FromString(s string) Type {
    92  	switch s {
    93  	case TUUID.String():
    94  		return TUUID
    95  	case String.String():
    96  		return String
    97  	case Int32.String():
    98  		return Int32
    99  	case Int64.String():
   100  		return Int64
   101  	case Double.String():
   102  		return Double
   103  	case Blob.String():
   104  		return Blob
   105  	case Timestamp.String():
   106  		return Timestamp
   107  	case Bool.String():
   108  		return Bool
   109  	default:
   110  		return Invalid
   111  	}
   112  }
   113  
   114  func isInvalidPrimaryKeyType(c *ColumnDefinition) bool {
   115  	if c.IsPointer {
   116  		return true
   117  	}
   118  
   119  	switch c.Type {
   120  	case Invalid:
   121  		return true
   122  	default:
   123  		return false
   124  	}
   125  }