Types package simplification for easier type insertion

This commit is contained in:
Daylon Wilkins
2019-10-31 15:23:29 -07:00
committed by Daylon Wilkins
parent 23f12dfcce
commit 146dbf1104
70 changed files with 1800 additions and 1784 deletions

View File

@@ -142,10 +142,10 @@ func ParseKeyValues(nbf *types.NomsBinFormat, sch schema.Schema, args []string)
}
}
convFuncs := make(map[uint64]doltcore.ConvFunc)
convFuncs := make(map[uint64]types.MarshalCallback)
err := sch.GetPKCols().Iter(func(tag uint64, col schema.Column) (stop bool, err error) {
convFunc := doltcore.GetConvFunc(types.StringKind, col.Kind)
if convFunc == nil {
convFunc, err := doltcore.GetConvFunc(types.StringKind, col.Kind)
if err != nil {
return false, ColumnError{col.Name, "Conversion from string to " + col.KindString() + "is not defined."}
}

View File

@@ -84,9 +84,12 @@ func parseWhere(sch schema.Schema, whereClause string) (filterFn, error) {
}
tag := col.Tag
convFunc := doltcore.GetConvFunc(types.StringKind, col.Kind)
val, err := convFunc(types.String(valStr))
convFunc, err := doltcore.GetConvFunc(types.StringKind, col.Kind)
if err != nil {
return nil, err
}
val, err := convFunc(types.String(valStr))
if err != nil {
return nil, errors.New("unable to convert '" + valStr + "' to " + col.KindString())
}

View File

@@ -39,6 +39,8 @@ github.com/attic-labs/kingpin v2.2.7-0.20180312050558-442efcfac769+incompatible/
github.com/aws/aws-sdk-go v0.0.0-20180223184012-ebef4262e06a/go.mod h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k=
github.com/aws/aws-sdk-go v1.21.2 h1:CqbWrQzi7s8J2F0TRRdLvTr0+bt5Zxo2IDoFNGsAiUg=
github.com/aws/aws-sdk-go v1.21.2/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/bcicen/jstream v0.0.0-20190220045926-16c1f8af81c2 h1:M+TYzBcNIRyzPRg66ndEqUMd7oWDmhvdQmaPC6EZNwM=
github.com/bcicen/jstream v0.0.0-20190220045926-16c1f8af81c2/go.mod h1:RDu/qcrnpEdJC/p8tx34+YBFqqX71lB7dOX9QE+ZC4M=
github.com/beorn7/perks v0.0.0-20160229213445-3ac7bf7a47d1/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v0.0.0-20160229213445-3ac7bf7a47d1/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=

View File

@@ -32,7 +32,7 @@ type RowConverter struct {
*FieldMapping
// IdentityConverter is a bool which is true if the converter is doing nothing.
IdentityConverter bool
ConvFuncs map[uint64]doltcore.ConvFunc
ConvFuncs map[uint64]types.MarshalCallback
}
func newIdentityConverter(mapping *FieldMapping) *RowConverter {
@@ -47,7 +47,7 @@ func NewRowConverter(mapping *FieldMapping) (*RowConverter, error) {
return newIdentityConverter(mapping), nil
}
convFuncs := make(map[uint64]doltcore.ConvFunc, len(mapping.SrcToDest))
convFuncs := make(map[uint64]types.MarshalCallback, len(mapping.SrcToDest))
for srcTag, destTag := range mapping.SrcToDest {
destCol, destOk := mapping.DestSch.GetAllCols().GetByTag(destTag)
srcCol, srcOk := mapping.SrcSch.GetAllCols().GetByTag(srcTag)
@@ -56,11 +56,13 @@ func NewRowConverter(mapping *FieldMapping) (*RowConverter, error) {
return nil, fmt.Errorf("Could not find column being mapped. src tag: %d, dest tag: %d", srcTag, destTag)
}
convFuncs[srcTag] = doltcore.GetConvFunc(srcCol.Kind, destCol.Kind)
convFunc, err := doltcore.GetConvFunc(srcCol.Kind, destCol.Kind)
if convFuncs[srcTag] == nil {
if err != nil {
return nil, fmt.Errorf("Unsupported conversion from type %s to %s", srcCol.KindString(), destCol.KindString())
}
convFuncs[srcTag] = convFunc
}
return &RowConverter{mapping, false, convFuncs}, nil

View File

@@ -15,14 +15,7 @@
package doltcore
import (
"encoding/base64"
"errors"
"fmt"
"math"
"strconv"
"strings"
"github.com/google/uuid"
"github.com/liquidata-inc/dolt/go/store/types"
)
@@ -33,136 +26,12 @@ func StringToValue(s string, kind types.NomsKind) (types.Value, error) {
return nil, errors.New("Only primitive type support")
}
switch kind {
case types.StringKind:
return types.String(s), nil
case types.FloatKind:
return stringToFloat(s)
case types.BoolKind:
return stringToBool(s)
case types.IntKind:
return stringToInt(s)
case types.UintKind:
return stringToUint(s)
case types.UUIDKind:
return stringToUUID(s)
case types.NullKind:
return types.NullValue, nil
case types.InlineBlobKind:
return stringToInlineBlob(s)
}
panic("Unsupported type " + kind.String())
}
func stringToFloat(s string) (types.Value, error) {
if len(s) == 0 {
return types.NullValue, nil
}
f, err := strconv.ParseFloat(s, 64)
emptyStringType := types.KindToType[types.StringKind]
marshalFunc, err := emptyStringType.GetMarshalFunc(kind)
if err != nil {
return types.Float(math.NaN()), ConversionError{types.StringKind, types.FloatKind, err}
panic("Unsupported type " + kind.String())
}
return types.Float(f), nil
}
func stringToBool(s string) (types.Value, error) {
if len(s) == 0 {
return types.NullValue, nil
}
b, err := strconv.ParseBool(strings.ToLower(s))
if err != nil {
return types.Bool(false), ConversionError{types.StringKind, types.BoolKind, err}
}
return types.Bool(b), nil
}
func parseNumber(s string) (isNegative bool, decPos int, err error) {
decPos = -1
for i, c := range s {
if i == 0 && c == '-' {
isNegative = true
} else if c == '.' {
if decPos != -1 {
return false, -1, errors.New("not a valid number. multiple decimal points found.")
}
decPos = i
} else if c > '9' || c < '0' {
return false, -1, fmt.Errorf("for the string '%s' found invalid character '%s' at pos %d", s, string(c), i)
}
}
return isNegative, decPos, nil
}
func stringToInt(s string) (types.Value, error) {
_, decPos, err := parseNumber(s)
if err != nil {
return types.Int(0), ConversionError{types.StringKind, types.IntKind, err}
}
if decPos != -1 {
s = s[:decPos]
}
if len(s) == 0 {
return types.NullValue, nil
}
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return types.Int(0), ConversionError{types.StringKind, types.IntKind, err}
}
return types.Int(n), nil
}
func stringToUint(s string) (types.Value, error) {
if len(s) == 0 {
return types.NullValue, nil
}
n, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return types.Uint(0), ConversionError{types.StringKind, types.UintKind, err}
}
return types.Uint(n), nil
}
func stringToUUID(s string) (types.Value, error) {
if len(s) == 0 {
return types.NullValue, nil
}
u, err := uuid.Parse(s)
if err != nil {
return types.UUID(u), ConversionError{types.StringKind, types.UUIDKind, err}
}
return types.UUID(u), nil
}
func stringToInlineBlob(s string) (types.Value, error) {
if len(s) == 0 {
return types.NullValue, nil
}
data, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
return types.InlineBlob{}, ConversionError{types.StringKind, types.InlineBlobKind, err}
}
return types.InlineBlob(data), nil
return marshalFunc(types.String(s))
}

View File

@@ -35,6 +35,10 @@ var tests = []strToNomsTypeTests{
{"False", types.BoolKind, types.Bool(false), false},
{"-123456", types.IntKind, types.Int(-123456), false},
{"123456", types.IntKind, types.Int(123456), false},
{"0.123", types.IntKind, types.Int(0), false},
{".123", types.IntKind, types.Int(0), false},
{"-0.123", types.IntKind, types.Int(0), false},
{"-.123", types.IntKind, types.Int(0), false},
{"100000000000", types.UintKind, types.Uint(100000000000), false},
{"0", types.UintKind, types.Uint(0), false},
{
@@ -45,12 +49,12 @@ var tests = []strToNomsTypeTests{
{"0", types.UintKind, types.Uint(0), false},
{"", types.NullKind, types.NullValue, false},
{
"YWJjZGVmZw",
"61626364656667",
types.InlineBlobKind,
types.InlineBlob([]byte{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67}),
false},
{
"YdGE4ZCD8J2Vqw",
"61D184E19083F09D95AB",
types.InlineBlobKind,
types.InlineBlob([]byte{0x61, 0xd1, 0x84, 0xe1, 0x90, 0x83, 0xf0, 0x9d, 0x95, 0xab}),
false},

View File

@@ -148,16 +148,28 @@ func (r *JSONReader) convToRow(rowMap map[string]interface{}) (row.Row, error) {
switch val := v.(type) {
case int:
f := doltcore.GetConvFunc(types.IntKind, col.Kind)
f, err := doltcore.GetConvFunc(types.IntKind, col.Kind)
if err != nil {
return nil, err
}
taggedVals[col.Tag], _ = f(types.Int(val))
case string:
f := doltcore.GetConvFunc(types.StringKind, col.Kind)
f, err := doltcore.GetConvFunc(types.StringKind, col.Kind)
if err != nil {
return nil, err
}
taggedVals[col.Tag], _ = f(types.String(val))
case bool:
f := doltcore.GetConvFunc(types.BoolKind, col.Kind)
f, err := doltcore.GetConvFunc(types.BoolKind, col.Kind)
if err != nil {
return nil, err
}
taggedVals[col.Tag], _ = f(types.Bool(val))
case float64:
f := doltcore.GetConvFunc(types.FloatKind, col.Kind)
f, err := doltcore.GetConvFunc(types.FloatKind, col.Kind)
if err != nil {
return nil, err
}
taggedVals[col.Tag], _ = f(types.Float(val))
}

View File

@@ -127,7 +127,11 @@ func (w *SqlExportWriter) insertStatementForRow(r row.Row) (string, error) {
if seenOne {
b.WriteRune(',')
}
b.WriteString(w.sqlString(val))
sqlString, err := w.sqlString(val)
if err != nil {
return true, err
}
b.WriteString(sqlString)
seenOne = true
return false, nil
})
@@ -151,29 +155,38 @@ func (w *SqlExportWriter) dropCreateStatement() string {
return b.String()
}
func (w *SqlExportWriter) sqlString(value types.Value) string {
func (w *SqlExportWriter) sqlString(value types.Value) (string, error) {
if types.IsNull(value) {
return "NULL"
return "NULL", nil
}
switch value.Kind() {
case types.BoolKind:
if value.(types.Bool) {
return "TRUE"
return "TRUE", nil
} else {
return "FALSE"
return "FALSE", nil
}
case types.UUIDKind:
convFn := doltcore.GetConvFunc(value.Kind(), types.StringKind)
convFn, err := doltcore.GetConvFunc(value.Kind(), types.StringKind)
if err != nil {
return "", err
}
str, _ := convFn(value)
return doubleQuot + string(str.(types.String)) + doubleQuot
return doubleQuot + string(str.(types.String)) + doubleQuot, nil
case types.StringKind:
s := string(value.(types.String))
s = strings.ReplaceAll(s, doubleQuot, "\\\"")
return doubleQuot + s + doubleQuot
return doubleQuot + s + doubleQuot, nil
default:
convFn := doltcore.GetConvFunc(value.Kind(), types.StringKind)
str, _ := convFn(value)
return string(str.(types.String))
convFn, err := doltcore.GetConvFunc(value.Kind(), types.StringKind)
if err != nil {
return "", err
}
str, err := convFn(value)
if err != nil {
return "", err
}
return string(str.(types.String)), nil
}
}

View File

@@ -16,387 +16,15 @@ package doltcore
import (
"fmt"
"strconv"
"github.com/liquidata-inc/dolt/go/store/types"
)
type ConversionError struct {
fromKind types.NomsKind
toKind types.NomsKind
err error
}
func (ce ConversionError) Error() string {
toKindStr := types.KindToString[ce.toKind]
fromKindStr := types.KindToString[ce.fromKind]
return fmt.Sprint("error converting", fromKindStr, "to", toKindStr+":", ce.err.Error())
}
func IsConversionError(err error) bool {
_, ok := err.(ConversionError)
return ok
}
func GetFromAndToKinds(err error) (from, to types.NomsKind) {
ce, ok := err.(ConversionError)
if !ok {
panic("Check that this is a conversion error before using this.")
}
return ce.fromKind, ce.toKind
}
func GetUnderlyingError(err error) error {
ce, ok := err.(ConversionError)
if !ok {
panic("Check that this is a conversion error before using this.")
}
return ce.err
}
// ConvFunc is a function that converts one noms or dolt value to another of a different type.
type ConvFunc func(types.Value) (types.Value, error)
var convFuncMap = map[types.NomsKind]map[types.NomsKind]ConvFunc{
types.StringKind: {
types.StringKind: identityConvFunc,
types.UUIDKind: convStringToUUID,
types.UintKind: convStringToUint,
types.IntKind: convStringToInt,
types.FloatKind: convStringToFloat,
types.BoolKind: convStringToBool,
types.InlineBlobKind: convStringToInlineBlob,
types.NullKind: convToNullFunc},
types.UUIDKind: {
types.StringKind: convUUIDToString,
types.UUIDKind: identityConvFunc,
types.UintKind: nil,
types.IntKind: nil,
types.FloatKind: nil,
types.BoolKind: nil,
types.InlineBlobKind: nil,
types.NullKind: convToNullFunc},
types.UintKind: {
types.StringKind: convUintToString,
types.UUIDKind: nil,
types.UintKind: identityConvFunc,
types.IntKind: convUintToInt,
types.FloatKind: convUintToFloat,
types.BoolKind: convUintToBool,
types.InlineBlobKind: nil,
types.NullKind: convToNullFunc},
types.IntKind: {
types.StringKind: convIntToString,
types.UUIDKind: nil,
types.UintKind: convIntToUint,
types.IntKind: identityConvFunc,
types.FloatKind: convIntToFloat,
types.BoolKind: convIntToBool,
types.InlineBlobKind: nil,
types.NullKind: convToNullFunc},
types.FloatKind: {
types.StringKind: convFloatToString,
types.UUIDKind: nil,
types.UintKind: convFloatToUint,
types.IntKind: convFloatToInt,
types.FloatKind: identityConvFunc,
types.BoolKind: convFloatToBool,
types.InlineBlobKind: nil,
types.NullKind: convToNullFunc},
types.BoolKind: {
types.StringKind: convBoolToString,
types.UUIDKind: nil,
types.UintKind: convBoolToUint,
types.IntKind: convBoolToInt,
types.FloatKind: convBoolToFloat,
types.BoolKind: identityConvFunc,
types.InlineBlobKind: nil,
types.NullKind: convToNullFunc},
types.InlineBlobKind: {
types.StringKind: convInlineBlobToString,
types.UUIDKind: nil,
types.UintKind: nil,
types.IntKind: nil,
types.FloatKind: nil,
types.BoolKind: nil,
types.InlineBlobKind: identityConvFunc,
types.NullKind: convToNullFunc},
types.NullKind: {
types.StringKind: convToNullFunc,
types.UUIDKind: convToNullFunc,
types.UintKind: convToNullFunc,
types.IntKind: convToNullFunc,
types.FloatKind: convToNullFunc,
types.BoolKind: convToNullFunc,
types.InlineBlobKind: convToNullFunc,
types.NullKind: convToNullFunc},
}
// GetConvFunc takes in a source kind and a destination kind and returns a ConvFunc which can convert values of the
// GetConvFunc takes in a source kind and a destination kind and returns a MarshalCallback which can convert values of the
// source kind to values of the destination kind.
func GetConvFunc(srcKind, destKind types.NomsKind) ConvFunc {
var convFunc ConvFunc
if destKindMap, ok := convFuncMap[srcKind]; ok {
convFunc = destKindMap[destKind]
func GetConvFunc(srcKind, destKind types.NomsKind) (types.MarshalCallback, error) {
if emptyVal, ok := types.KindToType[srcKind]; ok && emptyVal != nil {
return emptyVal.GetMarshalFunc(destKind)
}
return convFunc
}
var identityConvFunc = func(value types.Value) (types.Value, error) {
return value, nil
}
var convToNullFunc = func(types.Value) (types.Value, error) {
return types.NullValue, nil
}
func convStringToFloat(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return stringToFloat(string(val.(types.String)))
}
func convStringToBool(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return stringToBool(string(val.(types.String)))
}
func convStringToInt(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return stringToInt(string(val.(types.String)))
}
func convStringToUint(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return stringToUint(string(val.(types.String)))
}
func convStringToUUID(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return stringToUUID(string(val.(types.String)))
}
func convStringToInlineBlob(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return stringToInlineBlob(string(val.(types.String)))
}
func convUUIDToString(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return types.String(val.(types.UUID).String()), nil
}
func convUintToString(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(types.Uint))
str := strconv.FormatUint(n, 10)
return types.String(str), nil
}
func convUintToInt(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(types.Uint))
return types.Int(int64(n)), nil
}
func convUintToFloat(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(types.Uint))
return types.Float(float64(n)), nil
}
func convUintToBool(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(types.Uint))
return types.Bool(n != 0), nil
}
func convIntToString(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(types.Int))
str := strconv.FormatInt(n, 10)
return types.String(str), nil
}
func convIntToUint(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(types.Int))
return types.Uint(uint64(n)), nil
}
func convIntToFloat(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(types.Int))
return types.Float(float64(n)), nil
}
func convIntToBool(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(types.Int))
return types.Bool(n != 0), nil
}
func convFloatToString(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(types.Float))
str := strconv.FormatFloat(fl, 'f', -1, 64)
return types.String(str), nil
}
func convFloatToUint(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(types.Float))
return types.Uint(uint64(fl)), nil
}
func convFloatToInt(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(types.Float))
return types.Int(int(fl)), nil
}
func convFloatToBool(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(types.Float))
return types.Bool(fl != 0), nil
}
var trueValStr = types.String("true")
var falseValStr = types.String("false")
func convBoolToString(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
b := val.(types.Bool)
if b {
return trueValStr, nil
}
return falseValStr, nil
}
var zeroUintVal = types.Uint(0)
var oneUintVal = types.Uint(1)
func convBoolToUint(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
b := val.(types.Bool)
if b {
return oneUintVal, nil
}
return zeroUintVal, nil
}
var zeroIntVal = types.Int(0)
var oneIntVal = types.Int(1)
func convBoolToInt(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
b := val.(types.Bool)
if b {
return oneIntVal, nil
}
return zeroIntVal, nil
}
var zeroFloatVal = types.Float(0)
var oneFloatVal = types.Float(1)
func convBoolToFloat(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
b := val.(types.Bool)
if b {
return oneFloatVal, nil
}
return zeroFloatVal, nil
}
func convInlineBlobToString(val types.Value) (types.Value, error) {
if val == nil {
return nil, nil
}
return types.String(val.(types.InlineBlob).String()), nil
return nil, fmt.Errorf("%v does not have an associated Value implementation", srcKind)
}

View File

@@ -32,81 +32,86 @@ func TestConv(t *testing.T) {
tests := []struct {
input types.Value
expectedOut types.Value
expectFunc ConvFunc
expectFunc bool
expectErr bool
}{
{types.String("test"), types.String("test"), identityConvFunc, false},
{types.String(zeroUUIDStr), types.UUID(zeroUUID), convStringToUUID, false},
{types.String("10"), types.Uint(10), convStringToUint, false},
{types.String("-101"), types.Int(-101), convStringToInt, false},
{types.String("3.25"), types.Float(3.25), convStringToFloat, false},
{types.String("true"), types.Bool(true), convStringToBool, false},
{types.String("YdGE4ZCD8J2Vqw"), types.InlineBlob([]byte{0x61, 0xd1, 0x84, 0xe1, 0x90, 0x83, 0xf0, 0x9d, 0x95, 0xab}),
convStringToInlineBlob, false},
{types.String("anything"), types.NullValue, convToNullFunc, false},
{types.String("test"), types.String("test"), true, false},
{types.String(zeroUUIDStr), types.UUID(zeroUUID), true, false},
{types.String("10"), types.Uint(10), true, false},
{types.String("-101"), types.Int(-101), true, false},
{types.String("3.25"), types.Float(3.25), true, false},
{types.String("true"), types.Bool(true), true, false},
{types.String("61D184E19083F09D95AB"), types.InlineBlob([]byte{0x61, 0xd1, 0x84, 0xe1, 0x90, 0x83, 0xf0, 0x9d, 0x95, 0xab}),
true, false},
{types.String("anything"), types.NullValue, true, false},
{types.UUID(zeroUUID), types.String(zeroUUIDStr), convUUIDToString, false},
{types.UUID(zeroUUID), types.UUID(zeroUUID), identityConvFunc, false},
{types.UUID(zeroUUID), types.Uint(0), nil, false},
{types.UUID(zeroUUID), types.Int(0), nil, false},
{types.UUID(zeroUUID), types.Float(0), nil, false},
{types.UUID(zeroUUID), types.Bool(false), nil, false},
{types.UUID(zeroUUID), types.InlineBlob{}, nil, false},
{types.UUID(zeroUUID), types.NullValue, convToNullFunc, false},
{types.UUID(zeroUUID), types.String(zeroUUIDStr), true, false},
{types.UUID(zeroUUID), types.UUID(zeroUUID), true, false},
{types.UUID(zeroUUID), types.Uint(0), false, false},
{types.UUID(zeroUUID), types.Int(0), false, false},
{types.UUID(zeroUUID), types.Float(0), false, false},
{types.UUID(zeroUUID), types.Bool(false), false, false},
{types.UUID(zeroUUID), types.InlineBlob{}, false, false},
{types.UUID(zeroUUID), types.NullValue, true, false},
{types.Uint(10), types.String("10"), convUintToString, false},
{types.Uint(100), types.UUID(zeroUUID), nil, false},
{types.Uint(1000), types.Uint(1000), identityConvFunc, false},
{types.Uint(10000), types.Int(10000), convUintToInt, false},
{types.Uint(100000), types.Float(100000), convUintToFloat, false},
{types.Uint(1000000), types.Bool(true), convUintToBool, false},
{types.Uint(10000000), types.InlineBlob{}, nil, false},
{types.Uint(100000000), types.NullValue, convToNullFunc, false},
{types.Uint(10), types.String("10"), true, false},
{types.Uint(100), types.UUID(zeroUUID), false, false},
{types.Uint(1000), types.Uint(1000), true, false},
{types.Uint(10000), types.Int(10000), true, false},
{types.Uint(100000), types.Float(100000), true, false},
{types.Uint(1000000), types.Bool(true), true, false},
{types.Uint(10000000), types.InlineBlob{}, false, false},
{types.Uint(100000000), types.NullValue, true, false},
{types.Int(-10), types.String("-10"), convIntToString, false},
{types.Int(-100), types.UUID(zeroUUID), nil, false},
{types.Int(1000), types.Uint(1000), convIntToUint, false},
{types.Int(-10000), types.Int(-10000), identityConvFunc, false},
{types.Int(-100000), types.Float(-100000), convIntToFloat, false},
{types.Int(-1000000), types.Bool(true), convIntToBool, false},
{types.Int(-10000000), types.InlineBlob{}, nil, false},
{types.Int(-100000000), types.NullValue, convToNullFunc, false},
{types.Int(-10), types.String("-10"), true, false},
{types.Int(-100), types.UUID(zeroUUID), false, false},
{types.Int(1000), types.Uint(1000), true, false},
{types.Int(-10000), types.Int(-10000), true, false},
{types.Int(-100000), types.Float(-100000), true, false},
{types.Int(-1000000), types.Bool(true), true, false},
{types.Int(-10000000), types.InlineBlob{}, false, false},
{types.Int(-100000000), types.NullValue, true, false},
{types.Float(1.5), types.String("1.5"), convFloatToString, false},
{types.Float(10.5), types.UUID(zeroUUID), nil, false},
{types.Float(100.5), types.Uint(100), convFloatToUint, false},
{types.Float(1000.5), types.Int(1000), convFloatToInt, false},
{types.Float(10000.5), types.Float(10000.5), identityConvFunc, false},
{types.Float(100000.5), types.Bool(true), convFloatToBool, false},
{types.Float(1000000.5), types.InlineBlob{}, nil, false},
{types.Float(10000000.5), types.NullValue, convToNullFunc, false},
{types.Float(1.5), types.String("1.5"), true, false},
{types.Float(10.5), types.UUID(zeroUUID), false, false},
{types.Float(100.5), types.Uint(100), true, false},
{types.Float(1000.5), types.Int(1000), true, false},
{types.Float(10000.5), types.Float(10000.5), true, false},
{types.Float(100000.5), types.Bool(true), true, false},
{types.Float(1000000.5), types.InlineBlob{}, false, false},
{types.Float(10000000.5), types.NullValue, true, false},
{types.Bool(true), types.String("true"), convBoolToString, false},
{types.Bool(false), types.UUID(zeroUUID), nil, false},
{types.Bool(true), types.Uint(1), convBoolToUint, false},
{types.Bool(false), types.Int(0), convBoolToInt, false},
{types.Bool(true), types.Float(1), convBoolToFloat, false},
{types.Bool(false), types.Bool(false), identityConvFunc, false},
{types.Bool(false), types.InlineBlob{}, nil, true},
{types.Bool(true), types.NullValue, convToNullFunc, false},
{types.Bool(true), types.String("true"), true, false},
{types.Bool(false), types.UUID(zeroUUID), false, false},
{types.Bool(true), types.Uint(1), true, false},
{types.Bool(false), types.Int(0), true, false},
{types.Bool(true), types.Float(1), true, false},
{types.Bool(false), types.Bool(false), true, false},
{types.Bool(false), types.InlineBlob{}, false, true},
{types.Bool(true), types.NullValue, true, false},
{types.InlineBlob([]byte{0x61, 0xd1, 0x84, 0xe1, 0x90, 0x83, 0xf0, 0x9d, 0x95, 0xab}),
types.String("YdGE4ZCD8J2Vqw"), convInlineBlobToString, false},
{types.InlineBlob([]byte{}), types.UUID(zeroUUID), nil, false},
{types.InlineBlob([]byte{}), types.Uint(1583200922), nil, false},
{types.InlineBlob([]byte{}), types.Int(1901502183), nil, false},
{types.InlineBlob([]byte{}), types.Float(2219803444.4), nil, false},
{types.InlineBlob([]byte{}), types.Bool(false), nil, true},
{types.InlineBlob([]byte{1, 10, 100}), types.InlineBlob([]byte{1, 10, 100}), identityConvFunc, false},
{types.InlineBlob([]byte{}), types.NullValue, convToNullFunc, false},
types.String("61D184E19083F09D95AB"), true, false},
{types.InlineBlob([]byte{}), types.UUID(zeroUUID), false, false},
{types.InlineBlob([]byte{}), types.Uint(1583200922), false, false},
{types.InlineBlob([]byte{}), types.Int(1901502183), false, false},
{types.InlineBlob([]byte{}), types.Float(2219803444.4), false, false},
{types.InlineBlob([]byte{}), types.Bool(false), false, true},
{types.InlineBlob([]byte{1, 10, 100}), types.InlineBlob([]byte{1, 10, 100}), true, false},
{types.InlineBlob([]byte{}), types.NullValue, true, false},
}
for _, test := range tests {
convFunc := GetConvFunc(test.input.Kind(), test.expectedOut.Kind())
convFunc, err := GetConvFunc(test.input.Kind(), test.expectedOut.Kind())
if convFunc == nil && test.expectFunc != nil {
t.Error("Did not receive correct conversion function for conversion from", test.input.Kind(), "to", test.expectedOut.Kind())
if convFunc == nil && err != nil && test.expectFunc == true {
t.Error("Did not receive conversion function for conversion from", test.input.Kind(), "to", test.expectedOut.Kind())
} else if convFunc != nil {
if test.expectFunc == false {
t.Error("Incorrectly received conversion function for conversion from", test.input.Kind(), "to", test.expectedOut.Kind())
continue
}
result, err := convFunc(test.input)
if (err != nil) != test.expectErr {
@@ -125,9 +130,9 @@ var convertibleTypes = []types.NomsKind{types.StringKind, types.UUIDKind, types.
func TestNullConversion(t *testing.T) {
for _, srcKind := range convertibleTypes {
for _, destKind := range convertibleTypes {
convFunc := GetConvFunc(srcKind, destKind)
convFunc, err := GetConvFunc(srcKind, destKind)
if convFunc != nil {
if convFunc != nil && err == nil {
res, err := convFunc(nil)
if res != nil || err != nil {

View File

@@ -153,7 +153,7 @@ func mustSet(s types.Set, err error) types.Set {
}
func validate(ctx context.Context, nbf *types.NomsBinFormat, r types.Value) bool {
rootType := mustType(types.MakeMapType(types.StringType, mustType(types.MakeRefType(types.ValueType))))
rootType := mustType(types.MakeMapType(types.PrimitiveTypeMap[types.StringKind], mustType(types.MakeRefType(types.PrimitiveTypeMap[types.ValueKind]))))
if isSub, err := types.IsValueSubtypeOf(nbf, r, rootType); err != nil {
panic(err)
} else if !isSub {

View File

@@ -118,7 +118,7 @@ func TestNewCommit(t *testing.T) {
et, err := makeCommitStructType(
types.EmptyStructType,
mustType(types.MakeSetType(mustType(types.MakeUnionType()))),
types.FloaTType,
types.PrimitiveTypeMap[types.FloatKind],
)
assert.NoError(err)
assertTypeEquals(et, at)

View File

@@ -630,9 +630,9 @@ func (suite *DatabaseSuite) TestDatabaseHeightOfRefs() {
}
func (suite *DatabaseSuite) TestDatabaseHeightOfCollections() {
setOfStringType, err := types.MakeSetType(types.StringType)
setOfStringType, err := types.MakeSetType(types.PrimitiveTypeMap[types.StringKind])
suite.NoError(err)
setOfRefOfStringType, err := types.MakeSetType(mustType(types.MakeRefType(types.StringType)))
setOfRefOfStringType, err := types.MakeSetType(mustType(types.MakeRefType(types.PrimitiveTypeMap[types.StringKind])))
suite.NoError(err)
// Set<String>

View File

@@ -499,16 +499,16 @@ func TestNomsDiffPrintType(t *testing.T) {
expected1 := "- List<Float>\n+ List<String>\n"
expectedPaths1 := []string{""}
t1, err := types.MakeListType(types.FloaTType)
t1, err := types.MakeListType(types.PrimitiveTypeMap[types.FloatKind])
assert.NoError(err)
t2, err := types.MakeListType(types.StringType)
t2, err := types.MakeListType(types.PrimitiveTypeMap[types.StringKind])
assert.NoError(err)
expected2 := "- List<Float>\n+ Set<String>\n"
expectedPaths2 := []string{``}
t3, err := types.MakeListType(types.FloaTType)
t3, err := types.MakeListType(types.PrimitiveTypeMap[types.FloatKind])
assert.NoError(err)
t4, err := types.MakeSetType(types.StringType)
t4, err := types.MakeSetType(types.PrimitiveTypeMap[types.StringKind])
assert.NoError(err)
tf := func(leftRight bool) {

View File

@@ -606,13 +606,13 @@ func TestDecodeNomsTypePtr(t *testing.T) {
type S struct{ Type *types.Type }
var s S
primitive := types.StringType
primitive := types.PrimitiveTypeMap[types.StringKind]
testUnmarshal(mustStruct(types.NewStruct(types.Format_7_18, "S", types.StructData{"type": primitive})), &s, &S{primitive})
complex := mustType(types.MakeStructType("Complex",
types.StructField{
Name: "stuff",
Type: types.StringType,
Type: types.PrimitiveTypeMap[types.StringKind],
},
))
testUnmarshal(mustStruct(types.NewStruct(types.Format_7_18, "S", types.StructData{"type": complex})), &s, &S{complex})

View File

@@ -590,13 +590,13 @@ func TestEncodeNomsTypePtr(t *testing.T) {
Type *types.Type
}
primitive := types.StringType
primitive := types.PrimitiveTypeMap[types.StringKind]
testMarshal(S{primitive}, mustStruct(types.NewStruct(types.Format_7_18, "S", types.StructData{"type": primitive})))
complex := mustType(types.MakeStructType("Complex",
types.StructField{
Name: "stuff",
Type: types.StringType,
Type: types.PrimitiveTypeMap[types.StringKind],
},
))
testMarshal(S{complex}, mustStruct(types.NewStruct(types.Format_7_18, "S", types.StructData{"type": complex})))
@@ -627,7 +627,7 @@ func TestEncodeRecursive(t *testing.T) {
},
types.StructField{
Name: "value",
Type: types.FloaTType,
Type: types.PrimitiveTypeMap[types.FloatKind],
},
)
assert.NoError(err)
@@ -957,7 +957,7 @@ func TestEncodeOriginal(t *testing.T) {
assert.True(out.Equals(mustStruct(orig.Set("foo", types.Float(43)))))
st2 := mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"foo": types.FloaTType,
"foo": types.PrimitiveTypeMap[types.FloatKind],
}))
assert.True(mustType(types.TypeOf(out)).Equals(st2))
@@ -987,7 +987,7 @@ func TestNomsTypes(t *testing.T) {
Bool: types.Bool(true),
Number: types.Float(42),
String: types.String("hi"),
Type: types.FloaTType,
Type: types.PrimitiveTypeMap[types.FloatKind],
}
assert.True(mustValue(Marshal(context.Background(), vs, s)).Equals(
mustStruct(types.NewStruct(types.Format_7_18, "S", types.StructData{
@@ -995,7 +995,7 @@ func TestNomsTypes(t *testing.T) {
"bool": types.Bool(true),
"number": types.Float(42),
"string": types.String("hi"),
"type": types.FloaTType,
"type": types.PrimitiveTypeMap[types.FloatKind],
})),
))
}

View File

@@ -98,29 +98,29 @@ func encodeType(nbf *types.NomsBinFormat, t reflect.Type, seenStructs map[string
if t.Implements(nomsValueInterface) {
if t == typeOfTypesType {
return types.TypeType, nil
return types.PrimitiveTypeMap[types.TypeKind], nil
}
// Use Name because List and Blob are convertible to each other on Go.
switch t.Name() {
case "Blob":
return types.BlobType, nil
return types.PrimitiveTypeMap[types.BlobKind], nil
case "Bool":
return types.BoolType, nil
return types.PrimitiveTypeMap[types.BoolKind], nil
case "List":
return types.MakeListType(types.ValueType)
return types.MakeListType(types.PrimitiveTypeMap[types.ValueKind])
case "Map":
return types.MakeMapType(types.ValueType, types.ValueType)
return types.MakeMapType(types.PrimitiveTypeMap[types.ValueKind], types.PrimitiveTypeMap[types.ValueKind])
case "Float":
return types.FloaTType, nil
return types.PrimitiveTypeMap[types.FloatKind], nil
case "Ref":
return types.MakeRefType(types.ValueType)
return types.MakeRefType(types.PrimitiveTypeMap[types.ValueKind])
case "Set":
return types.MakeSetType(types.ValueType)
return types.MakeSetType(types.PrimitiveTypeMap[types.ValueKind])
case "String":
return types.StringType, nil
return types.PrimitiveTypeMap[types.StringKind], nil
case "Value":
return types.ValueType, nil
return types.PrimitiveTypeMap[types.ValueKind], nil
}
return nil, fmt.Errorf("cannot marshal type %s, it requires type parameters", t)
@@ -128,11 +128,11 @@ func encodeType(nbf *types.NomsBinFormat, t reflect.Type, seenStructs map[string
switch t.Kind() {
case reflect.Bool:
return types.BoolType, nil
return types.PrimitiveTypeMap[types.BoolKind], nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
return types.FloaTType, nil
return types.PrimitiveTypeMap[types.FloatKind], nil
case reflect.String:
return types.StringType, nil
return types.PrimitiveTypeMap[types.StringKind], nil
case reflect.Struct:
return structEncodeType(nbf, t, seenStructs)
case reflect.Array, reflect.Slice:

View File

@@ -44,32 +44,32 @@ func TestMarshalTypeType(tt *testing.T) {
assert.True(tt, exp.Equals(actual))
}
t(types.FloaTType, float32(0))
t(types.FloaTType, float64(0))
t(types.FloaTType, int(0))
t(types.FloaTType, int16(0))
t(types.FloaTType, int32(0))
t(types.FloaTType, int64(0))
t(types.FloaTType, int8(0))
t(types.FloaTType, uint(0))
t(types.FloaTType, uint16(0))
t(types.FloaTType, uint32(0))
t(types.FloaTType, uint64(0))
t(types.FloaTType, uint8(0))
t(types.PrimitiveTypeMap[types.FloatKind], float32(0))
t(types.PrimitiveTypeMap[types.FloatKind], float64(0))
t(types.PrimitiveTypeMap[types.FloatKind], int(0))
t(types.PrimitiveTypeMap[types.FloatKind], int16(0))
t(types.PrimitiveTypeMap[types.FloatKind], int32(0))
t(types.PrimitiveTypeMap[types.FloatKind], int64(0))
t(types.PrimitiveTypeMap[types.FloatKind], int8(0))
t(types.PrimitiveTypeMap[types.FloatKind], uint(0))
t(types.PrimitiveTypeMap[types.FloatKind], uint16(0))
t(types.PrimitiveTypeMap[types.FloatKind], uint32(0))
t(types.PrimitiveTypeMap[types.FloatKind], uint64(0))
t(types.PrimitiveTypeMap[types.FloatKind], uint8(0))
t(types.BoolType, true)
t(types.StringType, "hi")
t(types.PrimitiveTypeMap[types.BoolKind], true)
t(types.PrimitiveTypeMap[types.StringKind], "hi")
var l []int
t(mustType(types.MakeListType(types.FloaTType)), l)
t(mustType(types.MakeListType(types.PrimitiveTypeMap[types.FloatKind])), l)
var m map[uint32]string
t(mustType(types.MakeMapType(types.FloaTType, types.StringType)), m)
t(mustType(types.MakeMapType(types.PrimitiveTypeMap[types.FloatKind], types.PrimitiveTypeMap[types.StringKind])), m)
t(mustType(types.MakeListType(types.ValueType)), types.List{})
t(mustType(types.MakeSetType(types.ValueType)), types.Set{})
t(mustType(types.MakeMapType(types.ValueType, types.ValueType)), types.Map{})
t(mustType(types.MakeRefType(types.ValueType)), types.Ref{})
t(mustType(types.MakeListType(types.PrimitiveTypeMap[types.ValueKind])), types.List{})
t(mustType(types.MakeSetType(types.PrimitiveTypeMap[types.ValueKind])), types.Set{})
t(mustType(types.MakeMapType(types.PrimitiveTypeMap[types.ValueKind], types.PrimitiveTypeMap[types.ValueKind])), types.Map{})
t(mustType(types.MakeRefType(types.PrimitiveTypeMap[types.ValueKind])), types.Ref{})
type TestStruct struct {
Str string
@@ -77,14 +77,14 @@ func TestMarshalTypeType(tt *testing.T) {
}
var str TestStruct
t(mustType(types.MakeStructTypeFromFields("TestStruct", types.FieldMap{
"str": types.StringType,
"num": types.FloaTType,
"str": types.PrimitiveTypeMap[types.StringKind],
"num": types.PrimitiveTypeMap[types.FloatKind],
})), str)
// Same again to test caching
t(mustType(types.MakeStructTypeFromFields("TestStruct", types.FieldMap{
"str": types.StringType,
"num": types.FloaTType,
"str": types.PrimitiveTypeMap[types.StringKind],
"num": types.PrimitiveTypeMap[types.FloatKind],
})), str)
anonStruct := struct {
@@ -93,7 +93,7 @@ func TestMarshalTypeType(tt *testing.T) {
true,
}
t(mustType(types.MakeStructTypeFromFields("", types.FieldMap{
"b": types.BoolType,
"b": types.PrimitiveTypeMap[types.BoolKind],
})), anonStruct)
type TestNestedStruct struct {
@@ -103,12 +103,12 @@ func TestMarshalTypeType(tt *testing.T) {
}
var nestedStruct TestNestedStruct
t(mustType(types.MakeStructTypeFromFields("TestNestedStruct", types.FieldMap{
"a": mustType(types.MakeListType(types.FloaTType)),
"a": mustType(types.MakeListType(types.PrimitiveTypeMap[types.FloatKind])),
"b": mustType(types.MakeStructTypeFromFields("TestStruct", types.FieldMap{
"str": types.StringType,
"num": types.FloaTType,
"str": types.PrimitiveTypeMap[types.StringKind],
"num": types.PrimitiveTypeMap[types.FloatKind],
})),
"c": types.FloaTType,
"c": types.PrimitiveTypeMap[types.FloatKind],
})), nestedStruct)
type testStruct struct {
@@ -117,8 +117,8 @@ func TestMarshalTypeType(tt *testing.T) {
}
var ts testStruct
t(mustType(types.MakeStructTypeFromFields("TestStruct", types.FieldMap{
"str": types.StringType,
"num": types.FloaTType,
"str": types.PrimitiveTypeMap[types.StringKind],
"num": types.PrimitiveTypeMap[types.FloatKind],
})), ts)
}
@@ -149,8 +149,8 @@ func TestMarshalTypeEmbeddedStruct(t *testing.T) {
assert.NoError(err)
assert.True(mustType(types.MakeStructTypeFromFields("TestStruct", types.FieldMap{
"a": types.FloaTType,
"b": types.BoolType,
"a": types.PrimitiveTypeMap[types.FloatKind],
"b": types.PrimitiveTypeMap[types.BoolKind],
})).Equals(typ))
}
@@ -170,7 +170,7 @@ func TestMarshalTypeEmbeddedStructSkip(t *testing.T) {
assert.NoError(err)
assert.True(mustType(types.MakeStructTypeFromFields("TestStruct", types.FieldMap{
"a": types.FloaTType,
"a": types.PrimitiveTypeMap[types.FloatKind],
})).Equals(typ))
}
@@ -189,9 +189,9 @@ func TestMarshalTypeEmbeddedStructNamed(t *testing.T) {
typ := mustMarshalType(types.Format_7_18, s)
assert.True(mustType(types.MakeStructTypeFromFields("TestStruct", types.FieldMap{
"a": types.FloaTType,
"a": types.PrimitiveTypeMap[types.FloatKind],
"em": mustType(types.MakeStructTypeFromFields("EmbeddedStruct", types.FieldMap{
"b": types.BoolType,
"b": types.PrimitiveTypeMap[types.BoolKind],
})),
})).Equals(typ))
}
@@ -214,7 +214,7 @@ func TestMarshalTypeEncodeTaggingSkip(t *testing.T) {
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"def": types.BoolType,
"def": types.PrimitiveTypeMap[types.BoolKind],
})).Equals(typ))
}
@@ -230,9 +230,9 @@ func TestMarshalTypeNamedFields(t *testing.T) {
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"a": types.FloaTType,
"B": types.BoolType,
"ccc": types.StringType,
"a": types.PrimitiveTypeMap[types.FloatKind],
"B": types.PrimitiveTypeMap[types.BoolKind],
"ccc": types.PrimitiveTypeMap[types.StringKind],
})).Equals(typ))
}
@@ -253,7 +253,7 @@ func TestMarshalTypeOmitEmpty(t *testing.T) {
var s S
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeStructType("S", types.StructField{Name: "string", Type: types.StringType, Optional: true})).Equals(typ))
assert.True(mustType(types.MakeStructType("S", types.StructField{Name: "string", Type: types.PrimitiveTypeMap[types.StringKind], Optional: true})).Equals(typ))
}
func SkipExampleMarshalType() {
@@ -281,7 +281,7 @@ func TestMarshalTypeSlice(t *testing.T) {
s := []string{"a", "b", "c"}
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeListType(types.StringType)).Equals(typ))
assert.True(mustType(types.MakeListType(types.PrimitiveTypeMap[types.StringKind])).Equals(typ))
}
func TestMarshalTypeArray(t *testing.T) {
@@ -290,7 +290,7 @@ func TestMarshalTypeArray(t *testing.T) {
a := [3]int{1, 2, 3}
typ, err := MarshalType(types.Format_7_18, a)
assert.NoError(err)
assert.True(mustType(types.MakeListType(types.FloaTType)).Equals(typ))
assert.True(mustType(types.MakeListType(types.PrimitiveTypeMap[types.FloatKind])).Equals(typ))
}
func TestMarshalTypeStructWithSlice(t *testing.T) {
@@ -303,7 +303,7 @@ func TestMarshalTypeStructWithSlice(t *testing.T) {
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"list": mustType(types.MakeListType(types.FloaTType)),
"list": mustType(types.MakeListType(types.PrimitiveTypeMap[types.FloatKind])),
})).Equals(typ))
}
@@ -325,7 +325,7 @@ func TestMarshalTypeRecursive(t *testing.T) {
},
types.StructField{
Name: "value",
Type: types.FloaTType,
Type: types.PrimitiveTypeMap[types.FloatKind],
},
))
assert.True(typ2.Equals(typ))
@@ -337,7 +337,7 @@ func TestMarshalTypeMap(t *testing.T) {
var m map[string]int
typ, err := MarshalType(types.Format_7_18, m)
assert.NoError(err)
assert.True(mustType(types.MakeMapType(types.StringType, types.FloaTType)).Equals(typ))
assert.True(mustType(types.MakeMapType(types.PrimitiveTypeMap[types.StringKind], types.PrimitiveTypeMap[types.FloatKind])).Equals(typ))
type S struct {
N string
@@ -348,9 +348,9 @@ func TestMarshalTypeMap(t *testing.T) {
assert.NoError(err)
assert.True(mustType(types.MakeMapType(
mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"n": types.StringType,
"n": types.PrimitiveTypeMap[types.StringKind],
})),
types.BoolType)).Equals(typ))
types.PrimitiveTypeMap[types.BoolKind])).Equals(typ))
}
func TestMarshalTypeSet(t *testing.T) {
@@ -373,14 +373,14 @@ func TestMarshalTypeSet(t *testing.T) {
emptyStructType := mustType(types.MakeStructTypeFromFields("", types.FieldMap{}))
assert.True(mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"a": mustType(types.MakeSetType(types.FloaTType)),
"b": mustType(types.MakeMapType(types.FloaTType, emptyStructType)),
"c": mustType(types.MakeMapType(types.FloaTType, types.StringType)),
"d": mustType(types.MakeSetType(types.StringType)),
"e": mustType(types.MakeMapType(types.StringType, emptyStructType)),
"f": mustType(types.MakeMapType(types.StringType, types.FloaTType)),
"g": mustType(types.MakeSetType(types.FloaTType)),
"h": types.StringType,
"a": mustType(types.MakeSetType(types.PrimitiveTypeMap[types.FloatKind])),
"b": mustType(types.MakeMapType(types.PrimitiveTypeMap[types.FloatKind], emptyStructType)),
"c": mustType(types.MakeMapType(types.PrimitiveTypeMap[types.FloatKind], types.PrimitiveTypeMap[types.StringKind])),
"d": mustType(types.MakeSetType(types.PrimitiveTypeMap[types.StringKind])),
"e": mustType(types.MakeMapType(types.PrimitiveTypeMap[types.StringKind], emptyStructType)),
"f": mustType(types.MakeMapType(types.PrimitiveTypeMap[types.StringKind], types.PrimitiveTypeMap[types.FloatKind])),
"g": mustType(types.MakeSetType(types.PrimitiveTypeMap[types.FloatKind])),
"h": types.PrimitiveTypeMap[types.StringKind],
})).Equals(typ))
}
@@ -395,22 +395,22 @@ func TestEncodeTypeOpt(t *testing.T) {
{
[]string{},
Opt{},
mustType(types.MakeListType(types.StringType)),
mustType(types.MakeListType(types.PrimitiveTypeMap[types.StringKind])),
},
{
[]string{},
Opt{Set: true},
mustType(types.MakeSetType(types.StringType)),
mustType(types.MakeSetType(types.PrimitiveTypeMap[types.StringKind])),
},
{
map[string]struct{}{},
Opt{},
mustType(types.MakeMapType(types.StringType, mustType(types.MakeStructType("")))),
mustType(types.MakeMapType(types.PrimitiveTypeMap[types.StringKind], mustType(types.MakeStructType("")))),
},
{
map[string]struct{}{},
Opt{Set: true},
mustType(types.MakeSetType(types.StringType)),
mustType(types.MakeSetType(types.PrimitiveTypeMap[types.StringKind])),
},
}
@@ -434,9 +434,9 @@ func TestMarshalTypeSetWithTags(t *testing.T) {
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeStructType("S",
types.StructField{Name: "foo", Type: mustType(types.MakeSetType(types.FloaTType)), Optional: false},
types.StructField{Name: "b", Type: mustType(types.MakeSetType(types.FloaTType)), Optional: true},
types.StructField{Name: "bar", Type: mustType(types.MakeSetType(types.FloaTType)), Optional: true},
types.StructField{Name: "foo", Type: mustType(types.MakeSetType(types.PrimitiveTypeMap[types.FloatKind])), Optional: false},
types.StructField{Name: "b", Type: mustType(types.MakeSetType(types.PrimitiveTypeMap[types.FloatKind])), Optional: true},
types.StructField{Name: "bar", Type: mustType(types.MakeSetType(types.PrimitiveTypeMap[types.FloatKind])), Optional: true},
)).Equals(typ))
}
@@ -464,7 +464,7 @@ func TestMarshalTypeCanSkipUnexportedField(t *testing.T) {
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"abc": types.FloaTType,
"abc": types.PrimitiveTypeMap[types.FloatKind],
})).Equals(typ))
}
@@ -480,7 +480,7 @@ func TestMarshalTypeOriginal(t *testing.T) {
typ, err := MarshalType(types.Format_7_18, s)
assert.NoError(err)
assert.True(mustType(types.MakeStructType("S",
types.StructField{Name: "foo", Type: types.FloaTType, Optional: true},
types.StructField{Name: "foo", Type: types.PrimitiveTypeMap[types.FloatKind], Optional: true},
)).Equals(typ))
}
@@ -497,17 +497,17 @@ func TestMarshalTypeNomsTypes(t *testing.T) {
var s S
assert.True(mustMarshalType(types.Format_7_18, s).Equals(
mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"blob": types.BlobType,
"bool": types.BoolType,
"number": types.FloaTType,
"string": types.StringType,
"type": types.TypeType,
"blob": types.PrimitiveTypeMap[types.BlobKind],
"bool": types.PrimitiveTypeMap[types.BoolKind],
"number": types.PrimitiveTypeMap[types.FloatKind],
"string": types.PrimitiveTypeMap[types.StringKind],
"type": types.PrimitiveTypeMap[types.TypeKind],
})),
))
}
func (t primitiveType) MarshalNomsType() (*types.Type, error) {
return types.FloaTType, nil
return types.PrimitiveTypeMap[types.FloatKind], nil
}
func TestTypeMarshalerPrimitiveType(t *testing.T) {
@@ -515,11 +515,11 @@ func TestTypeMarshalerPrimitiveType(t *testing.T) {
var u primitiveType
typ := mustMarshalType(types.Format_7_18, u)
assert.Equal(types.FloaTType, typ)
assert.Equal(types.PrimitiveTypeMap[types.FloatKind], typ)
}
func (u primitiveSliceType) MarshalNomsType() (*types.Type, error) {
return types.StringType, nil
return types.PrimitiveTypeMap[types.StringKind], nil
}
func TestTypeMarshalerPrimitiveSliceType(t *testing.T) {
@@ -527,11 +527,11 @@ func TestTypeMarshalerPrimitiveSliceType(t *testing.T) {
var u primitiveSliceType
typ := mustMarshalType(types.Format_7_18, u)
assert.Equal(types.StringType, typ)
assert.Equal(types.PrimitiveTypeMap[types.StringKind], typ)
}
func (u primitiveMapType) MarshalNomsType() (*types.Type, error) {
return mustType(types.MakeSetType(types.StringType)), nil
return mustType(types.MakeSetType(types.PrimitiveTypeMap[types.StringKind])), nil
}
func TestTypeMarshalerPrimitiveMapType(t *testing.T) {
@@ -539,7 +539,7 @@ func TestTypeMarshalerPrimitiveMapType(t *testing.T) {
var u primitiveMapType
typ := mustMarshalType(types.Format_7_18, u)
assert.Equal(mustType(types.MakeSetType(types.StringType)), typ)
assert.Equal(mustType(types.MakeSetType(types.PrimitiveTypeMap[types.StringKind])), typ)
}
func TestTypeMarshalerPrimitiveStructTypeNoMarshalNomsType(t *testing.T) {
@@ -552,7 +552,7 @@ func TestTypeMarshalerPrimitiveStructTypeNoMarshalNomsType(t *testing.T) {
}
func (u builtinType) MarshalNomsType() (*types.Type, error) {
return types.StringType, nil
return types.PrimitiveTypeMap[types.StringKind], nil
}
func TestTypeMarshalerBuiltinType(t *testing.T) {
@@ -560,11 +560,11 @@ func TestTypeMarshalerBuiltinType(t *testing.T) {
var u builtinType
typ := mustMarshalType(types.Format_7_18, u)
assert.Equal(types.StringType, typ)
assert.Equal(types.PrimitiveTypeMap[types.StringKind], typ)
}
func (u wrappedMarshalerType) MarshalNomsType() (*types.Type, error) {
return types.FloaTType, nil
return types.PrimitiveTypeMap[types.FloatKind], nil
}
func TestTypeMarshalerWrapperMarshalerType(t *testing.T) {
@@ -572,7 +572,7 @@ func TestTypeMarshalerWrapperMarshalerType(t *testing.T) {
var u wrappedMarshalerType
typ := mustMarshalType(types.Format_7_18, u)
assert.Equal(types.FloaTType, typ)
assert.Equal(types.PrimitiveTypeMap[types.FloatKind], typ)
}
func (u returnsMarshalerError) MarshalNomsType() (*types.Type, error) {
@@ -601,7 +601,7 @@ func TestMarshalTypeStructName(t *testing.T) {
var ts TestStructWithNameImpl
typ := mustMarshalType(types.Format_7_18, ts)
assert.True(mustType(types.MakeStructType("A", types.StructField{Name: "x", Type: types.FloaTType, Optional: false})).Equals(typ), mustString(typ.Describe(context.Background())))
assert.True(mustType(types.MakeStructType("A", types.StructField{Name: "x", Type: types.PrimitiveTypeMap[types.FloatKind], Optional: false})).Equals(typ), mustString(typ.Describe(context.Background())))
}
func TestMarshalTypeStructName2(t *testing.T) {
@@ -609,7 +609,7 @@ func TestMarshalTypeStructName2(t *testing.T) {
var ts TestStructWithNameImpl2
typ := mustMarshalType(types.Format_7_18, ts)
assert.True(mustType(types.MakeStructType("", types.StructField{Name: "x", Type: types.FloaTType, Optional: false})).Equals(typ), mustString(typ.Describe(context.Background())))
assert.True(mustType(types.MakeStructType("", types.StructField{Name: "x", Type: types.PrimitiveTypeMap[types.FloatKind], Optional: false})).Equals(typ), mustString(typ.Describe(context.Background())))
}
type OutPhoto struct {

View File

@@ -201,17 +201,17 @@ func (p *Parser) parseSingleType() (*types.Type, error) {
func (p *Parser) parseSingleTypeWithToken(tok rune, tokenText string) (*types.Type, error) {
switch tokenText {
case "Bool":
return types.BoolType, nil
return types.PrimitiveTypeMap[types.BoolKind], nil
case "Blob":
return types.BlobType, nil
return types.PrimitiveTypeMap[types.BlobKind], nil
case "Float":
return types.FloaTType, nil
return types.PrimitiveTypeMap[types.FloatKind], nil
case "String":
return types.StringType, nil
return types.PrimitiveTypeMap[types.StringKind], nil
case "Type":
return types.TypeType, nil
return types.PrimitiveTypeMap[types.TypeKind], nil
case "Value":
return types.ValueType, nil
return types.PrimitiveTypeMap[types.ValueKind], nil
case "Struct":
return p.parseStructType()
case "Map":

View File

@@ -88,45 +88,45 @@ func assertParseError(t *testing.T, code, msg string) {
}
func TestSimpleTypes(t *testing.T) {
assertParseType(t, "Blob", types.BlobType)
assertParseType(t, "Bool", types.BoolType)
assertParseType(t, "Float", types.FloaTType)
assertParseType(t, "String", types.StringType)
assertParseType(t, "Value", types.ValueType)
assertParseType(t, "Type", types.TypeType)
assertParseType(t, "Blob", types.PrimitiveTypeMap[types.BlobKind])
assertParseType(t, "Bool", types.PrimitiveTypeMap[types.BoolKind])
assertParseType(t, "Float", types.PrimitiveTypeMap[types.FloatKind])
assertParseType(t, "String", types.PrimitiveTypeMap[types.StringKind])
assertParseType(t, "Value", types.PrimitiveTypeMap[types.ValueKind])
assertParseType(t, "Type", types.PrimitiveTypeMap[types.TypeKind])
}
func TestWhitespace(t *testing.T) {
for _, r := range " \t\n\r" {
assertParseType(t, string(r)+"Blob", types.BlobType)
assertParseType(t, "Blob"+string(r), types.BlobType)
assertParseType(t, string(r)+"Blob", types.PrimitiveTypeMap[types.BlobKind])
assertParseType(t, "Blob"+string(r), types.PrimitiveTypeMap[types.BlobKind])
}
}
func TestComments(t *testing.T) {
assertParseType(t, "/* */Blob", types.BlobType)
assertParseType(t, "Blob/* */", types.BlobType)
assertParseType(t, "Blob//", types.BlobType)
assertParseType(t, "//\nBlob", types.BlobType)
assertParseType(t, "/* */Blob", types.PrimitiveTypeMap[types.BlobKind])
assertParseType(t, "Blob/* */", types.PrimitiveTypeMap[types.BlobKind])
assertParseType(t, "Blob//", types.PrimitiveTypeMap[types.BlobKind])
assertParseType(t, "//\nBlob", types.PrimitiveTypeMap[types.BlobKind])
}
func TestCompoundTypes(t *testing.T) {
assertParseType(t, "List<>", mustType(types.MakeListType(mustType(types.MakeUnionType()))))
assertParseType(t, "List<Bool>", mustType(types.MakeListType(types.BoolType)))
assertParseType(t, "List<Bool>", mustType(types.MakeListType(types.PrimitiveTypeMap[types.BoolKind])))
assertParseError(t, "List<Bool, Float>", `Unexpected token ",", expected ">", example:1:11`)
assertParseError(t, "List<Bool", `Unexpected token EOF, expected ">", example:1:10`)
assertParseError(t, "List<", `Unexpected token EOF, expected Ident, example:1:6`)
assertParseError(t, "List", `Unexpected token EOF, expected "<", example:1:5`)
assertParseType(t, "Set<>", mustType(types.MakeSetType(mustType(types.MakeUnionType()))))
assertParseType(t, "Set<Bool>", mustType(types.MakeSetType(types.BoolType)))
assertParseType(t, "Set<Bool>", mustType(types.MakeSetType(types.PrimitiveTypeMap[types.BoolKind])))
assertParseError(t, "Set<Bool, Float>", `Unexpected token ",", expected ">", example:1:10`)
assertParseError(t, "Set<Bool", `Unexpected token EOF, expected ">", example:1:9`)
assertParseError(t, "Set<", `Unexpected token EOF, expected Ident, example:1:5`)
assertParseError(t, "Set", `Unexpected token EOF, expected "<", example:1:4`)
assertParseError(t, "Ref<>", `Unexpected token ">", expected Ident, example:1:6`)
assertParseType(t, "Ref<Bool>", mustType(types.MakeRefType(types.BoolType)))
assertParseType(t, "Ref<Bool>", mustType(types.MakeRefType(types.PrimitiveTypeMap[types.BoolKind])))
assertParseError(t, "Ref<Float, Bool>", `Unexpected token ",", expected ">", example:1:11`)
assertParseError(t, "Ref<Float", `Unexpected token EOF, expected ">", example:1:10`)
assertParseError(t, "Ref<", `Unexpected token EOF, expected Ident, example:1:5`)
@@ -143,7 +143,7 @@ func TestCompoundTypes(t *testing.T) {
assertParseError(t, "Cycle", `Unexpected token EOF, expected "<", example:1:6`)
assertParseType(t, "Map<>", mustType(types.MakeMapType(mustType(types.MakeUnionType()), mustType(types.MakeUnionType()))))
assertParseType(t, "Map<Bool, String>", mustType(types.MakeMapType(types.BoolType, types.StringType)))
assertParseType(t, "Map<Bool, String>", mustType(types.MakeMapType(types.PrimitiveTypeMap[types.BoolKind], types.PrimitiveTypeMap[types.StringKind])))
assertParseError(t, "Map<Bool,>", `Unexpected token ">", expected Ident, example:1:11`)
assertParseError(t, "Map<,Bool>", `Unexpected token ",", expected Ident, example:1:6`)
assertParseError(t, "Map<,>", `Unexpected token ",", expected Ident, example:1:6`)
@@ -160,21 +160,21 @@ func TestStructTypes(t *testing.T) {
assertParseType(t, `Struct S {
x: Float
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.FloaTType})))
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.PrimitiveTypeMap[types.FloatKind]})))
assertParseType(t, `Struct S {
x: Float,
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.FloaTType})))
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.PrimitiveTypeMap[types.FloatKind]})))
assertParseType(t, `Struct S {
x: Float,
y: String
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.FloaTType, "y": types.StringType})))
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.PrimitiveTypeMap[types.FloatKind], "y": types.PrimitiveTypeMap[types.StringKind]})))
assertParseType(t, `Struct S {
x: Float,
y: String,
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.FloaTType, "y": types.StringType})))
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{"x": types.PrimitiveTypeMap[types.FloatKind], "y": types.PrimitiveTypeMap[types.StringKind]})))
assertParseType(t, `Struct S {
x: Float,
@@ -182,16 +182,16 @@ func TestStructTypes(t *testing.T) {
z: String,
},
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"x": types.FloaTType,
"y": mustType(types.MakeStructTypeFromFields("", types.FieldMap{"z": types.StringType})),
"x": types.PrimitiveTypeMap[types.FloatKind],
"y": mustType(types.MakeStructTypeFromFields("", types.FieldMap{"z": types.PrimitiveTypeMap[types.StringKind]})),
})))
assertParseType(t, `Struct S {
x?: Float,
y: String,
}`, mustType(types.MakeStructType("S",
types.StructField{Name: "x", Type: types.FloaTType, Optional: true},
types.StructField{Name: "y", Type: types.StringType, Optional: false},
types.StructField{Name: "x", Type: types.PrimitiveTypeMap[types.FloatKind], Optional: true},
types.StructField{Name: "y", Type: types.PrimitiveTypeMap[types.StringKind], Optional: false},
)))
assertParseError(t, `Struct S {
@@ -219,26 +219,26 @@ func TestStructTypes(t *testing.T) {
}
func TestUnionTypes(t *testing.T) {
assertParseType(t, "Blob | Bool", mustType(types.MakeUnionType(types.BlobType, types.BoolType)))
assertParseType(t, "Bool | Float | String", mustType(types.MakeUnionType(types.BoolType, types.FloaTType, types.StringType)))
assertParseType(t, "List<Bool | Float>", mustType(types.MakeListType(mustType(types.MakeUnionType(types.BoolType, types.FloaTType)))))
assertParseType(t, "Blob | Bool", mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.BlobKind], types.PrimitiveTypeMap[types.BoolKind])))
assertParseType(t, "Bool | Float | String", mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.BoolKind], types.PrimitiveTypeMap[types.FloatKind], types.PrimitiveTypeMap[types.StringKind])))
assertParseType(t, "List<Bool | Float>", mustType(types.MakeListType(mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.BoolKind], types.PrimitiveTypeMap[types.FloatKind])))))
assertParseType(t, "Map<Bool | Float, Bool | Float>",
mustType(types.MakeMapType(
mustType(types.MakeUnionType(types.BoolType, types.FloaTType)),
mustType(types.MakeUnionType(types.BoolType, types.FloaTType)),
mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.BoolKind], types.PrimitiveTypeMap[types.FloatKind])),
mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.BoolKind], types.PrimitiveTypeMap[types.FloatKind])),
)),
)
assertParseType(t, `Struct S {
x: Float | Bool
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"x": mustType(types.MakeUnionType(types.BoolType, types.FloaTType)),
"x": mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.BoolKind], types.PrimitiveTypeMap[types.FloatKind])),
})))
assertParseType(t, `Struct S {
x: Float | Bool,
y: String
}`, mustType(types.MakeStructTypeFromFields("S", types.FieldMap{
"x": mustType(types.MakeUnionType(types.BoolType, types.FloaTType)),
"y": types.StringType,
"x": mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.BoolKind], types.PrimitiveTypeMap[types.FloatKind])),
"y": types.PrimitiveTypeMap[types.StringKind],
})))
assertParseError(t, "Bool |", "Unexpected token EOF, expected Ident, example:1:7")
@@ -249,8 +249,8 @@ func TestUnionTypes(t *testing.T) {
func TestValuePrimitives(t *testing.T) {
vs := newTestValueStore()
assertParse(t, vs, "Float", types.FloaTType)
assertParse(t, vs, "Float | String", mustType(types.MakeUnionType(types.FloaTType, types.StringType)))
assertParse(t, vs, "Float", types.PrimitiveTypeMap[types.FloatKind])
assertParse(t, vs, "Float | String", mustType(types.MakeUnionType(types.PrimitiveTypeMap[types.FloatKind], types.PrimitiveTypeMap[types.StringKind])))
assertParse(t, vs, "true", types.Bool(true))
assertParse(t, vs, "false", types.Bool(false))
@@ -306,10 +306,10 @@ func TestValueList(t *testing.T) {
assertParse(t, vs, `[42,
Bool,
]`, mustValue(types.NewList(context.Background(), vs, types.Float(42), types.BoolType)))
]`, mustValue(types.NewList(context.Background(), vs, types.Float(42), types.PrimitiveTypeMap[types.BoolKind])))
assertParse(t, vs, `[42,
Bool
]`, mustValue(types.NewList(context.Background(), vs, types.Float(42), types.BoolType)))
]`, mustValue(types.NewList(context.Background(), vs, types.Float(42), types.PrimitiveTypeMap[types.BoolKind])))
}
func TestValueSet(t *testing.T) {
@@ -328,10 +328,10 @@ func TestValueSet(t *testing.T) {
assertParse(t, vs, `set {42,
Bool,
}`, mustValue(types.NewSet(context.Background(), vs, types.Float(42), types.BoolType)))
}`, mustValue(types.NewSet(context.Background(), vs, types.Float(42), types.PrimitiveTypeMap[types.BoolKind])))
assertParse(t, vs, `set {42,
Bool
}`, mustValue(types.NewSet(context.Background(), vs, types.Float(42), types.BoolType)))
}`, mustValue(types.NewSet(context.Background(), vs, types.Float(42), types.PrimitiveTypeMap[types.BoolKind])))
}
func TestValueMap(t *testing.T) {
@@ -352,17 +352,17 @@ func TestValueMap(t *testing.T) {
assertParse(t, vs, `map {42:
Bool,
}`, mustValue(types.NewMap(context.Background(), vs, types.Float(42), types.BoolType)))
}`, mustValue(types.NewMap(context.Background(), vs, types.Float(42), types.PrimitiveTypeMap[types.BoolKind])))
assertParse(t, vs, `map {42:
Bool
}`, mustValue(types.NewMap(context.Background(), vs, types.Float(42), types.BoolType)))
}`, mustValue(types.NewMap(context.Background(), vs, types.Float(42), types.PrimitiveTypeMap[types.BoolKind])))
}
func TestValueType(t *testing.T) {
vs := newTestValueStore()
assertParse(t, vs, "Bool", types.BoolType)
assertParse(t, vs, "Float", types.FloaTType)
assertParse(t, vs, "String", types.StringType)
assertParse(t, vs, "Bool", types.PrimitiveTypeMap[types.BoolKind])
assertParse(t, vs, "Float", types.PrimitiveTypeMap[types.FloatKind])
assertParse(t, vs, "String", types.PrimitiveTypeMap[types.StringKind])
}
func TestValueStruct(t *testing.T) {
@@ -470,7 +470,7 @@ func TestRoundTrips(t *testing.T) {
test(mustValue(types.NewStruct(types.Format_7_18, "", nil)))
test(mustValue(types.NewStruct(types.Format_7_18, "Float", nil)))
test(mustValue(types.NewStruct(types.Format_7_18, "Float", types.StructData{
"Float": types.FloaTType,
"Float": types.PrimitiveTypeMap[types.FloatKind],
})))
test(mustType(types.MakeStructType("S", types.StructField{

View File

@@ -211,6 +211,17 @@ func (b Blob) Value(ctx context.Context) (Value, error) {
return b, nil
}
func (b Blob) isPrimitive() bool {
return true
}
func (b Blob) Kind() NomsKind {
if b.sequence == nil {
return BlobKind
}
return b.sequence.Kind()
}
func (b Blob) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -464,3 +475,23 @@ func readBlob(ctx context.Context, r io.Reader, vrw ValueReadWriter) (Blob, erro
return newBlob(seq), nil
}
func (Blob) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(BlobKind, targetKind)
}
func (b Blob) readFrom(nbf *NomsBinFormat, bnr *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (b Blob) skip(nbf *NomsBinFormat, bnr *binaryNomsReader) {
panic("unreachable")
}
func (b Blob) String() string {
panic("unreachable")
}
func (b Blob) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -44,7 +44,7 @@ func newBlobLeafSequence(vrw ValueReadWriter, data []byte) (sequence, error) {
count := uint64(len(data))
w.writeCount(count)
offsets[sequencePartValues] = w.offset
w.writeBytes(data)
w.writeRaw(data)
return blobLeafSequence{newLeafSequence(vrw, w.data(), offsets, count)}, nil
}
@@ -75,5 +75,5 @@ func (bl blobLeafSequence) getItem(idx int) (sequenceItem, error) {
}
func (bl blobLeafSequence) typeOf() (*Type, error) {
return BlobType, nil
return PrimitiveTypeMap[BlobKind], nil
}

View File

@@ -63,7 +63,7 @@ func newBlobTestSuite(size uint, expectChunkCount int, expectPrependChunkDiff in
return &blobTestSuite{
collectionTestSuite: collectionTestSuite{
col: blob,
expectType: BlobType,
expectType: PrimitiveTypeMap[BlobKind],
expectLen: uint64(length),
expectChunkCount: expectChunkCount,
expectPrependChunkDiff: expectPrependChunkDiff,

View File

@@ -23,6 +23,7 @@ package types
import (
"context"
"strconv"
"github.com/liquidata-inc/dolt/go/store/hash"
)
@@ -50,6 +51,10 @@ func (b Bool) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(b, nbf)
}
func (b Bool) isPrimitive() bool {
return true
}
func (b Bool) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -59,7 +64,7 @@ func (b Bool) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (b Bool) typeOf() (*Type, error) {
return BoolType, nil
return PrimitiveTypeMap[BoolKind], nil
}
func (b Bool) Kind() NomsKind {
@@ -82,9 +87,73 @@ func (b Bool) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
return nil
}
func (b Bool) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
if bool(b) {
return []byte{byte(BoolKind), 1}, nil
}
return []byte{byte(BoolKind), 0}, nil
func (b Bool) readFrom(nbf *NomsBinFormat, bnr *binaryNomsReader) (Value, error) {
return Bool(bnr.readBool()), nil
}
func (b Bool) skip(nbf *NomsBinFormat, bnr *binaryNomsReader) {
bnr.skipUint8()
}
func (Bool) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
switch targetKind {
case BoolKind:
return func(val Value) (Value, error) {
return val, nil
}, nil
case FloatKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
b := val.(Bool)
if b {
return Float(1), nil
}
return Float(0), nil
}, nil
case IntKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
b := val.(Bool)
if b {
return Int(1), nil
}
return Int(0), nil
}, nil
case NullKind:
return func(Value) (Value, error) {
return NullValue, nil
}, nil
case StringKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
b := val.(Bool)
if b {
return String("true"), nil
}
return String("false"), nil
}, nil
case UintKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
b := val.(Bool)
if b {
return Uint(1), nil
}
return Uint(0), nil
}, nil
}
return nil, CreateNoConversionError(BoolKind, targetKind)
}
func (b Bool) HumanReadableString() string {
return strconv.FormatBool(bool(b))
}

View File

@@ -32,37 +32,19 @@ import (
const initialBufferSize = 2048
type valueBytes interface {
valueBytes(*NomsBinFormat) ([]byte, error)
}
func EncodeValue(v Value, nbf *NomsBinFormat) (chunks.Chunk, error) {
switch v := v.(type) {
case valueBytes:
bytes, err := v.valueBytes(nbf)
if err != nil {
return chunks.EmptyChunk, err
}
return chunks.NewChunk(bytes), nil
case *Type:
if v.Kind() == UnknownKind {
return chunks.EmptyChunk, ErrUnknownType
}
w := newBinaryNomsWriter()
err := v.writeTo(&w, nbf)
if err != nil {
return chunks.EmptyChunk, err
}
return chunks.NewChunk(w.data()), nil
if v.Kind() == UnknownKind {
return chunks.EmptyChunk, ErrUnknownType
}
return chunks.EmptyChunk, ErrUnknownType
w := binaryNomsWriter{make([]byte, 4), 0}
err := v.writeTo(&w, nbf)
if err != nil {
return chunks.EmptyChunk, err
}
return chunks.NewChunk(w.data()), nil
}
func decodeFromBytes(data []byte, vrw ValueReadWriter) (Value, error) {
@@ -98,7 +80,6 @@ func DecodeValue(c chunks.Chunk, vrw ValueReadWriter) (Value, error) {
type nomsWriter interface {
writeBool(b bool)
writeBytes(v []byte)
writeCount(count uint64)
writeHash(h hash.Hash)
writeFloat(v Float, nbf *NomsBinFormat)
@@ -115,6 +96,17 @@ type binaryNomsReader struct {
offset uint32
}
func (b *binaryNomsReader) readBytes(count uint32) []byte {
v := make([]byte, count)
copy(v, b.buff[b.offset:b.offset+count])
b.offset += count
return v
}
func (b *binaryNomsReader) skipBytes(count uint32) {
b.offset += count
}
func (b *binaryNomsReader) pos() uint32 {
return b.offset
}
@@ -133,6 +125,12 @@ func (b *binaryNomsReader) skipUint8() {
b.offset++
}
func (b *binaryNomsReader) readUint16() uint16 {
v := binary.BigEndian.Uint16(b.buff[b.offset:])
b.offset += 2
return v
}
func (b *binaryNomsReader) peekKind() NomsKind {
return NomsKind(b.peekUint8())
}
@@ -146,39 +144,30 @@ func (b *binaryNomsReader) skipKind() {
}
func (b *binaryNomsReader) readCount() uint64 {
v, count := binary.Uvarint(b.buff[b.offset:])
b.offset += uint32(count)
return v
return b.readUint()
}
func (b *binaryNomsReader) skipCount() {
_, count := binary.Uvarint(b.buff[b.offset:])
b.offset += uint32(count)
b.skipUint()
}
func (b *binaryNomsReader) readFloat(nbf *NomsBinFormat) Float {
func (b *binaryNomsReader) readFloat(nbf *NomsBinFormat) float64 {
if isFormat_7_18(nbf) {
// b.assertCanRead(binary.MaxVarintLen64 * 2)
i, count := binary.Varint(b.buff[b.offset:])
b.offset += uint32(count)
exp, count2 := binary.Varint(b.buff[b.offset:])
b.offset += uint32(count2)
return Float(fracExpToFloat(i, int(exp)))
i := b.readInt()
exp := b.readInt()
return fracExpToFloat(i, int(exp))
} else {
floatbits := binary.BigEndian.Uint64(b.buff[b.offset:])
b.offset += 8
return Float(math.Float64frombits(floatbits))
floatbits := binary.BigEndian.Uint64(b.readBytes(8))
return math.Float64frombits(floatbits)
}
}
func (b *binaryNomsReader) skipFloat(nbf *NomsBinFormat) {
if isFormat_7_18(nbf) {
_, count := binary.Varint(b.buff[b.offset:])
b.offset += uint32(count)
_, count2 := binary.Varint(b.buff[b.offset:])
b.offset += uint32(count2)
b.skipInt()
b.skipInt()
} else {
b.offset += 8
b.skipBytes(8)
}
}
@@ -187,39 +176,21 @@ func (b *binaryNomsReader) skipInt() {
b.offset += uint32(count)
}
func (b *binaryNomsReader) skipUint() {
_, count := binary.Uvarint(b.buff[b.offset:])
b.offset += uint32(count)
}
func (b *binaryNomsReader) readInt() Int {
func (b *binaryNomsReader) readInt() int64 {
v, count := binary.Varint(b.buff[b.offset:])
b.offset += uint32(count)
return Int(v)
}
func (b *binaryNomsReader) readUint() Uint {
v, count := binary.Uvarint(b.buff[b.offset:])
b.offset += uint32(count)
return Uint(v)
}
func (b *binaryNomsReader) readUint16() uint16 {
v := binary.BigEndian.Uint16(b.buff[b.offset:])
b.offset += 2
return v
}
func (b *binaryNomsReader) readUUID() UUID {
id := UUID{}
copy(id[:uuidNumBytes], b.buff[b.offset:])
b.offset += uuidNumBytes
return id
func (b *binaryNomsReader) readUint() uint64 {
v, count := binary.Uvarint(b.buff[b.offset:])
b.offset += uint32(count)
return v
}
func (b *binaryNomsReader) skipUUID() {
b.offset += uuidNumBytes
func (b *binaryNomsReader) skipUint() {
_, count := binary.Uvarint(b.buff[b.offset:])
b.offset += uint32(count)
}
func (b *binaryNomsReader) readBool() bool {
@@ -243,18 +214,6 @@ func (b *binaryNomsReader) skipString() {
b.offset += size
}
func (b *binaryNomsReader) readInlineBlob() (InlineBlob, error) {
len := uint32(b.readUint16())
v := make([]byte, len)
copy(v, b.buff[b.offset:b.offset+len])
b.offset += len
return InlineBlob(v), nil
}
func (b *binaryNomsReader) skipInlineBlob() {
b.offset += uint32(b.readUint16())
}
func (b *binaryNomsReader) readHash() hash.Hash {
h := hash.Hash{}
copy(h[:], b.buff[b.offset:b.offset+hash.ByteLen])
@@ -303,13 +262,6 @@ func (b *binaryNomsWriter) ensureCapacity(n uint32) {
copy(b.buff, old)
}
func (b *binaryNomsWriter) writeBytes(v []byte) {
size := uint32(len(v))
b.ensureCapacity(size)
copy(b.buff[b.offset:], v)
b.offset += size
}
func (b *binaryNomsWriter) writeUint8(v uint8) {
b.ensureCapacity(1)
b.buff[b.offset] = byte(v)
@@ -379,7 +331,8 @@ func (b *binaryNomsWriter) writeHash(h hash.Hash) {
}
func (b *binaryNomsWriter) writeRaw(buff []byte) {
b.ensureCapacity(uint32(len(buff)))
size := uint32(len(buff))
b.ensureCapacity(size)
copy(b.buff[b.offset:], buff)
b.offset += uint32(len(buff))
b.offset += size
}

View File

@@ -54,7 +54,7 @@ func TestCodecReadFloat(t *testing.T) {
test := func(data []byte, exp float64) {
r := binaryNomsReader{buff: data}
n := r.readFloat(Format_7_18)
assert.Equal(t, exp, float64(n))
assert.Equal(t, exp, n)
assert.Equal(t, len(data), int(r.offset))
}

View File

@@ -27,140 +27,140 @@ func TestContainCommonSupertype(t *testing.T) {
out bool
}{
// bool & any -> true
{ValueType, StringType, true},
{PrimitiveTypeMap[ValueKind], PrimitiveTypeMap[StringKind], true},
// ref<bool> & ref<bool> -> true
{mustType(MakeRefType(BoolType)), mustType(MakeRefType(BoolType)), true},
{mustType(MakeRefType(PrimitiveTypeMap[BoolKind])), mustType(MakeRefType(PrimitiveTypeMap[BoolKind])), true},
// ref<number> & ref<string> -> false
{mustType(MakeRefType(FloaTType)), mustType(MakeRefType(StringType)), false},
{mustType(MakeRefType(PrimitiveTypeMap[FloatKind])), mustType(MakeRefType(PrimitiveTypeMap[StringKind])), false},
// set<bool> & set<bool> -> true
{mustType(MakeSetType(BoolType)), mustType(MakeSetType(BoolType)), true},
{mustType(MakeSetType(PrimitiveTypeMap[BoolKind])), mustType(MakeSetType(PrimitiveTypeMap[BoolKind])), true},
// set<bool> & set<string> -> false
{mustType(MakeSetType(BoolType)), mustType(MakeSetType(StringType)), false},
{mustType(MakeSetType(PrimitiveTypeMap[BoolKind])), mustType(MakeSetType(PrimitiveTypeMap[StringKind])), false},
// list<blob> & list<blob> -> true
{mustType(MakeListType(BlobType)), mustType(MakeListType(BlobType)), true},
{mustType(MakeListType(PrimitiveTypeMap[BlobKind])), mustType(MakeListType(PrimitiveTypeMap[BlobKind])), true},
// list<blob> & list<string> -> false
{mustType(MakeListType(BlobType)), mustType(MakeListType(StringType)), false},
{mustType(MakeListType(PrimitiveTypeMap[BlobKind])), mustType(MakeListType(PrimitiveTypeMap[StringKind])), false},
// list<blob|string|number> & list<string|bool> -> true
{mustType(MakeListType(mustType(MakeUnionType(BlobType, StringType, FloaTType)))), mustType(MakeListType(mustType(MakeUnionType(StringType, BoolType)))), true},
{mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[BlobKind], PrimitiveTypeMap[StringKind], PrimitiveTypeMap[FloatKind])))), mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind])))), true},
// list<blob|string> & list<number|bool> -> false
{mustType(MakeListType(mustType(MakeUnionType(BlobType, StringType)))), mustType(MakeListType(mustType(MakeUnionType(FloaTType, BoolType)))), false},
{mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[BlobKind], PrimitiveTypeMap[StringKind])))), mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))), false},
// map<bool,bool> & map<bool,bool> -> true
{mustType(MakeMapType(BoolType, BoolType)), mustType(MakeMapType(BoolType, BoolType)), true},
{mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])), mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])), true},
// map<bool,bool> & map<bool,string> -> false
{mustType(MakeMapType(BoolType, BoolType)), mustType(MakeMapType(BoolType, StringType)), false},
{mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])), mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])), false},
// map<bool,bool> & map<string,bool> -> false
{mustType(MakeMapType(BoolType, BoolType)), mustType(MakeMapType(StringType, BoolType)), false},
{mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])), mustType(MakeMapType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind])), false},
// map<bool,bool> & map<string,bool> -> false
{mustType(MakeMapType(BoolType, BoolType)), mustType(MakeMapType(StringType, BoolType)), false},
{mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])), mustType(MakeMapType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind])), false},
// map<struct{foo:string},bool> & map<struct{foo:string,bar:string},bool> -> false
{mustType(MakeMapType(mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})), BoolType)),
mustType(MakeMapType(mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType, "bar": StringType})), BoolType)), false},
{mustType(MakeMapType(mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})), PrimitiveTypeMap[BoolKind])),
mustType(MakeMapType(mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind], "bar": PrimitiveTypeMap[StringKind]})), PrimitiveTypeMap[BoolKind])), false},
// map<string|blob,string> & map<number|string,string> -> true
{mustType(MakeMapType(mustType(MakeUnionType(StringType, BlobType)), StringType)),
mustType(MakeMapType(mustType(MakeUnionType(FloaTType, StringType)), StringType)), true},
{mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BlobKind])), PrimitiveTypeMap[StringKind])),
mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])), PrimitiveTypeMap[StringKind])), true},
// map<blob|bool,string> & map<number|string,string> -> false
{mustType(MakeMapType(mustType(MakeUnionType(BlobType, BoolType)), StringType)),
mustType(MakeMapType(mustType(MakeUnionType(FloaTType, StringType)), StringType)), false},
{mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[BlobKind], PrimitiveTypeMap[BoolKind])), PrimitiveTypeMap[StringKind])),
mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])), PrimitiveTypeMap[StringKind])), false},
// bool & string|bool|blob -> true
{BoolType, mustType(MakeUnionType(StringType, BoolType, BlobType)), true},
{PrimitiveTypeMap[BoolKind], mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BlobKind])), true},
// string|bool|blob & blob -> true
{mustType(MakeUnionType(StringType, BoolType, BlobType)), BlobType, true},
{mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BlobKind])), PrimitiveTypeMap[BlobKind], true},
// string|bool|blob & number|blob|string -> true
{mustType(MakeUnionType(StringType, BoolType, BlobType)), mustType(MakeUnionType(FloaTType, BlobType, StringType)), true},
{mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BlobKind])), mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BlobKind], PrimitiveTypeMap[StringKind])), true},
// struct{foo:bool} & struct{foo:bool} -> true
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": BoolType})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": BoolType})), true},
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})), true},
// struct{foo:bool} & struct{foo:number} -> false
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": BoolType})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})), false},
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})), false},
// struct{foo:bool} & struct{foo:bool,bar:number} -> true
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": BoolType})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": BoolType, "bar": FloaTType})), true},
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BoolKind], "bar": PrimitiveTypeMap[FloatKind]})), true},
// struct{foo:ref<bool>} & struct{foo:ref<number>} -> false
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(BoolType))})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(FloaTType))})), false},
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(PrimitiveTypeMap[BoolKind]))})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(PrimitiveTypeMap[FloatKind]))})), false},
// struct{foo:ref<bool>} & struct{foo:ref<number|bool>} -> true
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(BoolType))})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(mustType(MakeUnionType(FloaTType, BoolType))))})), true},
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(PrimitiveTypeMap[BoolKind]))})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeRefType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind]))))})), true},
// struct A{foo:bool} & struct A{foo:bool, baz:string} -> true
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType, "baz": StringType})), true},
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind], "baz": PrimitiveTypeMap[StringKind]})), true},
// struct A{foo:bool, stuff:set<String|Blob>} & struct A{foo:bool, stuff:set<String>} -> true
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType, "stuff": mustType(MakeSetType(mustType(MakeUnionType(StringType, BlobType))))})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType, "stuff": mustType(MakeSetType(StringType))})), true},
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind], "stuff": mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BlobKind]))))})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind], "stuff": mustType(MakeSetType(PrimitiveTypeMap[StringKind]))})), true},
// struct A{stuff:set<String|Blob>} & struct A{foo:bool, stuff:set<Float>} -> false
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType, "stuff": mustType(MakeSetType(mustType(MakeUnionType(StringType, BlobType))))})),
mustType(MakeStructTypeFromFields("A", FieldMap{"stuff": mustType(MakeSetType(FloaTType))})), false},
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind], "stuff": mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BlobKind]))))})),
mustType(MakeStructTypeFromFields("A", FieldMap{"stuff": mustType(MakeSetType(PrimitiveTypeMap[FloatKind]))})), false},
// struct A{foo:bool} & struct {foo:bool} -> true
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": BoolType})), true},
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})), true},
// struct {foo:bool} & struct A{foo:bool} -> false
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": BoolType})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType})), true},
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})), true},
// struct A{foo:bool} & struct B{foo:bool} -> false
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": BoolType})),
mustType(MakeStructTypeFromFields("B", FieldMap{"foo": BoolType})), false},
{mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})),
mustType(MakeStructTypeFromFields("B", FieldMap{"foo": PrimitiveTypeMap[BoolKind]})), false},
// map<string, struct A{foo:string}> & map<string, struct A{foo:string, bar:bool}> -> true
{mustType(MakeMapType(StringType, mustType(MakeStructTypeFromFields("A", FieldMap{"foo": StringType})))),
mustType(MakeMapType(StringType, mustType(MakeStructTypeFromFields("A", FieldMap{"foo": StringType, "bar": BoolType})))), true},
{mustType(MakeMapType(PrimitiveTypeMap[StringKind], mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[StringKind]})))),
mustType(MakeMapType(PrimitiveTypeMap[StringKind], mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[StringKind], "bar": PrimitiveTypeMap[BoolKind]})))), true},
// struct{foo: string} & struct{foo: string|blob} -> true
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeUnionType(StringType, BlobType))})), true},
{mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BlobKind]))})), true},
// struct{foo: string}|struct{foo: blob} & struct{foo: string|blob} -> true
{mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": BlobType}))),
), mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeUnionType(StringType, BlobType))})), true},
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BlobKind]}))),
), mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BlobKind]))})), true},
// struct{foo: string}|struct{foo: blob} & struct{foo: number|bool} -> false
{mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": BlobType}))),
), mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeUnionType(FloaTType, BoolType))})), false},
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[BlobKind]}))),
), mustType(MakeStructTypeFromFields("", FieldMap{"foo": mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind]))})), false},
// map<struct{x:number, y:number}, struct A{foo:string}> & map<struct{x:number, y:number}, struct A{foo:string, bar:bool}> -> true
{
mustType(MakeMapType(
mustType(MakeStructTypeFromFields("", FieldMap{"x": FloaTType, "y": FloaTType})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": StringType})))),
mustType(MakeStructTypeFromFields("", FieldMap{"x": PrimitiveTypeMap[FloatKind], "y": PrimitiveTypeMap[FloatKind]})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[StringKind]})))),
mustType(MakeMapType(
mustType(MakeStructTypeFromFields("", FieldMap{"x": FloaTType, "y": FloaTType})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": StringType, "bar": BoolType})))),
mustType(MakeStructTypeFromFields("", FieldMap{"x": PrimitiveTypeMap[FloatKind], "y": PrimitiveTypeMap[FloatKind]})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[StringKind], "bar": PrimitiveTypeMap[BoolKind]})))),
true,
},
// map<struct{x:number, y:number}, struct A{foo:string}> & map<struct{x:number, y:number}, struct A{foo:string, bar:bool}> -> true
{
mustType(MakeMapType(
mustType(MakeStructTypeFromFields("", FieldMap{"x": FloaTType, "y": FloaTType})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": StringType})))),
mustType(MakeStructTypeFromFields("", FieldMap{"x": PrimitiveTypeMap[FloatKind], "y": PrimitiveTypeMap[FloatKind]})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[StringKind]})))),
mustType(MakeMapType(
mustType(MakeStructTypeFromFields("", FieldMap{"x": FloaTType, "y": FloaTType})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": StringType, "bar": BoolType})))),
mustType(MakeStructTypeFromFields("", FieldMap{"x": PrimitiveTypeMap[FloatKind], "y": PrimitiveTypeMap[FloatKind]})),
mustType(MakeStructTypeFromFields("A", FieldMap{"foo": PrimitiveTypeMap[StringKind], "bar": PrimitiveTypeMap[BoolKind]})))),
true,
},
// struct A{self:A} & struct A{self:A, foo:Float} -> true
{mustType(MakeStructTypeFromFields("A", FieldMap{"self": MakeCycleType("A")})),
mustType(MakeStructTypeFromFields("A", FieldMap{"self": MakeCycleType("A"), "foo": FloaTType})), true},
mustType(MakeStructTypeFromFields("A", FieldMap{"self": MakeCycleType("A"), "foo": PrimitiveTypeMap[FloatKind]})), true},
// struct{b:Bool} & struct{b?:Bool} -> true
{
mustType(MakeStructType("", StructField{"b", BoolType, false})),
mustType(MakeStructType("", StructField{"b", BoolType, true})),
mustType(MakeStructType("", StructField{"b", PrimitiveTypeMap[BoolKind], false})),
mustType(MakeStructType("", StructField{"b", PrimitiveTypeMap[BoolKind], true})),
true,
},
// struct{a?:Bool} & struct{b?:Bool} -> false
{
mustType(MakeStructType("", StructField{"a", BoolType, true})),
mustType(MakeStructType("", StructField{"b", BoolType, true})),
mustType(MakeStructType("", StructField{"a", PrimitiveTypeMap[BoolKind], true})),
mustType(MakeStructType("", StructField{"b", PrimitiveTypeMap[BoolKind], true})),
false,
},

View File

@@ -139,9 +139,9 @@ func compareEncodedNomsValues(a, b []byte) int {
return bytes.Compare(a, b)
case IntKind:
reader := binaryNomsReader{a[1:], 0}
aNum := reader.readInt()
aNum := Int(reader.readInt())
reader.buff, reader.offset = b[1:], 0
bNum := reader.readInt()
bNum := Int(reader.readInt())
if aNum == bNum {
return 0
}
@@ -151,9 +151,9 @@ func compareEncodedNomsValues(a, b []byte) int {
return 1
case UintKind:
reader := binaryNomsReader{a[1:], 0}
aNum := reader.readUint()
aNum := Uint(reader.readUint())
reader.buff, reader.offset = b[1:], 0
bNum := reader.readUint()
bNum := Uint(reader.readUint())
if aNum == bNum {
return 0
}
@@ -163,9 +163,9 @@ func compareEncodedNomsValues(a, b []byte) int {
return 1
case FloatKind:
reader := binaryNomsReader{a[1:], 0}
aNum := reader.readFloat(Format_7_18)
aNum := Float(reader.readFloat(Format_7_18))
reader.buff, reader.offset = b[1:], 0
bNum := reader.readFloat(Format_7_18)
bNum := Float(reader.readFloat(Format_7_18))
if aNum == bNum {
return 0
}
@@ -309,7 +309,7 @@ func TestCompareTotalOrdering(t *testing.T) {
// The order of these are done by the hash.
mustValue(NewSet(context.Background(), vrw, Float(0), Float(1), Float(2), Float(3))),
BoolType,
PrimitiveTypeMap[BoolKind],
// Value - values cannot be value
// Cycle - values cannot be cycle

View File

@@ -30,7 +30,6 @@ import (
"sync"
"github.com/dustin/go-humanize"
"github.com/google/uuid"
"github.com/liquidata-inc/dolt/go/store/d"
"github.com/liquidata-inc/dolt/go/store/util/writers"
@@ -196,14 +195,9 @@ func (w *hrsWriter) Write(ctx context.Context, v Value) error {
}
switch v.Kind() {
case BoolKind:
w.write(strconv.FormatBool(bool(v.(Bool))))
case FloatKind:
case FloatKind: // We're special-casing floats since it requires floatFormat & most other kinds don't need anything special
w.write(strconv.FormatFloat(float64(v.(Float)), w.floatFormat, -1, 64))
case StringKind:
w.write(strconv.Quote(string(v.(String))))
case BlobKind:
w.write("blob {")
blob := v.(Blob)
@@ -358,25 +352,11 @@ func (w *hrsWriter) Write(ctx context.Context, v Value) error {
return err
}
case UUIDKind:
id, _ := v.(UUID)
idStr := uuid.UUID(id).String()
w.write(idStr)
case IntKind:
w.write(strconv.FormatInt(int64(v.(Int)), 10))
case UintKind:
w.write(strconv.FormatUint(uint64(v.(Uint)), 10))
case NullKind:
w.write("null_value")
case InlineBlobKind:
uaStr := v.(InlineBlob).String()
w.write(uaStr)
default:
if IsPrimitiveKind(v.Kind()) {
w.write(v.HumanReadableString())
return nil
}
return ErrUnknownType
}
@@ -454,8 +434,6 @@ func (w *hrsWriter) writeSize(v Value) {
func (w *hrsWriter) writeType(t *Type, seenStructs map[*Type]struct{}) {
switch t.TargetKind() {
case BlobKind, BoolKind, FloatKind, StringKind, TypeKind, ValueKind, UUIDKind, IntKind, UintKind, InlineBlobKind, NullKind:
w.write(t.TargetKind().String())
case ListKind, RefKind, SetKind, MapKind, TupleKind:
w.write(t.TargetKind().String())
w.write("<")
@@ -496,7 +474,11 @@ func (w *hrsWriter) writeType(t *Type, seenStructs map[*Type]struct{}) {
return
}
default:
panic("unreachable")
if IsPrimitiveKind(t.TargetKind()) {
w.write(t.TargetKind().String())
} else {
panic("unreachable")
}
}
}

View File

@@ -242,26 +242,26 @@ func TestWriteHumanReadableListOfBlob(t *testing.T) {
}
func TestWriteHumanReadableType(t *testing.T) {
assertWriteHRSEqual(t, "Bool", BoolType)
assertWriteHRSEqual(t, "Blob", BlobType)
assertWriteHRSEqual(t, "String", StringType)
assertWriteHRSEqual(t, "Float", FloaTType)
assertWriteHRSEqual(t, "UUID", UUIDType)
assertWriteHRSEqual(t, "Int", IntType)
assertWriteHRSEqual(t, "Uint", UintType)
assertWriteHRSEqual(t, "InlineBlob", InlineBlobType)
assertWriteHRSEqual(t, "Null", NullType)
assertWriteHRSEqual(t, "Bool", PrimitiveTypeMap[BoolKind])
assertWriteHRSEqual(t, "Blob", PrimitiveTypeMap[BlobKind])
assertWriteHRSEqual(t, "String", PrimitiveTypeMap[StringKind])
assertWriteHRSEqual(t, "Float", PrimitiveTypeMap[FloatKind])
assertWriteHRSEqual(t, "UUID", PrimitiveTypeMap[UUIDKind])
assertWriteHRSEqual(t, "Int", PrimitiveTypeMap[IntKind])
assertWriteHRSEqual(t, "Uint", PrimitiveTypeMap[UintKind])
assertWriteHRSEqual(t, "InlineBlob", PrimitiveTypeMap[InlineBlobKind])
assertWriteHRSEqual(t, "Null", PrimitiveTypeMap[NullKind])
assertWriteHRSEqual(t, "List<Float>", mustType(MakeListType(FloaTType)))
assertWriteHRSEqual(t, "Set<Float>", mustType(MakeSetType(FloaTType)))
assertWriteHRSEqual(t, "Ref<Float>", mustType(MakeRefType(FloaTType)))
assertWriteHRSEqual(t, "Map<Float, String>", mustType(MakeMapType(FloaTType, StringType)))
assertWriteHRSEqual(t, "Float | String", mustType(MakeUnionType(FloaTType, StringType)))
assertWriteHRSEqual(t, "Bool", mustType(MakeUnionType(BoolType)))
assertWriteHRSEqual(t, "List<Float>", mustType(MakeListType(PrimitiveTypeMap[FloatKind])))
assertWriteHRSEqual(t, "Set<Float>", mustType(MakeSetType(PrimitiveTypeMap[FloatKind])))
assertWriteHRSEqual(t, "Ref<Float>", mustType(MakeRefType(PrimitiveTypeMap[FloatKind])))
assertWriteHRSEqual(t, "Map<Float, String>", mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])))
assertWriteHRSEqual(t, "Float | String", mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])))
assertWriteHRSEqual(t, "Bool", mustType(MakeUnionType(PrimitiveTypeMap[BoolKind])))
assertWriteHRSEqual(t, "", mustType(MakeUnionType()))
assertWriteHRSEqual(t, "List<Float | String>", mustType(MakeListType(mustType(MakeUnionType(FloaTType, StringType)))))
assertWriteHRSEqual(t, "List<Int | Uint>", mustType(MakeListType(mustType(MakeUnionType(IntType, UintType)))))
assertWriteHRSEqual(t, "List<Int | Null>", mustType(MakeListType(mustType(MakeUnionType(IntType, NullType)))))
assertWriteHRSEqual(t, "List<Float | String>", mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])))))
assertWriteHRSEqual(t, "List<Int | Uint>", mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[IntKind], PrimitiveTypeMap[UintKind])))))
assertWriteHRSEqual(t, "List<Int | Null>", mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[IntKind], PrimitiveTypeMap[NullKind])))))
assertWriteHRSEqual(t, "List<>", mustType(MakeListType(mustType(MakeUnionType()))))
}
@@ -345,13 +345,13 @@ func TestEmptyCollections(t *testing.T) {
b, err := NewStruct(Format_7_18, "Rien", StructData{})
assert.NoError(t, err)
assertWriteHRSEqual(t, "struct Rien {}", b)
c, err := MakeMapType(BlobType, FloaTType)
c, err := MakeMapType(PrimitiveTypeMap[BlobKind], PrimitiveTypeMap[FloatKind])
assert.NoError(t, err)
assertWriteHRSEqual(t, "Map<Blob, Float>", c)
d, err := NewMap(context.Background(), vrw)
assert.NoError(t, err)
assertWriteHRSEqual(t, "map {}", d)
e, err := MakeSetType(StringType)
e, err := MakeSetType(PrimitiveTypeMap[StringKind])
assert.NoError(t, err)
assertWriteHRSEqual(t, "Set<String>", e)
f, err := NewSet(context.Background(), vrw)
@@ -378,8 +378,8 @@ func TestEncodedValueMaxLines(t *testing.T) {
func TestWriteHumanReadableStructOptionalFields(t *testing.T) {
typ, err := MakeStructType("S1",
StructField{"a", BoolType, false},
StructField{"b", BoolType, true})
StructField{"a", PrimitiveTypeMap[BoolKind], false},
StructField{"b", PrimitiveTypeMap[BoolKind], true})
assert.NoError(t, err)
assertWriteHRSEqual(t, "Struct S1 {\n a: Bool,\n b?: Bool,\n}", typ)
}

View File

@@ -52,7 +52,7 @@ func toBinaryNomsReaderData(data []interface{}) []byte {
w.writeHash(v)
case []byte:
w.writeCount(uint64(len(v)))
w.writeBytes(v)
w.writeRaw(v)
case NomsKind:
w.writeUint8(uint8(v))
default:
@@ -341,9 +341,9 @@ func TestWriteCompoundBlob(t *testing.T) {
RefKind, r3, BlobKind, uint64(33), FloatKind, Float(60), uint64(60),
},
newBlob(mustSeq(newBlobMetaSequence(1, []metaTuple{
mustMetaTuple(newMetaTuple(mustRef(constructRef(Format_7_18, r1, BlobType, 11)), mustOrdKey(orderedKeyFromInt(20, Format_7_18)), 20)),
mustMetaTuple(newMetaTuple(mustRef(constructRef(Format_7_18, r2, BlobType, 22)), mustOrdKey(orderedKeyFromInt(40, Format_7_18)), 40)),
mustMetaTuple(newMetaTuple(mustRef(constructRef(Format_7_18, r3, BlobType, 33)), mustOrdKey(orderedKeyFromInt(60, Format_7_18)), 60)),
mustMetaTuple(newMetaTuple(mustRef(constructRef(Format_7_18, r1, PrimitiveTypeMap[BlobKind], 11)), mustOrdKey(orderedKeyFromInt(20, Format_7_18)), 20)),
mustMetaTuple(newMetaTuple(mustRef(constructRef(Format_7_18, r2, PrimitiveTypeMap[BlobKind], 22)), mustOrdKey(orderedKeyFromInt(40, Format_7_18)), 40)),
mustMetaTuple(newMetaTuple(mustRef(constructRef(Format_7_18, r3, PrimitiveTypeMap[BlobKind], 33)), mustOrdKey(orderedKeyFromInt(60, Format_7_18)), 60)),
}, newTestValueStore()))),
)
}
@@ -567,7 +567,7 @@ func TestWriteListOfStruct(t *testing.T) {
func TestWriteListOfUnionWithType(t *testing.T) {
vrw := newTestValueStore()
structType, err := MakeStructType("S", StructField{"x", FloaTType, false})
structType, err := MakeStructType("S", StructField{"x", PrimitiveTypeMap[FloatKind], false})
assert.NoError(t, err)
assertEncoding(t,
@@ -580,8 +580,8 @@ func TestWriteListOfUnionWithType(t *testing.T) {
},
mustList(NewList(context.Background(), vrw,
Bool(true),
FloaTType,
TypeType,
PrimitiveTypeMap[FloatKind],
PrimitiveTypeMap[TypeKind],
structType,
)),
)
@@ -594,7 +594,7 @@ func TestWriteRef(t *testing.T) {
[]interface{}{
RefKind, r, FloatKind, uint64(4),
},
mustValue(constructRef(Format_7_18, r, FloaTType, 4)),
mustValue(constructRef(Format_7_18, r, PrimitiveTypeMap[FloatKind], 4)),
)
}
@@ -606,7 +606,7 @@ func TestWriteListOfTypes(t *testing.T) {
ListKind, uint64(0), uint64(2), /* len */
TypeKind, BoolKind, TypeKind, StringKind,
},
mustValue(NewList(context.Background(), vrw, BoolType, StringType)),
mustValue(NewList(context.Background(), vrw, PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])),
)
}
@@ -632,28 +632,3 @@ func TestWriteEmptyUnionList(t *testing.T) {
mustValue(NewList(context.Background(), vrw)),
)
}
type bogusType int
func (bg bogusType) Value(ctx context.Context) (Value, error) { return bg, nil }
func (bg bogusType) Equals(other Value) bool { return false }
func (bg bogusType) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) { return false, nil }
func (bg bogusType) Hash(*NomsBinFormat) (hash.Hash, error) { return hash.Hash{}, nil }
func (bg bogusType) WalkValues(ctx context.Context, cb ValueCallback) error { return nil }
func (bg bogusType) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error { return nil }
func (bg bogusType) Kind() NomsKind {
return CycleKind
}
func (bg bogusType) typeOf() (*Type, error) {
return MakeCycleType("ABC"), nil
}
func (bg bogusType) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
panic("abc")
}
func TestBogusValueWithUnresolvedCycle(t *testing.T) {
g := bogusType(1)
_, err := EncodeValue(g, Format_7_18)
assert.Equal(t, err, ErrUnknownType)
}

View File

@@ -77,21 +77,21 @@ func TestValueEquals(t *testing.T) {
func() (Value, error) { return NewSet(context.Background(), vrw) },
func() (Value, error) { return NewSet(context.Background(), vrw, String("hi")) },
func() (Value, error) { return BoolType, nil },
func() (Value, error) { return StringType, nil },
func() (Value, error) { return PrimitiveTypeMap[BoolKind], nil },
func() (Value, error) { return PrimitiveTypeMap[StringKind], nil },
func() (Value, error) { return MakeStructType("a") },
func() (Value, error) { return MakeStructType("b") },
func() (Value, error) { return MakeListType(BoolType) },
func() (Value, error) { return MakeListType(FloaTType) },
func() (Value, error) { return MakeSetType(BoolType) },
func() (Value, error) { return MakeSetType(FloaTType) },
func() (Value, error) { return MakeRefType(BoolType) },
func() (Value, error) { return MakeRefType(FloaTType) },
func() (Value, error) { return MakeListType(PrimitiveTypeMap[BoolKind]) },
func() (Value, error) { return MakeListType(PrimitiveTypeMap[FloatKind]) },
func() (Value, error) { return MakeSetType(PrimitiveTypeMap[BoolKind]) },
func() (Value, error) { return MakeSetType(PrimitiveTypeMap[FloatKind]) },
func() (Value, error) { return MakeRefType(PrimitiveTypeMap[BoolKind]) },
func() (Value, error) { return MakeRefType(PrimitiveTypeMap[FloatKind]) },
func() (Value, error) {
return MakeMapType(BoolType, ValueType)
return MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[ValueKind])
},
func() (Value, error) {
return MakeMapType(FloaTType, ValueType)
return MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[ValueKind])
},
}

View File

@@ -23,7 +23,7 @@ package types
import (
"context"
"encoding/binary"
"strconv"
"github.com/liquidata-inc/dolt/go/store/hash"
)
@@ -51,6 +51,10 @@ func (v Float) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(v, nbf)
}
func (v Float) isPrimitive() bool {
return true
}
func (v Float) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -60,7 +64,7 @@ func (v Float) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (v Float) typeOf() (*Type, error) {
return FloaTType, nil
return PrimitiveTypeMap[FloatKind], nil
}
func (v Float) Kind() NomsKind {
@@ -82,16 +86,62 @@ func (v Float) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
return nil
}
func (v Float) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
// We know the size of the buffer here so allocate it once.
// FloatKind, int (Varint), exp (Varint)
buff := make([]byte, 1+2*binary.MaxVarintLen64)
w := binaryNomsWriter{buff, 0}
err := v.writeTo(&w, nbf)
func (v Float) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
return Float(b.readFloat(nbf)), nil
}
if err != nil {
return nil, err
func (v Float) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
b.skipFloat(nbf)
}
func (Float) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
switch targetKind {
case BoolKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(Float))
return Bool(fl != 0), nil
}, nil
case FloatKind:
return func(val Value) (Value, error) {
return val, nil
}, nil
case IntKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(Float))
return Int(int(fl)), nil
}, nil
case NullKind:
return func(Value) (Value, error) {
return NullValue, nil
}, nil
case StringKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(Float))
str := strconv.FormatFloat(fl, 'f', -1, 64)
return String(str), nil
}, nil
case UintKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
fl := float64(val.(Float))
return Uint(uint64(fl)), nil
}, nil
}
return buff[:w.offset], nil
return nil, CreateNoConversionError(FloatKind, targetKind)
}
func (v Float) HumanReadableString() string {
return strconv.FormatFloat(float64(v), 'g', -1, 64)
}

View File

@@ -17,9 +17,10 @@ package types
import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"math"
"strings"
"github.com/liquidata-inc/dolt/go/store/hash"
)
@@ -50,6 +51,10 @@ func (v InlineBlob) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(v, nbf)
}
func (v InlineBlob) isPrimitive() bool {
return true
}
func (v InlineBlob) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -59,7 +64,7 @@ func (v InlineBlob) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (v InlineBlob) typeOf() (*Type, error) {
return InlineBlobType, nil
return PrimitiveTypeMap[InlineBlobKind], nil
}
func (v InlineBlob) Kind() NomsKind {
@@ -82,23 +87,43 @@ func (v InlineBlob) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
}
w.writeUint16(uint16(byteLen))
w.writeBytes(v)
w.writeRaw(v)
return nil
}
func (v InlineBlob) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
// Length is uint8(InlineBlobKind) + uint16(length_prefix) + data
buff := make([]byte, 3+len(v))
w := binaryNomsWriter{buff, 0}
err := v.writeTo(&w, nbf)
func (v InlineBlob) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
size := uint32(b.readUint16())
ib := b.readBytes(size)
return InlineBlob(ib), nil
}
if err != nil {
return nil, err
func (v InlineBlob) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
size := uint32(b.readUint16())
b.skipBytes(size)
}
func (InlineBlob) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
switch targetKind {
case InlineBlobKind:
return func(val Value) (Value, error) {
return val, nil
}, nil
case NullKind:
return func(Value) (Value, error) {
return NullValue, nil
}, nil
case StringKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
return String(strings.ToUpper(hex.EncodeToString(val.(InlineBlob)))), nil
}, nil
}
return buff[:w.offset], err
return nil, CreateNoConversionError(InlineBlobKind, targetKind)
}
func (v InlineBlob) String() string {
return base64.RawURLEncoding.EncodeToString(v)
func (v InlineBlob) HumanReadableString() string {
return strings.ToUpper(hex.EncodeToString(v))
}

View File

@@ -23,7 +23,7 @@ package types
import (
"context"
"encoding/binary"
"strconv"
"github.com/liquidata-inc/dolt/go/store/hash"
)
@@ -51,6 +51,10 @@ func (v Int) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(v, nbf)
}
func (v Int) isPrimitive() bool {
return true
}
func (v Int) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -60,7 +64,7 @@ func (v Int) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (v Int) typeOf() (*Type, error) {
return IntType, nil
return PrimitiveTypeMap[IntKind], nil
}
func (v Int) Kind() NomsKind {
@@ -83,16 +87,62 @@ func (v Int) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
return nil
}
func (v Int) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
// We know the size of the buffer here so allocate it once.
// IntKind, int (Varint), exp (Varint)
buff := make([]byte, 1+2*binary.MaxVarintLen64)
w := binaryNomsWriter{buff, 0}
err := v.writeTo(&w, nbf)
func (v Int) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
return Int(b.readInt()), nil
}
if err != nil {
return nil, err
func (v Int) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
b.skipInt()
}
func (Int) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
switch targetKind {
case BoolKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(Int))
return Bool(n != 0), nil
}, nil
case FloatKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(Int))
return Float(float64(n)), nil
}, nil
case IntKind:
return func(val Value) (Value, error) {
return val, nil
}, nil
case NullKind:
return func(Value) (Value, error) {
return NullValue, nil
}, nil
case StringKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(Int))
str := strconv.FormatInt(n, 10)
return String(str), nil
}, nil
case UintKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := int64(val.(Int))
return Uint(uint64(n)), nil
}, nil
}
return buff[:w.offset], err
return nil, CreateNoConversionError(IntKind, targetKind)
}
func (v Int) HumanReadableString() string {
return strconv.FormatInt(int64(v), 10)
}

View File

@@ -173,6 +173,10 @@ func (l List) Concat(ctx context.Context, other List) (List, error) {
return newList(seq), nil
}
func (l List) isPrimitive() bool {
return false
}
// Iter iterates over the list and calls f for every element in the list. If f returns true then the
// iteration stops.
func (l List) Iter(ctx context.Context, f func(v Value, index uint64) (stop bool)) error {
@@ -329,13 +333,15 @@ func iterRange(ctx context.Context, col Collection, startIdx, endIdx uint64, cb
endIdx = endIdx - uint64(len(values))/valuesPerIdx - startIdx
startIdx = 0
bytes, err := seq.valueBytes(seq.format())
w := binaryNomsWriter{make([]byte, 4), 0}
err = seq.writeTo(&w, seq.format())
if err != nil {
return 0, err
}
numBytes += uint64(len(bytes)) // note: should really only include |values|
numBytes += uint64(w.offset) // note: should really only include |values|
}
return numBytes, nil
@@ -422,3 +428,23 @@ func makeListLeafChunkFn(vrw ValueReadWriter) makeChunkFn {
func newEmptyListSequenceChunker(ctx context.Context, vrw ValueReadWriter) (*sequenceChunker, error) {
return newEmptySequenceChunker(ctx, vrw, makeListLeafChunkFn(vrw), newIndexedMetaSequenceChunkFn(ListKind, vrw), hashValueBytes)
}
func (List) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(ListKind, targetKind)
}
func (l List) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (l List) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
panic("unreachable")
}
func (l List) String() string {
panic("unreachable")
}
func (l List) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -107,7 +107,7 @@ func newListTestSuite(size uint, expectChunkCount int, expectPrependChunkDiff in
length := 1 << size
elems := newTestList(length)
tr, err := MakeListType(FloaTType)
tr, err := MakeListType(PrimitiveTypeMap[FloatKind])
d.PanicIfError(err)
list, err := NewList(context.Background(), vrw, elems...)
d.PanicIfError(err)
@@ -1233,19 +1233,19 @@ func TestListTypeAfterMutations(t *testing.T) {
assert.NoError(err)
assert.Equal(l.Len(), uint64(n))
assert.IsType(c, l.asSequence())
assert.True(mustType(TypeOf(l)).Equals(mustType(MakeListType(FloaTType))))
assert.True(mustType(TypeOf(l)).Equals(mustType(MakeListType(PrimitiveTypeMap[FloatKind]))))
l, err = l.Edit().Append(String("a")).List(context.Background())
assert.NoError(err)
assert.Equal(l.Len(), uint64(n+1))
assert.IsType(c, l.asSequence())
assert.True(mustType(TypeOf(l)).Equals(mustType(MakeListType(mustType(MakeUnionType(FloaTType, StringType))))))
assert.True(mustType(TypeOf(l)).Equals(mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind]))))))
l, err = l.Edit().Splice(l.Len()-1, 1).List(context.Background())
assert.NoError(err)
assert.Equal(l.Len(), uint64(n))
assert.IsType(c, l.asSequence())
assert.True(mustType(TypeOf(l)).Equals(mustType(MakeListType(FloaTType))))
assert.True(mustType(TypeOf(l)).Equals(mustType(MakeListType(PrimitiveTypeMap[FloatKind]))))
}
test(15, listLeafSequence{})
@@ -1371,8 +1371,8 @@ func TestListWithStructShouldHaveOptionalFields(t *testing.T) {
assert.NoError(err)
assert.True(
mustType(MakeListType(mustType(MakeStructType("Foo",
StructField{"a", FloaTType, false},
StructField{"b", StringType, true},
StructField{"a", PrimitiveTypeMap[FloatKind], false},
StructField{"b", PrimitiveTypeMap[StringKind], true},
)),
)).Equals(mustType(TypeOf(list))))
}

View File

@@ -28,31 +28,9 @@ import (
)
func MakePrimitiveType(k NomsKind) (*Type, error) {
switch k {
case BoolKind:
return BoolType, nil
case FloatKind:
return FloaTType, nil
case UUIDKind:
return UUIDType, nil
case IntKind:
return IntType, nil
case UintKind:
return UintType, nil
case NullKind:
return NullType, nil
case StringKind:
return StringType, nil
case BlobKind:
return BlobType, nil
case ValueKind:
return ValueType, nil
case TypeKind:
return TypeType, nil
case InlineBlobKind:
return InlineBlobType, nil
if typ, ok := PrimitiveTypeMap[k]; ok {
return typ, nil
}
return nil, ErrUnknownType
}
@@ -128,17 +106,11 @@ func makePrimitiveType(k NomsKind) *Type {
return newType(PrimitiveDesc(k))
}
var BoolType = makePrimitiveType(BoolKind)
var FloaTType = makePrimitiveType(FloatKind)
var StringType = makePrimitiveType(StringKind)
var BlobType = makePrimitiveType(BlobKind)
var TypeType = makePrimitiveType(TypeKind)
var ValueType = makePrimitiveType(ValueKind)
var UUIDType = makePrimitiveType(UUIDKind)
var IntType = makePrimitiveType(IntKind)
var UintType = makePrimitiveType(UintKind)
var NullType = makePrimitiveType(NullKind)
var InlineBlobType = makePrimitiveType(InlineBlobKind)
// PrimitiveTypeMap auto populates with Value types that return true from isPrimitive().
// Only include a type here manually if it has no associated Value type.
var PrimitiveTypeMap = map[NomsKind]*Type{
ValueKind: makePrimitiveType(ValueKind),
}
func makeCompoundType(kind NomsKind, elemTypes ...*Type) (*Type, error) {
for _, el := range elemTypes {

View File

@@ -340,6 +340,10 @@ func (m Map) Any(ctx context.Context, cb func(k, v Value) bool) (yep bool, err e
return yep, err
}
func (m Map) isPrimitive() bool {
return false
}
func (m Map) Iterator(ctx context.Context) (MapIterator, error) {
return m.IteratorAt(ctx, 0)
}
@@ -504,3 +508,23 @@ func makeMapLeafChunkFn(vrw ValueReadWriter) makeChunkFn {
func newEmptyMapSequenceChunker(ctx context.Context, vrw ValueReadWriter) (*sequenceChunker, error) {
return newEmptySequenceChunker(ctx, vrw, makeMapLeafChunkFn(vrw), newOrderedMetaSequenceChunkFn(MapKind, vrw), mapHashValueBytes)
}
func (Map) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(MapKind, targetKind)
}
func (m Map) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (m Map) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
panic("unreachable")
}
func (m Map) String() string {
panic("unreachable")
}
func (m Map) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -132,13 +132,14 @@ func (ml mapLeafSequence) getItem(idx int) (sequenceItem, error) {
}
func (ml mapLeafSequence) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
bytes, err := ml.valueBytes(ml.format())
w := binaryNomsWriter{make([]byte, 4), 0}
err := ml.writeTo(&w, ml.format())
if err != nil {
return err
}
return walkRefs(bytes, ml.format(), cb)
return walkRefs(w.buff[:w.offset], ml.format(), cb)
}
func (ml mapLeafSequence) entries() (mapEntrySlice, error) {

View File

@@ -210,7 +210,7 @@ func newMapTestSuite(size uint, expectChunkCount int, expectPrependChunkDiff int
keyType, err := TypeOf(mustValue(gen(0)))
d.PanicIfError(err)
elems := newSortedTestMap(length, gen)
tr, err := MakeMapType(keyType, FloaTType)
tr, err := MakeMapType(keyType, PrimitiveTypeMap[FloatKind])
d.PanicIfError(err)
tmap, err := NewMap(context.Background(), vrw, elems.FlattenAll()...)
d.PanicIfError(err)
@@ -1421,7 +1421,7 @@ func TestMapOrdering(t *testing.T) {
vrw := newTestValueStore()
testMapOrder(assert, vrw,
StringType, StringType,
PrimitiveTypeMap[StringKind], PrimitiveTypeMap[StringKind],
[]Value{
String("a"), String("unused"),
String("z"), String("unused"),
@@ -1441,7 +1441,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
FloaTType, StringType,
PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind],
[]Value{
Float(0), String("unused"),
Float(1000), String("unused"),
@@ -1461,7 +1461,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
UintType, StringType,
PrimitiveTypeMap[UintKind], PrimitiveTypeMap[StringKind],
[]Value{
Uint(0), String("unused"),
Uint(1000), String("unused"),
@@ -1481,7 +1481,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
UintType, NullType,
PrimitiveTypeMap[UintKind], PrimitiveTypeMap[NullKind],
[]Value{
Uint(0), NullValue,
Uint(1000), NullValue,
@@ -1501,7 +1501,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
NullType, StringType,
PrimitiveTypeMap[NullKind], PrimitiveTypeMap[StringKind],
[]Value{
NullValue, String("val 1"),
NullValue, String("val 2"),
@@ -1513,7 +1513,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
IntType, StringType,
PrimitiveTypeMap[IntKind], PrimitiveTypeMap[StringKind],
[]Value{
Int(0), String("unused"),
Int(1000), String("unused"),
@@ -1533,7 +1533,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
FloaTType, StringType,
PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind],
[]Value{
Float(0), String("unused"),
Float(-30), String("unused"),
@@ -1559,7 +1559,7 @@ func TestMapOrdering(t *testing.T) {
UUID(uuid.Must(uuid.Parse("10000000-0000-0001-0000-000000000001"))),
UUID(uuid.Must(uuid.Parse("20000000-0000-0000-0000-000000000001"))),
}
testMapOrder(assert, vrw, UUIDType, StringType,
testMapOrder(assert, vrw, PrimitiveTypeMap[UUIDKind], PrimitiveTypeMap[StringKind],
[]Value{
uuids[4], String("unused"),
uuids[1], String("unused"),
@@ -1577,7 +1577,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
FloaTType, StringType,
PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind],
[]Value{
Float(0.0001), String("unused"),
Float(0.000001), String("unused"),
@@ -1597,7 +1597,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
ValueType, StringType,
PrimitiveTypeMap[ValueKind], PrimitiveTypeMap[StringKind],
[]Value{
String("a"), String("unused"),
String("z"), String("unused"),
@@ -1617,7 +1617,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
BoolType, StringType,
PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind],
[]Value{
Bool(true), String("unused"),
Bool(false), String("unused"),
@@ -1629,7 +1629,7 @@ func TestMapOrdering(t *testing.T) {
)
testMapOrder(assert, vrw,
InlineBlobType, StringType,
PrimitiveTypeMap[InlineBlobKind], PrimitiveTypeMap[StringKind],
[]Value{
InlineBlob([]byte{00, 01, 1}), String("unused"),
InlineBlob([]byte{00, 01, 9}), String("unused"),
@@ -1696,7 +1696,7 @@ func TestMapType(t *testing.T) {
assert.NoError(err)
assert.True(emptyMapType.Equals(mustType(TypeOf(m2))))
tr, err := MakeMapType(StringType, FloaTType)
tr, err := MakeMapType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[FloatKind])
assert.NoError(err)
m2, err = m.Edit().Set(String("A"), Float(1)).Map(context.Background())
assert.NoError(err)
@@ -1708,10 +1708,10 @@ func TestMapType(t *testing.T) {
m3, err := m2.Edit().Set(String("A"), Bool(true)).Map(context.Background())
assert.NoError(err)
assert.True(mustType(MakeMapType(StringType, mustType(MakeUnionType(BoolType, FloaTType)))).Equals(mustType(TypeOf(m3))), mustString(mustType(TypeOf(m3)).Describe(context.Background())))
assert.True(mustType(MakeMapType(PrimitiveTypeMap[StringKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))).Equals(mustType(TypeOf(m3))), mustString(mustType(TypeOf(m3)).Describe(context.Background())))
m4, err := m3.Edit().Set(Bool(true), Float(1)).Map(context.Background())
assert.NoError(err)
assert.True(mustType(MakeMapType(mustType(MakeUnionType(BoolType, StringType)), mustType(MakeUnionType(BoolType, FloaTType)))).Equals(mustType(TypeOf(m4))))
assert.True(mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])), mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))).Equals(mustType(TypeOf(m4))))
}
func TestMapChunks(t *testing.T) {
@@ -1829,19 +1829,19 @@ func TestMapTypeAfterMutations(t *testing.T) {
assert.NoError(err)
assert.Equal(m.Len(), uint64(n))
assert.IsType(c, m.asSequence())
assert.True(mustType(TypeOf(m)).Equals(mustType(MakeMapType(FloaTType, FloaTType))))
assert.True(mustType(TypeOf(m)).Equals(mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[FloatKind]))))
m, err = m.Edit().Set(String("a"), String("a")).Map(context.Background())
assert.NoError(err)
assert.Equal(m.Len(), uint64(n+1))
assert.IsType(c, m.asSequence())
assert.True(mustType(TypeOf(m)).Equals(mustType(MakeMapType(mustType(MakeUnionType(FloaTType, StringType)), mustType(MakeUnionType(FloaTType, StringType))))))
assert.True(mustType(TypeOf(m)).Equals(mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])), mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind]))))))
m, err = m.Edit().Remove(String("a")).Map(context.Background())
assert.NoError(err)
assert.Equal(m.Len(), uint64(n))
assert.IsType(c, m.asSequence())
assert.True(mustType(TypeOf(m)).Equals(mustType(MakeMapType(FloaTType, FloaTType))))
assert.True(mustType(TypeOf(m)).Equals(mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[FloatKind]))))
}
test(10, mapLeafSequence{})
@@ -2026,10 +2026,10 @@ func TestMapWithStructShouldHaveOptionalFields(t *testing.T) {
}))),
)
assert.True(
mustType(MakeMapType(StringType,
mustType(MakeMapType(PrimitiveTypeMap[StringKind],
mustType(MakeStructType("Foo",
StructField{"a", FloaTType, false},
StructField{"b", StringType, true},
StructField{"a", PrimitiveTypeMap[FloatKind], false},
StructField{"b", PrimitiveTypeMap[StringKind], true},
)),
)).Equals(mustType(TypeOf(list))))
@@ -2048,10 +2048,10 @@ func TestMapWithStructShouldHaveOptionalFields(t *testing.T) {
assert.True(
mustType(MakeMapType(
mustType(MakeStructType("Foo",
StructField{"a", FloaTType, false},
StructField{"b", StringType, true},
StructField{"a", PrimitiveTypeMap[FloatKind], false},
StructField{"b", PrimitiveTypeMap[StringKind], true},
)),
StringType,
PrimitiveTypeMap[StringKind],
)).Equals(mustType(TypeOf(list))))
}

View File

@@ -0,0 +1,50 @@
// Copyright 2019 Liquidata, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"errors"
"fmt"
)
type ConversionError struct {
fromKind NomsKind
toKind NomsKind
err error
}
// CreateConversionError creates a special kind of error to return during issues with marshalling values.
func CreateConversionError(sourceKind NomsKind, targetKind NomsKind, err error) ConversionError {
return ConversionError{
fromKind: sourceKind,
toKind: targetKind,
err: err,
}
}
// CreateNoConversionError creates a special kind of error to return when no marshal function is provided.
func CreateNoConversionError(sourceKind NomsKind, targetKind NomsKind) ConversionError {
return ConversionError{
fromKind: sourceKind,
toKind: targetKind,
err: errors.New("no marshalling function found"),
}
}
func (ce ConversionError) Error() string {
toKindStr := KindToString[ce.toKind]
fromKindStr := KindToString[ce.fromKind]
return fmt.Sprint("error converting", fromKindStr, "to", toKindStr, ": ", ce.err.Error())
}

View File

@@ -629,10 +629,6 @@ func (es emptySequence) Less(nbf *NomsBinFormat, other LesserValuable) (bool, er
panic("empty sequence")
}
func (es emptySequence) valueBytes(*NomsBinFormat) ([]byte, error) {
panic("empty sequence")
}
func (es emptySequence) valuesSlice(from, to uint64) ([]Value, error) {
panic("empty sequence")
}

View File

@@ -57,27 +57,26 @@ const (
UnknownKind NomsKind = 255
)
var SupportedKinds = map[NomsKind]struct{}{
BoolKind: {},
FloatKind: {},
StringKind: {},
BlobKind: {},
ValueKind: {},
ListKind: {},
MapKind: {},
RefKind: {},
SetKind: {},
StructKind: {},
CycleKind: {},
TypeKind: {},
UnionKind: {},
hashKind: {},
UUIDKind: {},
IntKind: {},
UintKind: {},
NullKind: {},
TupleKind: {},
InlineBlobKind: {},
var KindToType = map[NomsKind]Value{
BlobKind: Blob{},
BoolKind: Bool(false),
CycleKind: nil,
ListKind: List{},
MapKind: Map{},
FloatKind: Float(0),
RefKind: Ref{},
SetKind: Set{},
StructKind: Struct{},
StringKind: String(""),
TypeKind: &Type{},
UnionKind: nil,
ValueKind: nil,
UUIDKind: UUID{},
IntKind: Int(0),
UintKind: Uint(0),
NullKind: NullValue,
TupleKind: EmptyTuple(Format_7_18),
InlineBlobKind: InlineBlob{},
}
var KindToString = map[NomsKind]string{
@@ -110,12 +109,8 @@ func (k NomsKind) String() string {
// IsPrimitiveKind returns true if k represents a Noms primitive type, which excludes collections (List, Map, Set), Refs, Structs, Symbolic and Unresolved types.
func IsPrimitiveKind(k NomsKind) bool {
switch k {
case BoolKind, FloatKind, IntKind, UintKind, StringKind, BlobKind, UUIDKind, ValueKind, TypeKind, NullKind, InlineBlobKind:
return true
default:
return false
}
_, ok := PrimitiveTypeMap[k]
return ok
}
// isKindOrderedByValue determines if a value is ordered by its value instead of its hash.

View File

@@ -54,6 +54,10 @@ func (v Null) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(NullValue, nbf)
}
func (v Null) isPrimitive() bool {
return true
}
func (v Null) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -63,7 +67,7 @@ func (v Null) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (v Null) typeOf() (*Type, error) {
return NullType, nil
return PrimitiveTypeMap[NullKind], nil
}
func (v Null) Kind() NomsKind {
@@ -78,14 +82,18 @@ func (v Null) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
return NullKind.writeTo(w, nbf)
}
func (v Null) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
buff := make([]byte, 1)
w := binaryNomsWriter{buff, 0}
err := v.writeTo(&w, nbf)
if err != nil {
return nil, err
}
return buff, nil
func (v Null) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
return NullValue, nil
}
func (v Null) skip(nbf *NomsBinFormat, b *binaryNomsReader) {}
func (Null) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return func(Value) (Value, error) {
return NullValue, nil
}, nil
}
func (v Null) HumanReadableString() string {
return "null_value"
}

View File

@@ -87,15 +87,15 @@ func TestPathStructType(t *testing.T) {
assert := assert.New(t)
typ, err := MakeStructType("MyStruct",
StructField{Name: "foo", Type: StringType},
StructField{Name: "bar", Type: BoolType},
StructField{Name: "baz", Type: FloaTType},
StructField{Name: "foo", Type: PrimitiveTypeMap[StringKind]},
StructField{Name: "bar", Type: PrimitiveTypeMap[BoolKind]},
StructField{Name: "baz", Type: PrimitiveTypeMap[FloatKind]},
)
assert.NoError(err)
assertResolvesTo(assert, StringType, typ, `.foo`)
assertResolvesTo(assert, BoolType, typ, `.bar`)
assertResolvesTo(assert, FloaTType, typ, `.baz`)
assertResolvesTo(assert, PrimitiveTypeMap[StringKind], typ, `.foo`)
assertResolvesTo(assert, PrimitiveTypeMap[BoolKind], typ, `.bar`)
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], typ, `.baz`)
assertResolvesTo(assert, nil, typ, `.notHere`)
typ2, err := MakeStructType("",
@@ -104,9 +104,9 @@ func TestPathStructType(t *testing.T) {
assert.NoError(err)
assertResolvesTo(assert, typ, typ2, `.typ`)
assertResolvesTo(assert, StringType, typ2, `.typ.foo`)
assertResolvesTo(assert, BoolType, typ2, `.typ.bar`)
assertResolvesTo(assert, FloaTType, typ2, `.typ.baz`)
assertResolvesTo(assert, PrimitiveTypeMap[StringKind], typ2, `.typ.foo`)
assertResolvesTo(assert, PrimitiveTypeMap[BoolKind], typ2, `.typ.bar`)
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], typ2, `.typ.baz`)
assertResolvesTo(assert, nil, typ2, `.typ.notHere`)
assertResolvesTo(assert, nil, typ2, `.notHere.typ`)
}
@@ -154,7 +154,7 @@ func TestPathIndex(t *testing.T) {
func TestPathIndexType(t *testing.T) {
assert := assert.New(t)
st, err := MakeSetType(FloaTType)
st, err := MakeSetType(PrimitiveTypeMap[FloatKind])
assert.NoError(err)
lt, err := MakeListType(st)
assert.NoError(err)
@@ -163,16 +163,16 @@ func TestPathIndexType(t *testing.T) {
ut, err := MakeUnionType(lt, mt, st)
assert.NoError(err)
assertResolvesTo(assert, FloaTType, st, "[0]")
assertResolvesTo(assert, FloaTType, st, "[-1]")
assertResolvesTo(assert, FloaTType, st, "@at(0)")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], st, "[0]")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], st, "[-1]")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], st, "@at(0)")
assertResolvesTo(assert, nil, st, "[1]")
assertResolvesTo(assert, nil, st, "[-2]")
assertResolvesTo(assert, st, lt, "[0]")
assertResolvesTo(assert, st, lt, "[-1]")
assertResolvesTo(assert, FloaTType, lt, "[0][0]")
assertResolvesTo(assert, FloaTType, lt, "@at(0)@at(0)")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], lt, "[0][0]")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], lt, "@at(0)@at(0)")
assertResolvesTo(assert, nil, lt, "[1]")
assertResolvesTo(assert, nil, lt, "[-2]")
@@ -180,8 +180,8 @@ func TestPathIndexType(t *testing.T) {
assertResolvesTo(assert, st, mt, "[-2]")
assertResolvesTo(assert, lt, mt, "[1]")
assertResolvesTo(assert, lt, mt, "[-1]")
assertResolvesTo(assert, FloaTType, mt, "[1][0][0]")
assertResolvesTo(assert, FloaTType, mt, "@at(1)@at(0)@at(0)")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], mt, "[1][0][0]")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], mt, "@at(1)@at(0)@at(0)")
assertResolvesTo(assert, nil, mt, "[2]")
assertResolvesTo(assert, nil, mt, "[-3]")
@@ -191,8 +191,8 @@ func TestPathIndexType(t *testing.T) {
assertResolvesTo(assert, mt, ut, "[-2]")
assertResolvesTo(assert, st, ut, "[2]")
assertResolvesTo(assert, st, ut, "[-1]")
assertResolvesTo(assert, FloaTType, ut, "[1][1][0][0]")
assertResolvesTo(assert, FloaTType, ut, "@at(1)@at(1)@at(0)@at(0)")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], ut, "[1][1][0][0]")
assertResolvesTo(assert, PrimitiveTypeMap[FloatKind], ut, "@at(1)@at(1)@at(0)@at(0)")
assertResolvesTo(assert, nil, ut, "[3]")
assertResolvesTo(assert, nil, ut, "[-4]")
}
@@ -545,7 +545,7 @@ func TestPathType(t *testing.T) {
})
assert.NoError(err)
assertResolvesTo(assert, StringType, m, `["string"]@key@type`)
assertResolvesTo(assert, PrimitiveTypeMap[StringKind], m, `["string"]@key@type`)
assertResolvesTo(assert, mustType(TypeOf(m)), m, `@type`)
s, err := NewStruct(Format_7_18, "", StructData{
"str": String("foo"),

View File

@@ -67,7 +67,7 @@ func NewRef(v Value, nbf *NomsBinFormat) (Ref, error) {
// ToRefOfValue returns a new Ref that points to the same target as |r|, but
// with the type 'Ref<Value>'.
func ToRefOfValue(r Ref, nbf *NomsBinFormat) (Ref, error) {
return constructRef(nbf, r.TargetHash(), ValueType, r.Height())
return constructRef(nbf, r.TargetHash(), PrimitiveTypeMap[ValueKind], r.Height())
}
func constructRef(nbf *NomsBinFormat, targetHash hash.Hash, targetType *Type, height uint64) (Ref, error) {
@@ -177,6 +177,10 @@ func (r Ref) TargetType() (*Type, error) {
}
// Value interface
func (r Ref) isPrimitive() bool {
return false
}
func (r Ref) Value(ctx context.Context) (Value, error) {
return r, nil
}
@@ -200,3 +204,23 @@ func (r Ref) isSameTargetType(other Ref) bool {
otherTargetTypeBytes := other.buff[other.offsetAtPart(refPartTargetType):other.offsetAtPart(refPartHeight)]
return bytes.Equal(targetTypeBytes, otherTargetTypeBytes)
}
func (Ref) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(RefKind, targetKind)
}
func (r Ref) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (r Ref) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
panic("unreachable")
}
func (r Ref) String() string {
panic("unreachable")
}
func (r Ref) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -39,7 +39,7 @@ func TestRefByHeight(t *testing.T) {
return Ref{}, err
}
return constructRef(Format_7_18, h, FloaTType, height)
return constructRef(Format_7_18, h, PrimitiveTypeMap[FloatKind], height)
}
assert := assert.New(t)
@@ -120,7 +120,7 @@ func TestPopRefsOfHeight(t *testing.T) {
for i, n := range []int{6, 3, 6, 6, 2} {
hsh, err := Float(i).Hash(Format_7_18)
assert.NoError(t, err)
r, err := constructRef(Format_7_18, hsh, FloaTType, uint64(n))
r, err := constructRef(Format_7_18, hsh, PrimitiveTypeMap[FloatKind], uint64(n))
assert.NoError(t, err)
h.PushBack(r)
}

View File

@@ -51,7 +51,6 @@ type sequence interface {
seqLen() int
treeLevel() uint64
typeOf() (*Type, error)
valueBytes(*NomsBinFormat) ([]byte, error)
valueReadWriter() ValueReadWriter
valuesSlice(from, to uint64) ([]Value, error)
WalkRefs(nbf *NomsBinFormat, cb RefCallback) error

View File

@@ -95,10 +95,6 @@ func (ts testSequence) Equals(other Value) bool {
panic("not reached")
}
func (ts testSequence) valueBytes(*NomsBinFormat) ([]byte, error) {
panic("not reached")
}
func (ts testSequence) valuesSlice(from, to uint64) ([]Value, error) {
panic("not reached")
}

View File

@@ -228,6 +228,10 @@ func (s Set) Has(ctx context.Context, v Value) (bool, error) {
type setIterCallback func(v Value) (bool, error)
func (s Set) isPrimitive() bool {
return false
}
func (s Set) Iter(ctx context.Context, cb setIterCallback) error {
cur, err := newCursorAt(ctx, s.orderedSequence, emptyKey, false, false)
@@ -349,3 +353,23 @@ func makeSetLeafChunkFn(vrw ValueReadWriter) makeChunkFn {
func newEmptySetSequenceChunker(ctx context.Context, vrw ValueReadWriter) (*sequenceChunker, error) {
return newEmptySequenceChunker(ctx, vrw, makeSetLeafChunkFn(vrw), newOrderedMetaSequenceChunkFn(SetKind, vrw), hashValueBytes)
}
func (Set) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(SetKind, targetKind)
}
func (s Set) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (s Set) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
panic("unreachable")
}
func (s Set) String() string {
panic("unreachable")
}
func (s Set) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -340,11 +340,11 @@ func TestNewSet(t *testing.T) {
s, err = NewSet(context.Background(), vs, Float(0))
assert.NoError(err)
assert.True(mustType(MakeSetType(FloaTType)).Equals(mustType(TypeOf(s))))
assert.True(mustType(MakeSetType(PrimitiveTypeMap[FloatKind])).Equals(mustType(TypeOf(s))))
s, err = NewSet(context.Background(), vs)
assert.NoError(err)
assert.IsType(mustType(MakeSetType(FloaTType)), mustType(TypeOf(s)))
assert.IsType(mustType(MakeSetType(PrimitiveTypeMap[FloatKind])), mustType(TypeOf(s)))
se, err := s.Edit().Remove(Float(1))
assert.NoError(err)
@@ -905,7 +905,7 @@ func TestSetOrdering(t *testing.T) {
vs := newTestValueStore()
testSetOrder(assert, vs,
StringType,
PrimitiveTypeMap[StringKind],
[]Value{
String("a"),
String("z"),
@@ -925,7 +925,7 @@ func TestSetOrdering(t *testing.T) {
)
testSetOrder(assert, vs,
FloaTType,
PrimitiveTypeMap[FloatKind],
[]Value{
Float(0),
Float(1000),
@@ -945,7 +945,7 @@ func TestSetOrdering(t *testing.T) {
)
testSetOrder(assert, vs,
FloaTType,
PrimitiveTypeMap[FloatKind],
[]Value{
Float(0),
Float(-30),
@@ -965,7 +965,7 @@ func TestSetOrdering(t *testing.T) {
)
testSetOrder(assert, vs,
FloaTType,
PrimitiveTypeMap[FloatKind],
[]Value{
Float(0.0001),
Float(0.000001),
@@ -985,7 +985,7 @@ func TestSetOrdering(t *testing.T) {
)
testSetOrder(assert, vs,
ValueType,
PrimitiveTypeMap[ValueKind],
[]Value{
String("a"),
String("z"),
@@ -1006,7 +1006,7 @@ func TestSetOrdering(t *testing.T) {
)
testSetOrder(assert, vs,
BoolType,
PrimitiveTypeMap[BoolKind],
[]Value{
Bool(true),
Bool(false),
@@ -1030,13 +1030,13 @@ func TestSetType(t *testing.T) {
s, err = NewSet(context.Background(), vs, Float(0))
assert.NoError(err)
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(FloaTType))))
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(PrimitiveTypeMap[FloatKind]))))
se2, err := s.Edit().Remove(Float(1))
assert.NoError(err)
s2, err := se2.Set(context.Background())
assert.NoError(err)
assert.True(mustType(TypeOf(s2)).Equals(mustType(MakeSetType(FloaTType))))
assert.True(mustType(TypeOf(s2)).Equals(mustType(MakeSetType(PrimitiveTypeMap[FloatKind]))))
se2, err = s.Edit().Insert(Float(0), Float(1))
assert.NoError(err)
@@ -1048,12 +1048,12 @@ func TestSetType(t *testing.T) {
assert.NoError(err)
s3, err := se3.Set(context.Background())
assert.NoError(err)
assert.True(mustType(TypeOf(s3)).Equals(mustType(MakeSetType(mustType(MakeUnionType(BoolType, FloaTType))))))
assert.True(mustType(TypeOf(s3)).Equals(mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind]))))))
se4, err := s.Edit().Insert(Float(3), Bool(true))
assert.NoError(err)
s4, err := se4.Set(context.Background())
assert.NoError(err)
assert.True(mustType(TypeOf(s4)).Equals(mustType(MakeSetType(mustType(MakeUnionType(BoolType, FloaTType))))))
assert.True(mustType(TypeOf(s4)).Equals(mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind]))))))
}
func TestSetChunks(t *testing.T) {
@@ -1170,14 +1170,14 @@ func TestSetTypeAfterMutations(t *testing.T) {
assert.NoError(err)
assert.Equal(s.Len(), uint64(n))
assert.IsType(c, s.asSequence())
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(FloaTType))))
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(PrimitiveTypeMap[FloatKind]))))
se, err := s.Edit().Insert(String("a"))
assert.NoError(err)
s, err = se.Set(context.Background())
assert.Equal(s.Len(), uint64(n+1))
assert.IsType(c, s.asSequence())
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(mustType(MakeUnionType(FloaTType, StringType))))))
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind]))))))
se, err = s.Edit().Remove(String("a"))
assert.NoError(err)
@@ -1185,7 +1185,7 @@ func TestSetTypeAfterMutations(t *testing.T) {
assert.NoError(err)
assert.Equal(s.Len(), uint64(n))
assert.IsType(c, s.asSequence())
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(FloaTType))))
assert.True(mustType(TypeOf(s)).Equals(mustType(MakeSetType(PrimitiveTypeMap[FloatKind]))))
}
test(10, setLeafSequence{})
@@ -1316,8 +1316,8 @@ func TestSetWithStructShouldHaveOptionalFields(t *testing.T) {
assert.NoError(err)
assert.True(
mustType(MakeSetType(mustType(MakeStructType("Foo",
StructField{"a", FloaTType, false},
StructField{"b", StringType, true},
StructField{"a", PrimitiveTypeMap[FloatKind], false},
StructField{"b", PrimitiveTypeMap[StringKind], true},
),
))).Equals(mustType(TypeOf(list))))
}

View File

@@ -137,9 +137,12 @@ func cloneTypeTreeAndReplaceNamedStructs(t *Type, namedStructs map[string]struct
var rec func(t *Type) *Type
rec = func(t *Type) *Type {
kind := t.TargetKind()
switch kind {
case BoolKind, FloatKind, StringKind, BlobKind, ValueKind, TypeKind, UUIDKind, IntKind, UintKind, InlineBlobKind, NullKind:
if IsPrimitiveKind(kind) {
return t
}
switch kind {
case ListKind, MapKind, RefKind, SetKind, UnionKind, TupleKind:
elemTypes := make(typeSlice, len(t.Desc.(CompoundDesc).ElemTypes))
for i, et := range t.Desc.(CompoundDesc).ElemTypes {
@@ -191,51 +194,53 @@ func foldUnions(t *Type, seenStructs typeset, intersectStructs bool) (*Type, err
var err error
kind := t.TargetKind()
switch kind {
case BoolKind, FloatKind, StringKind, BlobKind, ValueKind, TypeKind, CycleKind, UUIDKind, IntKind, UintKind, InlineBlobKind, NullKind:
break
case ListKind, MapKind, RefKind, SetKind, TupleKind:
elemTypes := t.Desc.(CompoundDesc).ElemTypes
for i, et := range elemTypes {
elemTypes[i], err = foldUnions(et, seenStructs, intersectStructs)
if err != nil {
return nil, err
}
}
case StructKind:
if seenStructs.has(t) {
return t, nil
}
seenStructs.add(t)
fields := t.Desc.(StructDesc).fields
for i, f := range fields {
fields[i].Type, err = foldUnions(f.Type, seenStructs, intersectStructs)
if err != nil {
return nil, err
}
}
case UnionKind:
elemTypes := t.Desc.(CompoundDesc).ElemTypes
if len(elemTypes) == 0 {
if !IsPrimitiveKind(kind) {
switch kind {
case CycleKind:
break
}
ts := make(typeset, len(elemTypes))
for _, t := range elemTypes {
ts.add(t)
}
if len(ts) == 0 {
t.Desc = CompoundDesc{UnionKind, nil}
return t, nil
}
return foldUnionImpl(ts, seenStructs, intersectStructs)
default:
panic("Unknown noms kind")
case ListKind, MapKind, RefKind, SetKind, TupleKind:
elemTypes := t.Desc.(CompoundDesc).ElemTypes
for i, et := range elemTypes {
elemTypes[i], err = foldUnions(et, seenStructs, intersectStructs)
if err != nil {
return nil, err
}
}
case StructKind:
if seenStructs.has(t) {
return t, nil
}
seenStructs.add(t)
fields := t.Desc.(StructDesc).fields
for i, f := range fields {
fields[i].Type, err = foldUnions(f.Type, seenStructs, intersectStructs)
if err != nil {
return nil, err
}
}
case UnionKind:
elemTypes := t.Desc.(CompoundDesc).ElemTypes
if len(elemTypes) == 0 {
break
}
ts := make(typeset, len(elemTypes))
for _, t := range elemTypes {
ts.add(t)
}
if len(ts) == 0 {
t.Desc = CompoundDesc{UnionKind, nil}
return t, nil
}
return foldUnionImpl(ts, seenStructs, intersectStructs)
default:
panic("Unknown noms kind")
}
}
return t, nil
}

View File

@@ -33,41 +33,41 @@ func TestSimplifyStructFields(t *testing.T) {
test([]structTypeFields{
{
StructField{"a", BoolType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
},
{
StructField{"a", BoolType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
},
},
structTypeFields{
StructField{"a", BoolType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
},
)
test([]structTypeFields{
{
StructField{"a", BoolType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
},
{
StructField{"b", BoolType, false},
StructField{"b", PrimitiveTypeMap[BoolKind], false},
},
},
structTypeFields{
StructField{"a", BoolType, true},
StructField{"b", BoolType, true},
StructField{"a", PrimitiveTypeMap[BoolKind], true},
StructField{"b", PrimitiveTypeMap[BoolKind], true},
},
)
test([]structTypeFields{
{
StructField{"a", BoolType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
},
{
StructField{"a", BoolType, true},
StructField{"a", PrimitiveTypeMap[BoolKind], true},
},
},
structTypeFields{
StructField{"a", BoolType, true},
StructField{"a", PrimitiveTypeMap[BoolKind], true},
},
)
}
@@ -85,16 +85,16 @@ func TestSimplifyType(t *testing.T) {
test(t, t)
}
testSame(BlobType)
testSame(BoolType)
testSame(FloaTType)
testSame(StringType)
testSame(TypeType)
testSame(ValueType)
testSame(mustType(makeCompoundType(ListKind, BoolType)))
testSame(mustType(makeCompoundType(SetKind, BoolType)))
testSame(mustType(makeCompoundType(RefKind, BoolType)))
testSame(mustType(makeCompoundType(MapKind, BoolType, FloaTType)))
testSame(PrimitiveTypeMap[BlobKind])
testSame(PrimitiveTypeMap[BoolKind])
testSame(PrimitiveTypeMap[FloatKind])
testSame(PrimitiveTypeMap[StringKind])
testSame(PrimitiveTypeMap[TypeKind])
testSame(PrimitiveTypeMap[ValueKind])
testSame(mustType(makeCompoundType(ListKind, PrimitiveTypeMap[BoolKind])))
testSame(mustType(makeCompoundType(SetKind, PrimitiveTypeMap[BoolKind])))
testSame(mustType(makeCompoundType(RefKind, PrimitiveTypeMap[BoolKind])))
testSame(mustType(makeCompoundType(MapKind, PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))
{
// Cannot do equals on cycle types
@@ -103,39 +103,39 @@ func TestSimplifyType(t *testing.T) {
assert.Equal(in, act)
}
test(mustType(makeUnionType(BoolType)), BoolType)
test(mustType(makeUnionType(BoolType, BoolType)), BoolType)
testSame(mustType(makeUnionType(BoolType, FloaTType)))
test(mustType(makeUnionType(FloaTType, BoolType)), mustType(makeUnionType(BoolType, FloaTType)))
test(mustType(makeUnionType(FloaTType, BoolType)), mustType(makeUnionType(BoolType, FloaTType)))
test(mustType(makeUnionType(PrimitiveTypeMap[BoolKind])), PrimitiveTypeMap[BoolKind])
test(mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])), PrimitiveTypeMap[BoolKind])
testSame(mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))
test(mustType(makeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])), mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))
test(mustType(makeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])), mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))
testSame(mustType(makeCompoundType(ListKind, mustType(makeUnionType(BoolType, FloaTType)))))
test(mustType(makeCompoundType(ListKind, mustType(makeUnionType(BoolType)))), mustType(makeCompoundType(ListKind, BoolType)))
test(mustType(makeCompoundType(ListKind, mustType(makeUnionType(BoolType, BoolType)))), mustType(makeCompoundType(ListKind, BoolType)))
testSame(mustType(makeCompoundType(ListKind, mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))))
test(mustType(makeCompoundType(ListKind, mustType(makeUnionType(PrimitiveTypeMap[BoolKind])))), mustType(makeCompoundType(ListKind, PrimitiveTypeMap[BoolKind])))
test(mustType(makeCompoundType(ListKind, mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])))), mustType(makeCompoundType(ListKind, PrimitiveTypeMap[BoolKind])))
testSame(mustType(makeStructType("", nil)))
testSame(mustType(makeStructType("", structTypeFields{})))
testSame(mustType(makeStructType("", structTypeFields{
StructField{"b", BoolType, false},
StructField{"s", StringType, !intersectStructs},
StructField{"b", PrimitiveTypeMap[BoolKind], false},
StructField{"s", PrimitiveTypeMap[StringKind], !intersectStructs},
})))
test(
mustType(makeStructType("", structTypeFields{
StructField{"a", BoolType, false},
StructField{"b", mustType(makeUnionType(FloaTType, FloaTType)), false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
StructField{"b", mustType(makeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[FloatKind])), false},
})),
mustType(makeStructType("", structTypeFields{
StructField{"a", BoolType, false},
StructField{"b", FloaTType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
StructField{"b", PrimitiveTypeMap[FloatKind], false},
})),
)
// non named structs do not create cycles.
testSame(mustType(makeStructType("", structTypeFields{
StructField{"b", BoolType, false},
StructField{"b", PrimitiveTypeMap[BoolKind], false},
StructField{
"s",
mustType(makeStructType("", structTypeFields{
StructField{"c", StringType, false},
StructField{"c", PrimitiveTypeMap[StringKind], false},
})),
!intersectStructs,
},
@@ -146,15 +146,15 @@ func TestSimplifyType(t *testing.T) {
mustType(makeCompoundType(
UnionKind,
mustType(makeStructType("", structTypeFields{
StructField{"a", BoolType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
})),
mustType(makeStructType("", structTypeFields{
StructField{"b", BoolType, false},
StructField{"b", PrimitiveTypeMap[BoolKind], false},
})),
)),
mustType(makeStructType("", structTypeFields{
StructField{"a", BoolType, !intersectStructs},
StructField{"b", BoolType, !intersectStructs},
StructField{"a", PrimitiveTypeMap[BoolKind], !intersectStructs},
StructField{"b", PrimitiveTypeMap[BoolKind], !intersectStructs},
})),
)
@@ -163,11 +163,11 @@ func TestSimplifyType(t *testing.T) {
test(
mustType(makeCompoundType(
UnionKind,
mustType(makeCompoundType(k, FloaTType)),
mustType(makeCompoundType(k, BoolType)),
mustType(makeCompoundType(k, PrimitiveTypeMap[FloatKind])),
mustType(makeCompoundType(k, PrimitiveTypeMap[BoolKind])),
)),
mustType(makeCompoundType(k,
mustType(makeUnionType(BoolType, FloaTType)),
mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])),
)),
)
}
@@ -176,12 +176,12 @@ func TestSimplifyType(t *testing.T) {
test(
mustType(makeCompoundType(
UnionKind,
mustType(makeCompoundType(MapKind, FloaTType, FloaTType)),
mustType(makeCompoundType(MapKind, BoolType, FloaTType)),
mustType(makeCompoundType(MapKind, PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[FloatKind])),
mustType(makeCompoundType(MapKind, PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])),
)),
mustType(makeCompoundType(MapKind,
mustType(makeUnionType(BoolType, FloaTType)),
FloaTType,
mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])),
PrimitiveTypeMap[FloatKind],
)),
)
@@ -189,19 +189,19 @@ func TestSimplifyType(t *testing.T) {
test(
mustType(makeCompoundType(
UnionKind,
mustType(makeCompoundType(MapKind, FloaTType, FloaTType)),
mustType(makeCompoundType(MapKind, FloaTType, BoolType)),
mustType(makeCompoundType(MapKind, PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[FloatKind])),
mustType(makeCompoundType(MapKind, PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])),
)),
mustType(makeCompoundType(MapKind,
FloaTType,
mustType(makeUnionType(BoolType, FloaTType)),
PrimitiveTypeMap[FloatKind],
mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])),
)),
)
// union flattening
test(
mustType(makeUnionType(FloaTType, mustType(makeUnionType(FloaTType, BoolType)))),
mustType(makeUnionType(BoolType, FloaTType)),
mustType(makeUnionType(PrimitiveTypeMap[FloatKind], mustType(makeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))),
mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])),
)
{
@@ -225,14 +225,14 @@ func TestSimplifyType(t *testing.T) {
testSame(mustType(makeStructType("A", nil)))
testSame(mustType(makeStructType("A", structTypeFields{})))
testSame(mustType(makeStructType("A", structTypeFields{
StructField{"a", BoolType, !intersectStructs},
StructField{"a", PrimitiveTypeMap[BoolKind], !intersectStructs},
})))
test(
mustType(makeStructType("A", structTypeFields{
StructField{"a", mustType(makeUnionType(BoolType, BoolType, FloaTType)), false},
StructField{"a", mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), false},
})),
mustType(makeStructType("A", structTypeFields{
StructField{"a", mustType(makeUnionType(BoolType, FloaTType)), false},
StructField{"a", mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), false},
})),
)
@@ -241,7 +241,7 @@ func TestSimplifyType(t *testing.T) {
StructField{
"a",
mustType(makeStructType("B", structTypeFields{
StructField{"b", BoolType, !intersectStructs},
StructField{"b", PrimitiveTypeMap[BoolKind], !intersectStructs},
})),
false,
},
@@ -253,7 +253,7 @@ func TestSimplifyType(t *testing.T) {
exp := mustType(makeStructType("A", structTypeFields{
StructField{
"a",
BoolType, // placeholder
PrimitiveTypeMap[BoolKind], // placeholder
!intersectStructs,
},
}))
@@ -284,18 +284,18 @@ func TestSimplifyType(t *testing.T) {
{
a := mustType(makeStructType("S", structTypeFields{
StructField{"a", BoolType, !intersectStructs},
StructField{"b", mustType(makeUnionType(BoolType, StringType)), false},
StructField{"a", PrimitiveTypeMap[BoolKind], !intersectStructs},
StructField{"b", mustType(makeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])), false},
}))
exp := mustType(makeCompoundType(MapKind, a, a))
test(
mustType(makeCompoundType(MapKind,
mustType(makeStructType("S", structTypeFields{
StructField{"a", BoolType, false},
StructField{"b", StringType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
StructField{"b", PrimitiveTypeMap[StringKind], false},
})),
mustType(makeStructType("S", structTypeFields{
StructField{"b", BoolType, false},
StructField{"b", PrimitiveTypeMap[BoolKind], false},
})),
)),
exp,
@@ -306,11 +306,11 @@ func TestSimplifyType(t *testing.T) {
testSame(
mustType(makeCompoundType(MapKind,
mustType(makeStructType("", structTypeFields{
StructField{"a", BoolType, false},
StructField{"b", StringType, false},
StructField{"a", PrimitiveTypeMap[BoolKind], false},
StructField{"b", PrimitiveTypeMap[StringKind], false},
})),
mustType(makeStructType("", structTypeFields{
StructField{"b", BoolType, false},
StructField{"b", PrimitiveTypeMap[BoolKind], false},
})),
)),
)
@@ -320,12 +320,12 @@ func TestSimplifyType(t *testing.T) {
a := mustType(makeStructType("A", structTypeFields{
StructField{
"a",
BoolType, // placeholder
PrimitiveTypeMap[BoolKind], // placeholder
!intersectStructs,
},
}))
a.Desc.(StructDesc).fields[0].Type = a
exp := mustType(makeUnionType(FloaTType, a, TypeType))
exp := mustType(makeUnionType(PrimitiveTypeMap[FloatKind], a, PrimitiveTypeMap[TypeKind]))
test(
mustType(makeCompoundType(UnionKind,
mustType(makeStructType("A", structTypeFields{
@@ -335,8 +335,8 @@ func TestSimplifyType(t *testing.T) {
false,
},
})),
FloaTType,
TypeType,
PrimitiveTypeMap[FloatKind],
PrimitiveTypeMap[TypeKind],
)),
exp,
)
@@ -346,20 +346,20 @@ func TestSimplifyType(t *testing.T) {
mustType(makeCompoundType(RefKind,
mustType(makeCompoundType(UnionKind,
mustType(makeCompoundType(ListKind,
BoolType,
PrimitiveTypeMap[BoolKind],
)),
mustType(makeCompoundType(SetKind,
mustType(makeUnionType(StringType, FloaTType)),
mustType(makeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[FloatKind])),
)),
)),
)),
mustType(makeCompoundType(RefKind,
mustType(makeCompoundType(UnionKind,
mustType(makeCompoundType(ListKind,
BoolType,
PrimitiveTypeMap[BoolKind],
)),
mustType(makeCompoundType(SetKind,
mustType(makeUnionType(FloaTType, StringType)),
mustType(makeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])),
)),
)),
)),

View File

@@ -23,7 +23,14 @@ package types
import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
"strconv"
"strings"
"github.com/google/uuid"
"github.com/liquidata-inc/dolt/go/store/hash"
)
@@ -51,6 +58,10 @@ func (s String) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(s, nbf)
}
func (s String) isPrimitive() bool {
return true
}
func (s String) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -60,7 +71,7 @@ func (s String) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (s String) typeOf() (*Type, error) {
return StringType, nil
return PrimitiveTypeMap[StringKind], nil
}
func (s String) Kind() NomsKind {
@@ -83,16 +94,148 @@ func (s String) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
return nil
}
func (s String) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
// We know the size of the buffer here so allocate it once.
// StringKind, Length (UVarint), UTF-8 encoded string
buff := make([]byte, 1+binary.MaxVarintLen64+len(s))
w := binaryNomsWriter{buff, 0}
err := s.writeTo(&w, nbf)
func (s String) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
return String(b.readString()), nil
}
if err != nil {
return nil, err
func (s String) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
b.skipString()
}
func parseNumber(s String) (isNegative bool, decPos int, err error) {
decPos = -1
for i, c := range s {
if i == 0 && c == '-' {
isNegative = true
} else if c == '.' {
if decPos != -1 {
return false, -1, errors.New("not a valid number. multiple decimal points found.")
}
decPos = i
} else if c > '9' || c < '0' {
return false, -1, fmt.Errorf("for the string '%s' found invalid character '%s' at pos %d", s, string(c), i)
}
}
return buff[:w.offset], nil
return isNegative, decPos, nil
}
func (String) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
switch targetKind {
case BoolKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
s := val.(String)
if len(s) == 0 {
return NullValue, nil
}
b, err := strconv.ParseBool(strings.ToLower(string(s)))
if err != nil {
return Bool(false), CreateConversionError(s.Kind(), BoolKind, err)
}
return Bool(b), nil
}, nil
case FloatKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
s := val.(String)
if len(s) == 0 {
return NullValue, nil
}
f, err := strconv.ParseFloat(string(s), 64)
if err != nil {
return Float(math.NaN()), CreateConversionError(s.Kind(), FloatKind, err)
}
return Float(f), nil
}, nil
case InlineBlobKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
s := val.(String)
if len(s) == 0 {
return NullValue, nil
}
data, err := hex.DecodeString(string(s))
if err != nil {
return InlineBlob{}, CreateConversionError(s.Kind(), InlineBlobKind, err)
}
return InlineBlob(data), nil
}, nil
case IntKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
s := val.(String)
if len(s) == 0 {
return NullValue, nil
}
isNegative, decPos, err := parseNumber(s)
if err != nil {
return Int(0), CreateConversionError(s.Kind(), IntKind, err)
}
if decPos == 0 || (decPos == 1 && isNegative) {
return Int(0), nil
}
if decPos != -1 {
s = s[:decPos]
}
n, err := strconv.ParseInt(string(s), 10, 64)
if err != nil {
return Int(0), CreateConversionError(s.Kind(), IntKind, err)
}
return Int(n), nil
}, nil
case NullKind:
return func(Value) (Value, error) {
return NullValue, nil
}, nil
case StringKind:
return func(val Value) (Value, error) {
return val, nil
}, nil
case UintKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
s := val.(String)
if len(s) == 0 {
return NullValue, nil
}
n, err := strconv.ParseUint(string(s), 10, 64)
if err != nil {
return Uint(0), CreateConversionError(s.Kind(), UintKind, err)
}
return Uint(n), nil
}, nil
case UUIDKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
s := val.(String)
if len(s) == 0 {
return NullValue, nil
}
u, err := uuid.Parse(string(s))
if err != nil {
return UUID(u), CreateConversionError(s.Kind(), UUIDKind, err)
}
return UUID(u), nil
}, nil
}
return nil, CreateNoConversionError(StringKind, targetKind)
}
func (s String) HumanReadableString() string {
return strconv.Quote(string(s))
}

View File

@@ -50,5 +50,5 @@ func TestStringString(t *testing.T) {
}
func TestStringType(t *testing.T) {
assert.True(t, mustType(TypeOf(String("hi"))).Equals(StringType))
assert.True(t, mustType(TypeOf(String("hi"))).Equals(PrimitiveTypeMap[StringKind]))
}

View File

@@ -207,6 +207,10 @@ func (s Struct) Empty() bool {
}
// Value interface
func (s Struct) isPrimitive() bool {
return false
}
func (s Struct) Value(ctx context.Context) (Value, error) {
return s, nil
}
@@ -651,3 +655,23 @@ func verifyStructName(name string) {
verifyName(name, "")
}
}
func (Struct) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(StructKind, targetKind)
}
func (s Struct) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (s Struct) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
panic("unreachable")
}
func (s Struct) String() string {
panic("unreachable")
}
func (s Struct) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -100,17 +100,17 @@ func TestGenericStructSet(t *testing.T) {
s4, err := s.Set("b", Float(42))
assert.NoError(err)
assert.True(mustType(MakeStructType("S3",
StructField{"b", FloaTType, false},
StructField{"o", StringType, false},
StructField{"b", PrimitiveTypeMap[FloatKind], false},
StructField{"o", PrimitiveTypeMap[StringKind], false},
)).Equals(mustType(TypeOf(s4))))
// Adds a new field
s5, err := s.Set("x", Float(42))
assert.NoError(err)
assert.True(mustType(MakeStructType("S3",
StructField{"b", BoolType, false},
StructField{"o", StringType, false},
StructField{"x", FloaTType, false},
StructField{"b", PrimitiveTypeMap[BoolKind], false},
StructField{"o", PrimitiveTypeMap[StringKind], false},
StructField{"x", PrimitiveTypeMap[FloatKind], false},
)).Equals(mustType(TypeOf(s5))))
// Subtype is not equal.
@@ -119,7 +119,7 @@ func TestGenericStructSet(t *testing.T) {
s7, err := s6.Set("l", mustList(NewList(context.Background(), vs, Float(2), Float(3))))
assert.NoError(err)
t7, err := MakeStructTypeFromFields("", FieldMap{
"l": mustType(MakeListType(FloaTType)),
"l": mustType(MakeListType(PrimitiveTypeMap[FloatKind])),
})
assert.NoError(err)
assert.True(t7.Equals(mustType(TypeOf(s7))))

View File

@@ -208,8 +208,6 @@ func IsValueSubtypeOfDetails(nbf *NomsBinFormat, v Value, t *Type) (bool, bool,
func isValueSubtypeOfDetails(nbf *NomsBinFormat, v Value, t *Type, hasExtra bool) (bool, bool, error) {
switch t.TargetKind() {
case BoolKind, FloatKind, StringKind, BlobKind, TypeKind, UUIDKind, IntKind, UintKind, InlineBlobKind, NullKind:
return v.Kind() == t.TargetKind(), hasExtra, nil
case ValueKind:
return true, hasExtra, nil
case UnionKind:
@@ -263,6 +261,9 @@ func isValueSubtypeOfDetails(nbf *NomsBinFormat, v Value, t *Type, hasExtra bool
case CycleKind:
panic("unreachable") // CycleKind are ephemeral.
default:
if IsPrimitiveKind(t.TargetKind()) {
return v.Kind() == t.TargetKind(), hasExtra, nil
}
if v.Kind() != t.TargetKind() {
return false, hasExtra, nil
}

View File

@@ -51,20 +51,20 @@ func assertInvalid(tt *testing.T, t *Type, v Value) {
func assertAll(tt *testing.T, t *Type, v Value) {
allTypes := []*Type{
BoolType,
FloaTType,
StringType,
BlobType,
TypeType,
ValueType,
UUIDType,
IntType,
UintType,
InlineBlobType,
PrimitiveTypeMap[BoolKind],
PrimitiveTypeMap[FloatKind],
PrimitiveTypeMap[StringKind],
PrimitiveTypeMap[BlobKind],
PrimitiveTypeMap[TypeKind],
PrimitiveTypeMap[ValueKind],
PrimitiveTypeMap[UUIDKind],
PrimitiveTypeMap[IntKind],
PrimitiveTypeMap[UintKind],
PrimitiveTypeMap[InlineBlobKind],
}
for _, at := range allTypes {
if at == ValueType || t.Equals(at) {
if at == PrimitiveTypeMap[ValueKind] || t.Equals(at) {
assertSubtype(context.Background(), Format_7_18, at, v)
} else {
assertInvalid(tt, at, v)
@@ -73,34 +73,34 @@ func assertAll(tt *testing.T, t *Type, v Value) {
}
func TestAssertTypePrimitives(t *testing.T) {
assertSubtype(context.Background(), Format_7_18, BoolType, Bool(true))
assertSubtype(context.Background(), Format_7_18, BoolType, Bool(false))
assertSubtype(context.Background(), Format_7_18, FloaTType, Float(42))
assertSubtype(context.Background(), Format_7_18, StringType, String("abc"))
assertSubtype(context.Background(), Format_7_18, UUIDType, UUID(uuid.Must(uuid.NewUUID())))
assertSubtype(context.Background(), Format_7_18, IntType, Int(-1))
assertSubtype(context.Background(), Format_7_18, UintType, Uint(0xffffffffffffffff))
assertSubtype(context.Background(), Format_7_18, InlineBlobType, InlineBlob{})
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[BoolKind], Bool(true))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[BoolKind], Bool(false))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[FloatKind], Float(42))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[StringKind], String("abc"))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[UUIDKind], UUID(uuid.Must(uuid.NewUUID())))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[IntKind], Int(-1))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[UintKind], Uint(0xffffffffffffffff))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[InlineBlobKind], InlineBlob{})
assertInvalid(t, BoolType, Float(1))
assertInvalid(t, BoolType, String("abc"))
assertInvalid(t, FloaTType, Bool(true))
assertInvalid(t, StringType, UUID(uuid.Must(uuid.NewUUID())))
assertInvalid(t, UUIDType, String("abs"))
assertInvalid(t, IntType, Float(-1))
assertInvalid(t, UintType, Float(500))
assertInvalid(t, InlineBlobType, Int(742))
assertInvalid(t, PrimitiveTypeMap[BoolKind], Float(1))
assertInvalid(t, PrimitiveTypeMap[BoolKind], String("abc"))
assertInvalid(t, PrimitiveTypeMap[FloatKind], Bool(true))
assertInvalid(t, PrimitiveTypeMap[StringKind], UUID(uuid.Must(uuid.NewUUID())))
assertInvalid(t, PrimitiveTypeMap[UUIDKind], String("abs"))
assertInvalid(t, PrimitiveTypeMap[IntKind], Float(-1))
assertInvalid(t, PrimitiveTypeMap[UintKind], Float(500))
assertInvalid(t, PrimitiveTypeMap[InlineBlobKind], Int(742))
}
func TestAssertTypeValue(t *testing.T) {
vs := newTestValueStore()
assertSubtype(context.Background(), Format_7_18, ValueType, Bool(true))
assertSubtype(context.Background(), Format_7_18, ValueType, Float(1))
assertSubtype(context.Background(), Format_7_18, ValueType, String("abc"))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[ValueKind], Bool(true))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[ValueKind], Float(1))
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[ValueKind], String("abc"))
l, err := NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))
assert.NoError(t, err)
assertSubtype(context.Background(), Format_7_18, ValueType, l)
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[ValueKind], l)
}
func TestAssertTypeBlob(t *testing.T) {
@@ -108,87 +108,87 @@ func TestAssertTypeBlob(t *testing.T) {
blob, err := NewBlob(context.Background(), vs, bytes.NewBuffer([]byte{0x00, 0x01}))
assert.NoError(t, err)
assertAll(t, BlobType, blob)
assertAll(t, PrimitiveTypeMap[BlobKind], blob)
}
func TestAssertTypeList(tt *testing.T) {
vs := newTestValueStore()
listOfNumberType, err := MakeListType(FloaTType)
listOfNumberType, err := MakeListType(PrimitiveTypeMap[FloatKind])
l, err := NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, listOfNumberType, l)
assertAll(tt, listOfNumberType, l)
assertSubtype(context.Background(), Format_7_18, mustType(MakeListType(ValueType)), l)
assertSubtype(context.Background(), Format_7_18, mustType(MakeListType(PrimitiveTypeMap[ValueKind])), l)
}
func TestAssertTypeMap(tt *testing.T) {
vs := newTestValueStore()
mapOfNumberToStringType, err := MakeMapType(FloaTType, StringType)
mapOfNumberToStringType, err := MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])
assert.NoError(tt, err)
m, err := NewMap(context.Background(), vs, Float(0), String("a"), Float(2), String("b"))
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, mapOfNumberToStringType, m)
assertAll(tt, mapOfNumberToStringType, m)
assertSubtype(context.Background(), Format_7_18, mustType(MakeMapType(ValueType, ValueType)), m)
assertSubtype(context.Background(), Format_7_18, mustType(MakeMapType(PrimitiveTypeMap[ValueKind], PrimitiveTypeMap[ValueKind])), m)
}
func TestAssertTypeSet(tt *testing.T) {
vs := newTestValueStore()
setOfNumberType, err := MakeSetType(FloaTType)
setOfNumberType, err := MakeSetType(PrimitiveTypeMap[FloatKind])
assert.NoError(tt, err)
s, err := NewSet(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, setOfNumberType, s)
assertAll(tt, setOfNumberType, s)
assertSubtype(context.Background(), Format_7_18, mustType(MakeSetType(ValueType)), s)
assertSubtype(context.Background(), Format_7_18, mustType(MakeSetType(PrimitiveTypeMap[ValueKind])), s)
}
func TestAssertTypeType(tt *testing.T) {
t, err := MakeSetType(FloaTType)
t, err := MakeSetType(PrimitiveTypeMap[FloatKind])
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, TypeType, t)
assertAll(tt, TypeType, t)
assertSubtype(context.Background(), Format_7_18, ValueType, t)
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[TypeKind], t)
assertAll(tt, PrimitiveTypeMap[TypeKind], t)
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[ValueKind], t)
}
func TestAssertTypeStruct(tt *testing.T) {
t, err := MakeStructType("Struct", StructField{"x", BoolType, false})
t, err := MakeStructType("Struct", StructField{"x", PrimitiveTypeMap[BoolKind], false})
assert.NoError(tt, err)
v, err := NewStruct(Format_7_18, "Struct", StructData{"x": Bool(true)})
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, t, v)
assertAll(tt, t, v)
assertSubtype(context.Background(), Format_7_18, ValueType, v)
assertSubtype(context.Background(), Format_7_18, PrimitiveTypeMap[ValueKind], v)
}
func TestAssertTypeUnion(tt *testing.T) {
vs := newTestValueStore()
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(FloaTType)), Float(42))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(FloaTType, StringType)), Float(42))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(FloaTType, StringType)), String("hi"))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(FloaTType, StringType, BoolType)), Float(555))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(FloaTType, StringType, BoolType)), String("hi"))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(FloaTType, StringType, BoolType)), Bool(true))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(PrimitiveTypeMap[FloatKind])), Float(42))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])), Float(42))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])), String("hi"))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind])), Float(555))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind])), String("hi"))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind])), Bool(true))
lt, err := MakeListType(mustType(MakeUnionType(FloaTType, StringType)))
lt, err := MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])))
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, lt, mustList(NewList(context.Background(), vs, Float(1), String("hi"), Float(2), String("bye"))))
st, err := MakeSetType(StringType)
st, err := MakeSetType(PrimitiveTypeMap[StringKind])
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(st, FloaTType)), Float(42))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(st, FloaTType)), mustValue(NewSet(context.Background(), vs, String("a"), String("b"))))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(st, PrimitiveTypeMap[FloatKind])), Float(42))
assertSubtype(context.Background(), Format_7_18, mustType(MakeUnionType(st, PrimitiveTypeMap[FloatKind])), mustValue(NewSet(context.Background(), vs, String("a"), String("b"))))
assertInvalid(tt, mustType(MakeUnionType()), Float(42))
assertInvalid(tt, mustType(MakeUnionType(StringType)), Float(42))
assertInvalid(tt, mustType(MakeUnionType(StringType, BoolType)), Float(42))
assertInvalid(tt, mustType(MakeUnionType(st, StringType)), Float(42))
assertInvalid(tt, mustType(MakeUnionType(st, FloaTType)), mustValue(NewSet(context.Background(), vs, Float(1), Float(2))))
assertInvalid(tt, mustType(MakeUnionType(PrimitiveTypeMap[StringKind])), Float(42))
assertInvalid(tt, mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind])), Float(42))
assertInvalid(tt, mustType(MakeUnionType(st, PrimitiveTypeMap[StringKind])), Float(42))
assertInvalid(tt, mustType(MakeUnionType(st, PrimitiveTypeMap[FloatKind])), mustValue(NewSet(context.Background(), vs, Float(1), Float(2))))
}
func TestAssertConcreteTypeIsUnion(tt *testing.T) {
@@ -196,32 +196,32 @@ func TestAssertConcreteTypeIsUnion(tt *testing.T) {
Format_7_18,
mustType(MakeStructTypeFromFields("", FieldMap{})),
mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": StringType}))))))
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": PrimitiveTypeMap[StringKind]}))))))
assert.False(tt, IsSubtype(
Format_7_18,
mustType(MakeStructTypeFromFields("", FieldMap{})),
mustType(MakeUnionType(mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})), FloaTType))))
mustType(MakeUnionType(mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})), PrimitiveTypeMap[FloatKind]))))
assert.True(tt, IsSubtype(
Format_7_18,
mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": StringType})))),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": PrimitiveTypeMap[StringKind]})))),
mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType, "bar": StringType})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": StringType})))),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind], "bar": PrimitiveTypeMap[StringKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": PrimitiveTypeMap[StringKind]})))),
))
assert.False(tt, IsSubtype(
Format_7_18,
mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": StringType})))),
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind]})),
mustType(MakeStructTypeFromFields("", FieldMap{"bar": PrimitiveTypeMap[StringKind]})))),
mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("", FieldMap{"foo": StringType, "bar": StringType})),
FloaTType))))
mustType(MakeStructTypeFromFields("", FieldMap{"foo": PrimitiveTypeMap[StringKind], "bar": PrimitiveTypeMap[StringKind]})),
PrimitiveTypeMap[FloatKind]))))
}
func TestAssertTypeEmptyListUnion(tt *testing.T) {
@@ -239,7 +239,7 @@ func TestAssertTypeEmptyListUnion(tt *testing.T) {
func TestAssertTypeEmptyList(tt *testing.T) {
vs := newTestValueStore()
lt, err := MakeListType(FloaTType)
lt, err := MakeListType(PrimitiveTypeMap[FloatKind])
assert.NoError(tt, err)
l, err := NewList(context.Background(), vs)
assert.NoError(tt, err)
@@ -252,7 +252,7 @@ func TestAssertTypeEmptyList(tt *testing.T) {
func TestAssertTypeEmptySet(tt *testing.T) {
vs := newTestValueStore()
st, err := MakeSetType(FloaTType)
st, err := MakeSetType(PrimitiveTypeMap[FloatKind])
assert.NoError(tt, err)
s, err := NewSet(context.Background(), vs)
assert.NoError(tt, err)
@@ -265,7 +265,7 @@ func TestAssertTypeEmptySet(tt *testing.T) {
func TestAssertTypeEmptyMap(tt *testing.T) {
vs := newTestValueStore()
mt, err := MakeMapType(FloaTType, StringType)
mt, err := MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])
assert.NoError(tt, err)
m, err := NewMap(context.Background(), vs)
assert.NoError(tt, err)
@@ -278,9 +278,9 @@ func TestAssertTypeEmptyMap(tt *testing.T) {
}
func TestAssertTypeStructSubtypeByName(tt *testing.T) {
namedT, err := MakeStructType("Name", StructField{"x", FloaTType, false})
namedT, err := MakeStructType("Name", StructField{"x", PrimitiveTypeMap[FloatKind], false})
assert.NoError(tt, err)
anonT, err := MakeStructType("", StructField{"x", FloaTType, false})
anonT, err := MakeStructType("", StructField{"x", PrimitiveTypeMap[FloatKind], false})
assert.NoError(tt, err)
namedV, err := NewStruct(Format_7_18, "Name", StructData{"x": Float(42)})
assert.NoError(tt, err)
@@ -301,9 +301,9 @@ func TestAssertTypeStructSubtypeByName(tt *testing.T) {
func TestAssertTypeStructSubtypeExtraFields(tt *testing.T) {
at, err := MakeStructType("")
assert.NoError(tt, err)
bt, err := MakeStructType("", StructField{"x", FloaTType, false})
bt, err := MakeStructType("", StructField{"x", PrimitiveTypeMap[FloatKind], false})
assert.NoError(tt, err)
ct, err := MakeStructType("", StructField{"s", StringType, false}, StructField{"x", FloaTType, false})
ct, err := MakeStructType("", StructField{"s", PrimitiveTypeMap[StringKind], false}, StructField{"x", PrimitiveTypeMap[FloatKind], false})
assert.NoError(tt, err)
av, err := NewStruct(Format_7_18, "", StructData{})
assert.NoError(tt, err)
@@ -335,14 +335,14 @@ func TestAssertTypeStructSubtype(tt *testing.T) {
assert.NoError(tt, err)
t1, err := MakeStructType("Commit",
StructField{"parents", mustType(MakeSetType(mustType(MakeUnionType()))), false},
StructField{"value", FloaTType, false},
StructField{"value", PrimitiveTypeMap[FloatKind], false},
)
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, t1, c1)
t11, err := MakeStructType("Commit",
StructField{"parents", mustType(MakeSetType(mustType(MakeRefType(MakeCycleType("Commit"))))), false},
StructField{"value", FloaTType, false},
StructField{"value", PrimitiveTypeMap[FloatKind], false},
)
assert.NoError(tt, err)
assertSubtype(context.Background(), Format_7_18, t11, c1)
@@ -362,7 +362,7 @@ func TestAssertTypeCycleUnion(tt *testing.T) {
// }
t1, err := MakeStructType("S",
StructField{"x", MakeCycleType("S"), false},
StructField{"y", FloaTType, false},
StructField{"y", PrimitiveTypeMap[FloatKind], false},
)
assert.NoError(tt, err)
// struct S {
@@ -371,7 +371,7 @@ func TestAssertTypeCycleUnion(tt *testing.T) {
// }
t2, err := MakeStructType("S",
StructField{"x", MakeCycleType("S"), false},
StructField{"y", mustType(MakeUnionType(FloaTType, StringType)), false},
StructField{"y", mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])), false},
)
assert.NoError(tt, err)
@@ -383,8 +383,8 @@ func TestAssertTypeCycleUnion(tt *testing.T) {
// y: Float | String,
// }
t3, err := MakeStructType("S",
StructField{"x", mustType(MakeUnionType(MakeCycleType("S"), FloaTType)), false},
StructField{"y", mustType(MakeUnionType(FloaTType, StringType)), false},
StructField{"x", mustType(MakeUnionType(MakeCycleType("S"), PrimitiveTypeMap[FloatKind])), false},
StructField{"y", mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])), false},
)
assert.NoError(tt, err)
@@ -399,8 +399,8 @@ func TestAssertTypeCycleUnion(tt *testing.T) {
// y: Float,
// }
t4, err := MakeStructType("S",
StructField{"x", mustType(MakeUnionType(MakeCycleType("S"), FloaTType)), false},
StructField{"y", FloaTType, false},
StructField{"x", mustType(MakeUnionType(MakeCycleType("S"), PrimitiveTypeMap[FloatKind])), false},
StructField{"y", PrimitiveTypeMap[FloatKind], false},
)
assert.NoError(tt, err)
@@ -453,7 +453,7 @@ func TestIsSubtypeEmptySruct(tt *testing.T) {
// b: struct {},
// }
t1, err := MakeStructType("X",
StructField{"a", FloaTType, false},
StructField{"a", PrimitiveTypeMap[FloatKind], false},
StructField{"b", EmptyStructType, false},
)
assert.NoError(tt, err)
@@ -461,7 +461,7 @@ func TestIsSubtypeEmptySruct(tt *testing.T) {
// struct {
// a: Float,
// }
t2, err := MakeStructType("X", StructField{"a", FloaTType, false})
t2, err := MakeStructType("X", StructField{"a", PrimitiveTypeMap[FloatKind], false})
assert.NoError(tt, err)
assert.False(tt, IsSubtype(Format_7_18, t1, t2))
@@ -472,9 +472,9 @@ func TestIsSubtypeCompoundUnion(tt *testing.T) {
rt, err := MakeListType(EmptyStructType)
assert.NoError(tt, err)
st1, err := MakeStructType("One", StructField{"a", FloaTType, false})
st1, err := MakeStructType("One", StructField{"a", PrimitiveTypeMap[FloatKind], false})
assert.NoError(tt, err)
st2, err := MakeStructType("Two", StructField{"b", StringType, false})
st2, err := MakeStructType("Two", StructField{"b", PrimitiveTypeMap[StringKind], false})
assert.NoError(tt, err)
ct, err := MakeListType(mustType(MakeUnionType(st1, st2)))
assert.NoError(tt, err)
@@ -482,7 +482,7 @@ func TestIsSubtypeCompoundUnion(tt *testing.T) {
assert.True(tt, IsSubtype(Format_7_18, rt, ct))
assert.False(tt, IsSubtype(Format_7_18, ct, rt))
ct2, err := MakeListType(mustType(MakeUnionType(st1, st2, FloaTType)))
ct2, err := MakeListType(mustType(MakeUnionType(st1, st2, PrimitiveTypeMap[FloatKind])))
assert.NoError(tt, err)
assert.False(tt, IsSubtype(Format_7_18, rt, ct2))
assert.False(tt, IsSubtype(Format_7_18, ct2, rt))
@@ -491,18 +491,18 @@ func TestIsSubtypeCompoundUnion(tt *testing.T) {
func TestIsSubtypeOptionalFields(tt *testing.T) {
assert := assert.New(tt)
s1, err := MakeStructType("", StructField{"a", FloaTType, true})
s1, err := MakeStructType("", StructField{"a", PrimitiveTypeMap[FloatKind], true})
assert.NoError(err)
s2, err := MakeStructType("", StructField{"a", FloaTType, false})
s2, err := MakeStructType("", StructField{"a", PrimitiveTypeMap[FloatKind], false})
assert.NoError(err)
assert.True(IsSubtype(Format_7_18, s1, s2))
assert.False(IsSubtype(Format_7_18, s2, s1))
s3, err := MakeStructType("", StructField{"a", StringType, false})
s3, err := MakeStructType("", StructField{"a", PrimitiveTypeMap[StringKind], false})
assert.False(IsSubtype(Format_7_18, s1, s3))
assert.False(IsSubtype(Format_7_18, s3, s1))
s4, err := MakeStructType("", StructField{"a", StringType, true})
s4, err := MakeStructType("", StructField{"a", PrimitiveTypeMap[StringKind], true})
assert.False(IsSubtype(Format_7_18, s1, s4))
assert.False(IsSubtype(Format_7_18, s4, s1))
@@ -543,9 +543,9 @@ func TestIsSubtypeOptionalFields(tt *testing.T) {
test("a? c? e", "b d e", true, false)
test("a? c? e?", "b d", true, false)
t1, err := MakeStructType("", StructField{"a", BoolType, true})
t1, err := MakeStructType("", StructField{"a", PrimitiveTypeMap[BoolKind], true})
assert.NoError(err)
t2, err := MakeStructType("", StructField{"a", FloaTType, true})
t2, err := MakeStructType("", StructField{"a", PrimitiveTypeMap[FloatKind], true})
assert.NoError(err)
assert.False(IsSubtype(Format_7_18, t1, t2))
assert.False(IsSubtype(Format_7_18, t2, t1))
@@ -564,7 +564,7 @@ func makeTestStructTypeFromFieldNames(s string) (*Type, error) {
f = f[:len(f)-1]
optional = true
}
fields[i] = StructField{f, BoolType, optional}
fields[i] = StructField{f, PrimitiveTypeMap[BoolKind], optional}
}
return MakeStructType("", fields...)
}
@@ -649,15 +649,15 @@ func TestIsValueSubtypeOf(tt *testing.T) {
v Value
t *Type
}{
{Bool(true), BoolType},
{Float(42), FloaTType},
{String("s"), StringType},
{mustBlob(NewEmptyBlob(vs)), BlobType},
{BoolType, TypeType},
{mustList(NewList(context.Background(), vs, Float(42))), mustType(MakeListType(FloaTType))},
{mustValue(NewSet(context.Background(), vs, Float(42))), mustType(MakeSetType(FloaTType))},
{mustRef(NewRef(Float(42), Format_7_18)), mustType(MakeRefType(FloaTType))},
{mustValue(NewMap(context.Background(), vs, Float(42), String("a"))), mustType(MakeMapType(FloaTType, StringType))},
{Bool(true), PrimitiveTypeMap[BoolKind]},
{Float(42), PrimitiveTypeMap[FloatKind]},
{String("s"), PrimitiveTypeMap[StringKind]},
{mustBlob(NewEmptyBlob(vs)), PrimitiveTypeMap[BlobKind]},
{PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[TypeKind]},
{mustList(NewList(context.Background(), vs, Float(42))), mustType(MakeListType(PrimitiveTypeMap[FloatKind]))},
{mustValue(NewSet(context.Background(), vs, Float(42))), mustType(MakeSetType(PrimitiveTypeMap[FloatKind]))},
{mustRef(NewRef(Float(42), Format_7_18)), mustType(MakeRefType(PrimitiveTypeMap[FloatKind]))},
{mustValue(NewMap(context.Background(), vs, Float(42), String("a"))), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind]))},
{mustValue(NewStruct(Format_7_18, "A", StructData{})), mustType(MakeStructType("A"))},
// Not including CycleType or Union here
}
@@ -673,19 +673,19 @@ func TestIsValueSubtypeOf(tt *testing.T) {
}
for _, rec := range allTypes {
assertTrue(rec.v, ValueType)
assertTrue(rec.v, PrimitiveTypeMap[ValueKind])
}
assertTrue(Bool(true), mustType((MakeUnionType(BoolType, FloaTType))))
assertTrue(Float(123), mustType(MakeUnionType(BoolType, FloaTType)))
assertFalse(String("abc"), mustType(MakeUnionType(BoolType, FloaTType)))
assertTrue(Bool(true), mustType((MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind]))))
assertTrue(Float(123), mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))
assertFalse(String("abc"), mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))
assertFalse(String("abc"), mustType(MakeUnionType()))
assertTrue(mustList(NewList(context.Background(), vs)), mustType(MakeListType(FloaTType)))
assertTrue(mustList(NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeListType(FloaTType)))
assertFalse(mustList(NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeListType(BoolType)))
assertTrue(mustList(NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeListType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(mustList(NewList(context.Background(), vs, Float(0), Bool(true))), mustType(MakeListType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(mustList(NewList(context.Background(), vs)), mustType(MakeListType(PrimitiveTypeMap[FloatKind])))
assertTrue(mustList(NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeListType(PrimitiveTypeMap[FloatKind])))
assertFalse(mustList(NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeListType(PrimitiveTypeMap[BoolKind])))
assertTrue(mustList(NewList(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertTrue(mustList(NewList(context.Background(), vs, Float(0), Bool(true))), mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertFalse(mustList(NewList(context.Background(), vs, Float(0))), mustType(MakeListType(mustType(MakeUnionType()))))
assertTrue(mustList(NewList(context.Background(), vs)), mustType(MakeListType(mustType(MakeUnionType()))))
@@ -713,18 +713,18 @@ func TestIsValueSubtypeOf(tt *testing.T) {
return newList(mseq)
}
assertTrue(newChunkedList(Float(0), Float(1), Float(2), Float(3)), mustType(MakeListType(FloaTType)))
assertFalse(newChunkedList(Float(0), Float(1), Float(2), Float(3)), mustType(MakeListType(BoolType)))
assertTrue(newChunkedList(Float(0), Float(1), Float(2), Float(3)), mustType(MakeListType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(newChunkedList(Float(0), Bool(true)), mustType(MakeListType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(newChunkedList(Float(0), Float(1), Float(2), Float(3)), mustType(MakeListType(PrimitiveTypeMap[FloatKind])))
assertFalse(newChunkedList(Float(0), Float(1), Float(2), Float(3)), mustType(MakeListType(PrimitiveTypeMap[BoolKind])))
assertTrue(newChunkedList(Float(0), Float(1), Float(2), Float(3)), mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertTrue(newChunkedList(Float(0), Bool(true)), mustType(MakeListType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertFalse(newChunkedList(Float(0)), mustType(MakeListType(mustType(MakeUnionType()))))
}
assertTrue(mustValue(NewSet(context.Background(), vs)), mustType(MakeSetType(FloaTType)))
assertTrue(mustValue(NewSet(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeSetType(FloaTType)))
assertFalse(mustValue(NewSet(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeSetType(BoolType)))
assertTrue(mustValue(NewSet(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeSetType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(mustValue(NewSet(context.Background(), vs, Float(0), Bool(true))), mustType(MakeSetType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(mustValue(NewSet(context.Background(), vs)), mustType(MakeSetType(PrimitiveTypeMap[FloatKind])))
assertTrue(mustValue(NewSet(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeSetType(PrimitiveTypeMap[FloatKind])))
assertFalse(mustValue(NewSet(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeSetType(PrimitiveTypeMap[BoolKind])))
assertTrue(mustValue(NewSet(context.Background(), vs, Float(0), Float(1), Float(2), Float(3))), mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertTrue(mustValue(NewSet(context.Background(), vs, Float(0), Bool(true))), mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertFalse(mustValue(NewSet(context.Background(), vs, Float(0))), mustType(MakeSetType(mustType(MakeUnionType()))))
assertTrue(mustValue(NewSet(context.Background(), vs)), mustType(MakeSetType(mustType(MakeUnionType()))))
@@ -749,23 +749,23 @@ func TestIsValueSubtypeOf(tt *testing.T) {
}
return newSet(mustOrdSeq(newSetMetaSequence(1, tuples, vs)))
}
assertTrue(newChunkedSet(Float(0), Float(1), Float(2), Float(3)), mustType(MakeSetType(FloaTType)))
assertFalse(newChunkedSet(Float(0), Float(1), Float(2), Float(3)), mustType(MakeSetType(BoolType)))
assertTrue(newChunkedSet(Float(0), Float(1), Float(2), Float(3)), mustType(MakeSetType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(newChunkedSet(Float(0), Bool(true)), mustType(MakeSetType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(newChunkedSet(Float(0), Float(1), Float(2), Float(3)), mustType(MakeSetType(PrimitiveTypeMap[FloatKind])))
assertFalse(newChunkedSet(Float(0), Float(1), Float(2), Float(3)), mustType(MakeSetType(PrimitiveTypeMap[BoolKind])))
assertTrue(newChunkedSet(Float(0), Float(1), Float(2), Float(3)), mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertTrue(newChunkedSet(Float(0), Bool(true)), mustType(MakeSetType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertFalse(newChunkedSet(Float(0)), mustType(MakeSetType(mustType(MakeUnionType()))))
}
assertTrue(mustMap(NewMap(context.Background(), vs)), mustType(MakeMapType(FloaTType, StringType)))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(FloaTType, StringType)))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(BoolType, StringType)))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(FloaTType, BoolType)))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(mustType(MakeUnionType(FloaTType, BoolType)), StringType)))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(FloaTType, mustType(MakeUnionType(BoolType, StringType)))))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Bool(true), String("b"))), mustType(MakeMapType(mustType(MakeUnionType(FloaTType, BoolType)), StringType)))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), Bool(true))), mustType(MakeMapType(FloaTType, mustType(MakeUnionType(BoolType, StringType)))))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"))), mustType(MakeMapType(mustType(MakeUnionType()), StringType)))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"))), mustType(MakeMapType(FloaTType, mustType(MakeUnionType()))))
assertTrue(mustMap(NewMap(context.Background(), vs)), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])), PrimitiveTypeMap[StringKind])))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), String("b"))), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])))))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Bool(true), String("b"))), mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])), PrimitiveTypeMap[StringKind])))
assertTrue(mustMap(NewMap(context.Background(), vs, Float(0), String("a"), Float(1), Bool(true))), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])))))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"))), mustType(MakeMapType(mustType(MakeUnionType()), PrimitiveTypeMap[StringKind])))
assertFalse(mustMap(NewMap(context.Background(), vs, Float(0), String("a"))), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], mustType(MakeUnionType()))))
assertTrue(mustMap(NewMap(context.Background(), vs)), mustType(MakeMapType(mustType(MakeUnionType()), mustType(MakeUnionType()))))
{
@@ -790,29 +790,29 @@ func TestIsValueSubtypeOf(tt *testing.T) {
return newMap(mustOrdSeq(newMapMetaSequence(1, tuples, vs)))
}
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(FloaTType, StringType)))
assertFalse(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(BoolType, StringType)))
assertFalse(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(FloaTType, BoolType)))
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(mustType(MakeUnionType(FloaTType, BoolType)), StringType)))
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(FloaTType, mustType(MakeUnionType(BoolType, StringType)))))
assertTrue(newChunkedMap(Float(0), String("a"), Bool(true), String("b")), mustType(MakeMapType(mustType(MakeUnionType(FloaTType, BoolType)), StringType)))
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), Bool(true)), mustType(MakeMapType(FloaTType, mustType(MakeUnionType(BoolType, StringType)))))
assertFalse(newChunkedMap(Float(0), String("a")), mustType(MakeMapType(mustType(MakeUnionType()), StringType)))
assertFalse(newChunkedMap(Float(0), String("a")), mustType(MakeMapType(FloaTType, mustType(MakeUnionType()))))
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind])))
assertFalse(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])))
assertFalse(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])), PrimitiveTypeMap[StringKind])))
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), String("b")), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])))))
assertTrue(newChunkedMap(Float(0), String("a"), Bool(true), String("b")), mustType(MakeMapType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])), PrimitiveTypeMap[StringKind])))
assertTrue(newChunkedMap(Float(0), String("a"), Float(1), Bool(true)), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])))))
assertFalse(newChunkedMap(Float(0), String("a")), mustType(MakeMapType(mustType(MakeUnionType()), PrimitiveTypeMap[StringKind])))
assertFalse(newChunkedMap(Float(0), String("a")), mustType(MakeMapType(PrimitiveTypeMap[FloatKind], mustType(MakeUnionType()))))
}
assertTrue(mustRef(NewRef(Float(1), Format_7_18)), mustType(MakeRefType(FloaTType)))
assertFalse(mustRef(NewRef(Float(1), Format_7_18)), mustType(MakeRefType(BoolType)))
assertTrue(mustRef(NewRef(Float(1), Format_7_18)), mustType(MakeRefType(mustType(MakeUnionType(FloaTType, BoolType)))))
assertTrue(mustRef(NewRef(Float(1), Format_7_18)), mustType(MakeRefType(PrimitiveTypeMap[FloatKind])))
assertFalse(mustRef(NewRef(Float(1), Format_7_18)), mustType(MakeRefType(PrimitiveTypeMap[BoolKind])))
assertTrue(mustRef(NewRef(Float(1), Format_7_18)), mustType(MakeRefType(mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))))
assertFalse(mustRef(NewRef(Float(1), Format_7_18)), mustType(MakeRefType(mustType(MakeUnionType()))))
assertTrue(
mustValue(NewStruct(Format_7_18, "Struct", StructData{"x": Bool(true)})),
mustType(MakeStructType("Struct", StructField{"x", BoolType, false})),
mustType(MakeStructType("Struct", StructField{"x", PrimitiveTypeMap[BoolKind], false})),
)
assertTrue(
mustValue(NewStruct(Format_7_18, "Struct", StructData{"x": Bool(true)})),
mustType(MakeStructType("Struct", StructField{"x", BoolType, true})),
mustType(MakeStructType("Struct", StructField{"x", PrimitiveTypeMap[BoolKind], true})),
)
assertTrue(
mustValue(NewStruct(Format_7_18, "Struct", StructData{"x": Bool(true)})),
@@ -832,15 +832,15 @@ func TestIsValueSubtypeOf(tt *testing.T) {
)
assertTrue(
mustValue(NewStruct(Format_7_18, "Struct", StructData{"x": Bool(true)})),
mustType(MakeStructType("Struct", StructField{"x", mustType(MakeUnionType(BoolType, FloaTType)), true})),
mustType(MakeStructType("Struct", StructField{"x", mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), true})),
)
assertTrue(
mustValue(NewStruct(Format_7_18, "Struct", StructData{"x": Bool(true)})),
mustType(MakeStructType("Struct", StructField{"y", BoolType, true})),
mustType(MakeStructType("Struct", StructField{"y", PrimitiveTypeMap[BoolKind], true})),
)
assertFalse(
mustValue(NewStruct(Format_7_18, "Struct", StructData{"x": Bool(true)})),
mustType(MakeStructType("Struct", StructField{"x", StringType, true})),
mustType(MakeStructType("Struct", StructField{"x", PrimitiveTypeMap[StringKind], true})),
)
assertTrue(
@@ -854,7 +854,7 @@ func TestIsValueSubtypeOf(tt *testing.T) {
)),
})),
mustType(MakeStructType("Node",
StructField{"value", FloaTType, false},
StructField{"value", PrimitiveTypeMap[FloatKind], false},
StructField{"children", mustType(MakeListType(MakeCycleType("Node"))), false},
)),
)
@@ -870,7 +870,7 @@ func TestIsValueSubtypeOf(tt *testing.T) {
),
})),
mustType(MakeStructType("Node",
StructField{"value", FloaTType, false},
StructField{"value", PrimitiveTypeMap[FloatKind], false},
StructField{"children", mustType(MakeListType(MakeCycleType("Node"))), false},
)),
)
@@ -892,7 +892,7 @@ func TestIsValueSubtypeOf(tt *testing.T) {
}
requiredType, err := MakeStructType("Node",
StructField{"value", FloaTType, false},
StructField{"value", PrimitiveTypeMap[FloatKind], false},
StructField{"children", mustType(MakeListType(mustType(MakeRefType(MakeCycleType("Node"))))), false},
)
assert.NoError(err)
@@ -912,12 +912,12 @@ func TestIsValueSubtypeOf(tt *testing.T) {
{
t1, err := MakeStructType("A",
StructField{"a", FloaTType, false},
StructField{"a", PrimitiveTypeMap[FloatKind], false},
StructField{"b", MakeCycleType("A"), false},
)
assert.NoError(err)
t2, err := MakeStructType("A",
StructField{"a", FloaTType, false},
StructField{"a", PrimitiveTypeMap[FloatKind], false},
StructField{"b", MakeCycleType("A"), true},
)
assert.NoError(err)
@@ -935,8 +935,8 @@ func TestIsValueSubtypeOf(tt *testing.T) {
{
t, err := MakeStructType("A",
StructField{"aa", FloaTType, true},
StructField{"bb", BoolType, false},
StructField{"aa", PrimitiveTypeMap[FloatKind], true},
StructField{"bb", PrimitiveTypeMap[BoolKind], false},
)
assert.NoError(err)
v, err := NewStruct(Format_7_18, "A", StructData{

View File

@@ -225,6 +225,10 @@ func (t Tuple) Len() uint64 {
return count
}
func (t Tuple) isPrimitive() bool {
return false
}
func (t Tuple) Iterator() (*TupleIterator, error) {
return t.IteratorAt(0)
}
@@ -491,3 +495,23 @@ func (t Tuple) fieldsToMap() (map[Value]Value, error) {
return valMap, nil
}
func (Tuple) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(TupleKind, targetKind)
}
func (t Tuple) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (t Tuple) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
panic("unreachable")
}
func (t Tuple) String() string {
panic("unreachable")
}
func (t Tuple) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -96,6 +96,10 @@ func (t *Type) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(t, nbf)
}
func (t *Type) isPrimitive() bool {
return true
}
func (t *Type) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
err := TypeKind.writeTo(w, nbf)
@@ -119,7 +123,7 @@ func (t *Type) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (t *Type) typeOf() (*Type, error) {
return TypeType, nil
return PrimitiveTypeMap[TypeKind], nil
}
func (t *Type) Kind() NomsKind {
@@ -184,3 +188,23 @@ func indexOfType(t *Type, tl []*Type) (uint32, bool) {
}
return 0, false
}
func (*Type) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
return nil, CreateNoConversionError(TypeKind, targetKind)
}
func (t *Type) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
panic("unreachable")
}
func (t *Type) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
panic("unreachable")
}
func (t *Type) String() string {
panic("unreachable")
}
func (t *Type) HumanReadableString() string {
panic("unreachable")
}

View File

@@ -32,13 +32,13 @@ func TestTypes(t *testing.T) {
assert := assert.New(t)
vs := newTestValueStore()
mapType, err := MakeMapType(StringType, FloaTType)
mapType, err := MakeMapType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[FloatKind])
assert.NoError(err)
setType, err := MakeSetType(StringType)
setType, err := MakeSetType(PrimitiveTypeMap[StringKind])
assert.NoError(err)
mahType, err := MakeStructType("MahStruct",
StructField{"Field1", StringType, false},
StructField{"Field2", BoolType, false},
StructField{"Field1", PrimitiveTypeMap[StringKind], false},
StructField{"Field2", PrimitiveTypeMap[BoolKind], false},
)
assert.NoError(err)
recType, err := MakeStructType("RecursiveStruct", StructField{"self", MakeCycleType("RecursiveStruct"), false})
@@ -56,29 +56,29 @@ func TestTypes(t *testing.T) {
}
func TestTypeType(t *testing.T) {
assert.True(t, mustType(TypeOf(BoolType)).Equals(TypeType))
assert.True(t, mustType(TypeOf(PrimitiveTypeMap[BoolKind])).Equals(PrimitiveTypeMap[TypeKind]))
}
func TestTypeRefDescribe(t *testing.T) {
assert := assert.New(t)
mapType, err := MakeMapType(StringType, FloaTType)
mapType, err := MakeMapType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[FloatKind])
assert.NoError(err)
setType, err := MakeSetType(StringType)
setType, err := MakeSetType(PrimitiveTypeMap[StringKind])
assert.NoError(err)
assert.Equal("Bool", mustString(BoolType.Describe(context.Background())))
assert.Equal("Float", mustString(FloaTType.Describe(context.Background())))
assert.Equal("String", mustString(StringType.Describe(context.Background())))
assert.Equal("UUID", mustString(UUIDType.Describe(context.Background())))
assert.Equal("Int", mustString(IntType.Describe(context.Background())))
assert.Equal("Uint", mustString(UintType.Describe(context.Background())))
assert.Equal("InlineBlob", mustString(InlineBlobType.Describe(context.Background())))
assert.Equal("Bool", mustString(PrimitiveTypeMap[BoolKind].Describe(context.Background())))
assert.Equal("Float", mustString(PrimitiveTypeMap[FloatKind].Describe(context.Background())))
assert.Equal("String", mustString(PrimitiveTypeMap[StringKind].Describe(context.Background())))
assert.Equal("UUID", mustString(PrimitiveTypeMap[UUIDKind].Describe(context.Background())))
assert.Equal("Int", mustString(PrimitiveTypeMap[IntKind].Describe(context.Background())))
assert.Equal("Uint", mustString(PrimitiveTypeMap[UintKind].Describe(context.Background())))
assert.Equal("InlineBlob", mustString(PrimitiveTypeMap[InlineBlobKind].Describe(context.Background())))
assert.Equal("Map<String, Float>", mustString(mapType.Describe(context.Background())))
assert.Equal("Set<String>", mustString(setType.Describe(context.Background())))
mahType, err := MakeStructType("MahStruct",
StructField{"Field1", StringType, false},
StructField{"Field2", BoolType, false},
StructField{"Field1", PrimitiveTypeMap[StringKind], false},
StructField{"Field2", PrimitiveTypeMap[BoolKind], false},
)
assert.NoError(err)
assert.Equal("Struct MahStruct {\n Field1: String,\n Field2: Bool,\n}", mustString(mahType.Describe(context.Background())))
@@ -86,37 +86,37 @@ func TestTypeRefDescribe(t *testing.T) {
func TestTypeOrdered(t *testing.T) {
assert := assert.New(t)
assert.True(isKindOrderedByValue(BoolType.TargetKind()))
assert.True(isKindOrderedByValue(FloaTType.TargetKind()))
assert.True(isKindOrderedByValue(UUIDType.TargetKind()))
assert.True(isKindOrderedByValue(StringType.TargetKind()))
assert.True(isKindOrderedByValue(IntType.TargetKind()))
assert.True(isKindOrderedByValue(UintType.TargetKind()))
assert.True(isKindOrderedByValue(InlineBlobType.TargetKind()))
assert.True(isKindOrderedByValue(PrimitiveTypeMap[BoolKind].TargetKind()))
assert.True(isKindOrderedByValue(PrimitiveTypeMap[FloatKind].TargetKind()))
assert.True(isKindOrderedByValue(PrimitiveTypeMap[UUIDKind].TargetKind()))
assert.True(isKindOrderedByValue(PrimitiveTypeMap[StringKind].TargetKind()))
assert.True(isKindOrderedByValue(PrimitiveTypeMap[IntKind].TargetKind()))
assert.True(isKindOrderedByValue(PrimitiveTypeMap[UintKind].TargetKind()))
assert.True(isKindOrderedByValue(PrimitiveTypeMap[InlineBlobKind].TargetKind()))
assert.True(isKindOrderedByValue(TupleKind))
assert.False(isKindOrderedByValue(BlobType.TargetKind()))
assert.False(isKindOrderedByValue(ValueType.TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeListType(StringType)).TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeSetType(StringType)).TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeMapType(StringType, ValueType)).TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeRefType(StringType)).TargetKind()))
assert.False(isKindOrderedByValue(PrimitiveTypeMap[BlobKind].TargetKind()))
assert.False(isKindOrderedByValue(PrimitiveTypeMap[ValueKind].TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeListType(PrimitiveTypeMap[StringKind])).TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeSetType(PrimitiveTypeMap[StringKind])).TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeMapType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[ValueKind])).TargetKind()))
assert.False(isKindOrderedByValue(mustType(MakeRefType(PrimitiveTypeMap[StringKind])).TargetKind()))
}
func TestFlattenUnionTypes(t *testing.T) {
assert := assert.New(t)
assert.Equal(BoolType, mustType(MakeUnionType(BoolType)))
assert.Equal(PrimitiveTypeMap[BoolKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind])))
assert.Equal(mustType(MakeUnionType()), mustType(MakeUnionType()))
assert.Equal(mustType(MakeUnionType(BoolType, StringType)), mustType(MakeUnionType(BoolType, mustType(MakeUnionType(StringType)))))
assert.Equal(mustType(MakeUnionType(BoolType, StringType, FloaTType)), mustType(MakeUnionType(BoolType, mustType(MakeUnionType(StringType, FloaTType)))))
assert.Equal(BoolType, mustType(MakeUnionType(BoolType, BoolType)))
assert.Equal(BoolType, mustType(MakeUnionType(BoolType, mustType(MakeUnionType()))))
assert.Equal(BoolType, mustType(MakeUnionType(mustType(MakeUnionType()), BoolType)))
assert.Equal(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind])), mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], mustType(MakeUnionType(PrimitiveTypeMap[StringKind])))))
assert.Equal(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[StringKind], PrimitiveTypeMap[FloatKind])), mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], mustType(MakeUnionType(PrimitiveTypeMap[StringKind], PrimitiveTypeMap[FloatKind])))))
assert.Equal(PrimitiveTypeMap[BoolKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[BoolKind])))
assert.Equal(PrimitiveTypeMap[BoolKind], mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], mustType(MakeUnionType()))))
assert.Equal(PrimitiveTypeMap[BoolKind], mustType(MakeUnionType(mustType(MakeUnionType()), PrimitiveTypeMap[BoolKind])))
assert.True(mustType(MakeUnionType(mustType(MakeUnionType()), mustType(MakeUnionType()))).Equals(mustType(MakeUnionType())))
assert.Equal(mustType(MakeUnionType(BoolType, FloaTType)), mustType(MakeUnionType(BoolType, FloaTType)))
assert.Equal(mustType(MakeUnionType(BoolType, FloaTType)), mustType(MakeUnionType(FloaTType, BoolType)))
assert.Equal(mustType(MakeUnionType(BoolType, FloaTType)), mustType(MakeUnionType(BoolType, FloaTType, BoolType)))
assert.Equal(mustType(MakeUnionType(BoolType, FloaTType)), mustType(MakeUnionType(mustType(MakeUnionType(BoolType, FloaTType)), FloaTType, BoolType)))
assert.Equal(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])))
assert.Equal(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))
assert.Equal(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))
assert.Equal(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), mustType(MakeUnionType(mustType(MakeUnionType(PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[FloatKind])), PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[BoolKind])))
}
func TestVerifyStructFieldName(t *testing.T) {
@@ -124,7 +124,7 @@ func TestVerifyStructFieldName(t *testing.T) {
assertInvalid := func(n string) {
assert.Panics(func() {
MakeStructType("S", StructField{n, StringType, false})
MakeStructType("S", StructField{n, PrimitiveTypeMap[StringKind], false})
})
}
assertInvalid("")
@@ -138,7 +138,7 @@ func TestVerifyStructFieldName(t *testing.T) {
assertInvalid("💩")
assertValid := func(n string) {
MakeStructType("S", StructField{n, StringType, false})
MakeStructType("S", StructField{n, PrimitiveTypeMap[StringKind], false})
}
assertValid("a")
assertValid("A")
@@ -179,19 +179,19 @@ func TestVerifyStructName(t *testing.T) {
func TestStructUnionWithCycles(tt *testing.T) {
inodeType := mustType(MakeStructTypeFromFields("Inode", FieldMap{
"attr": mustType(MakeStructTypeFromFields("Attr", FieldMap{
"ctime": FloaTType,
"mode": FloaTType,
"mtime": FloaTType,
"ctime": PrimitiveTypeMap[FloatKind],
"mode": PrimitiveTypeMap[FloatKind],
"mtime": PrimitiveTypeMap[FloatKind],
})),
"contents": mustType(MakeUnionType(
mustType(MakeStructTypeFromFields("Directory", FieldMap{
"entries": mustType(MakeMapType(StringType, MakeCycleType("Inode"))),
"entries": mustType(MakeMapType(PrimitiveTypeMap[StringKind], MakeCycleType("Inode"))),
})),
mustType(MakeStructTypeFromFields("File", FieldMap{
"data": BlobType,
"data": PrimitiveTypeMap[BlobKind],
})),
mustType(MakeStructTypeFromFields("Symlink", FieldMap{
"targetPath": StringType,
"targetPath": PrimitiveTypeMap[StringKind],
})),
)),
}))
@@ -214,12 +214,12 @@ func TestStructUnionWithCycles(tt *testing.T) {
func TestHasStructCycles(tt *testing.T) {
assert := assert.New(tt)
assert.False(HasStructCycles(BoolType))
assert.False(HasStructCycles(BlobType))
assert.False(HasStructCycles(FloaTType))
assert.False(HasStructCycles(StringType))
assert.False(HasStructCycles(TypeType))
assert.False(HasStructCycles(ValueType))
assert.False(HasStructCycles(PrimitiveTypeMap[BoolKind]))
assert.False(HasStructCycles(PrimitiveTypeMap[BlobKind]))
assert.False(HasStructCycles(PrimitiveTypeMap[FloatKind]))
assert.False(HasStructCycles(PrimitiveTypeMap[StringKind]))
assert.False(HasStructCycles(PrimitiveTypeMap[TypeKind]))
assert.False(HasStructCycles(PrimitiveTypeMap[ValueKind]))
assert.Panics(func() {
HasStructCycles(MakeCycleType("Abc"))
})
@@ -253,12 +253,12 @@ func TestHasStructCycles(tt *testing.T) {
mustType(MakeStructType("",
StructField{
"a",
mustType(MakeStructType("", StructField{"b", BoolType, false})),
mustType(MakeStructType("", StructField{"b", PrimitiveTypeMap[BoolKind], false})),
false,
},
StructField{
"b",
mustType(MakeStructType("", StructField{"b", BoolType, false})),
mustType(MakeStructType("", StructField{"b", PrimitiveTypeMap[BoolKind], false})),
false},
))),
)

View File

@@ -23,7 +23,7 @@ package types
import (
"context"
"encoding/binary"
"strconv"
"github.com/liquidata-inc/dolt/go/store/hash"
)
@@ -52,6 +52,10 @@ func (v Uint) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(v, nbf)
}
func (v Uint) isPrimitive() bool {
return true
}
func (v Uint) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -61,7 +65,7 @@ func (v Uint) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (v Uint) typeOf() (*Type, error) {
return UintType, nil
return PrimitiveTypeMap[UintKind], nil
}
func (v Uint) Kind() NomsKind {
@@ -84,16 +88,62 @@ func (v Uint) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
return nil
}
func (v Uint) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
// We know the size of the buffer here so allocate it once.
// UintKind, int (Varint), exp (Varint)
buff := make([]byte, 1+2*binary.MaxVarintLen64)
w := binaryNomsWriter{buff, 0}
err := v.writeTo(&w, nbf)
func (v Uint) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
return Uint(b.readUint()), nil
}
if err != nil {
return nil, err
func (v Uint) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
b.skipUint()
}
func (Uint) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
switch targetKind {
case BoolKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(Uint))
return Bool(n != 0), nil
}, nil
case FloatKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(Uint))
return Float(float64(n)), nil
}, nil
case IntKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(Uint))
return Int(int64(n)), nil
}, nil
case NullKind:
return func(Value) (Value, error) {
return NullValue, nil
}, nil
case StringKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
n := uint64(val.(Uint))
str := strconv.FormatUint(n, 10)
return String(str), nil
}, nil
case UintKind:
return func(val Value) (Value, error) {
return val, nil
}, nil
}
return buff[:w.offset], nil
return nil, CreateNoConversionError(UintKind, targetKind)
}
func (v Uint) HumanReadableString() string {
return strconv.FormatUint(uint64(v), 10)
}

View File

@@ -56,6 +56,10 @@ func (v UUID) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
return getHash(v, nbf)
}
func (v UUID) isPrimitive() bool {
return true
}
func (v UUID) WalkValues(ctx context.Context, cb ValueCallback) error {
return nil
}
@@ -65,7 +69,7 @@ func (v UUID) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
}
func (v UUID) typeOf() (*Type, error) {
return UUIDType, nil
return PrimitiveTypeMap[UUIDKind], nil
}
func (v UUID) Kind() NomsKind {
@@ -85,22 +89,46 @@ func (v UUID) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
return err
}
w.writeBytes(byteSl)
w.writeRaw(byteSl)
return nil
}
func (v UUID) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
buff := make([]byte, 1+uuidNumBytes)
w := binaryNomsWriter{buff, 0}
err := v.writeTo(&w, nbf)
func (v UUID) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
id := UUID{}
copy(id[:uuidNumBytes], b.readBytes(uuidNumBytes))
return id, nil
}
if err != nil {
return nil, err
func (v UUID) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
b.skipBytes(uuidNumBytes)
}
func (UUID) GetMarshalFunc(targetKind NomsKind) (MarshalCallback, error) {
switch targetKind {
case NullKind:
return func(Value) (Value, error) {
return NullValue, nil
}, nil
case StringKind:
return func(val Value) (Value, error) {
if val == nil {
return nil, nil
}
return String(val.(UUID).String()), nil
}, nil
case UUIDKind:
return func(val Value) (Value, error) {
return val, nil
}, nil
}
return buff[:w.offset], err
return nil, CreateNoConversionError(UUIDKind, targetKind)
}
func (v UUID) String() string {
return uuid.UUID(v).String()
}
func (v UUID) HumanReadableString() string {
return v.String()
}

View File

@@ -30,6 +30,16 @@ import (
type ValueCallback func(v Value) error
type RefCallback func(ref Ref) error
type MarshalCallback func(val Value) (Value, error)
func init() {
for _, value := range KindToType {
if value != nil && value.isPrimitive() {
nomsKind := value.Kind()
PrimitiveTypeMap[nomsKind] = makePrimitiveType(nomsKind)
}
}
}
// Valuable is an interface from which a Value can be retrieved.
type Valuable interface {
@@ -65,6 +75,9 @@ type Value interface {
// same hash they must be equal.
Hash(*NomsBinFormat) (hash.Hash, error)
// isPrimitive returns whether the Value is a primitive type
isPrimitive() bool
// WalkValues iterates over the immediate children of this value in the DAG, if any, not including
// Type()
WalkValues(context.Context, ValueCallback) error
@@ -73,6 +86,13 @@ type Value interface {
// chunked then this will return the refs of th sub trees of the prolly-tree.
WalkRefs(*NomsBinFormat, RefCallback) error
// GetMarshalFunc takes in a Kind and returns a function that accepts a Value of the calling type.
// The returned function then marshals the given type into the given Kind.
GetMarshalFunc(NomsKind) (MarshalCallback, error)
// HumanReadableString returns a human-readable string version of this Value (not meant for re-parsing)
HumanReadableString() string
// typeOf is the internal implementation of types.TypeOf. It is not normalized
// and unions might have a single element, duplicates and be in the wrong
// order.
@@ -80,6 +100,12 @@ type Value interface {
// writeTo writes the encoded version of the value to a nomsWriter.
writeTo(nomsWriter, *NomsBinFormat) error
// readFrom reads the encoded version of the value from a binaryNomsReader
readFrom(*NomsBinFormat, *binaryNomsReader) (Value, error)
// skip takes in a binaryNomsReader and skips the encoded version of the value
skip(*NomsBinFormat, *binaryNomsReader)
}
type ValueSlice []Value
@@ -146,10 +172,6 @@ func (v valueImpl) writeTo(enc nomsWriter, nbf *NomsBinFormat) error {
return nil
}
func (v valueImpl) valueBytes(nbf *NomsBinFormat) ([]byte, error) {
return v.buff, nil
}
// IsZeroValue can be used to test if a Value is the same as T{}.
func (v valueImpl) IsZeroValue() bool {
return v.buff == nil
@@ -187,13 +209,14 @@ func (v valueImpl) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error)
}
func (v valueImpl) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
bts, err := v.valueBytes(nbf)
w := binaryNomsWriter{make([]byte, len(v.buff)+1), 0}
err := v.writeTo(&w, nbf)
if err != nil {
return err
}
return walkRefs(bts, nbf, cb)
return walkRefs(w.buff[:w.offset], nbf, cb)
}
type asValueImpl interface {

View File

@@ -264,61 +264,29 @@ func (r *valueDecoder) readValue(nbf *NomsBinFormat) (Value, error) {
switch k {
case BlobKind:
seq, err := r.readBlobSequence(nbf)
if err != nil {
return nil, err
}
return newBlob(seq), nil
case BoolKind:
r.skipKind()
return Bool(r.readBool()), nil
case FloatKind:
r.skipKind()
return r.readFloat(nbf), nil
case UUIDKind:
r.skipKind()
return r.readUUID(), nil
case IntKind:
r.skipKind()
return r.readInt(), nil
case UintKind:
r.skipKind()
return r.readUint(), nil
case NullKind:
r.skipKind()
return NullValue, nil
case StringKind:
r.skipKind()
return String(r.readString()), nil
case InlineBlobKind:
r.skipKind()
return r.readInlineBlob()
case ListKind:
seq, err := r.readListSequence(nbf)
if err != nil {
return nil, err
}
return newList(seq), nil
case MapKind:
seq, err := r.readMapSequence(nbf)
if err != nil {
return nil, err
}
return newMap(seq), nil
case RefKind:
return r.readRef(nbf)
case SetKind:
seq, err := r.readSetSequence(nbf)
if err != nil {
return nil, err
}
return newSet(seq), nil
case StructKind:
return r.readStruct(nbf)
@@ -331,6 +299,13 @@ func (r *valueDecoder) readValue(nbf *NomsBinFormat) (Value, error) {
d.Panic("A value instance can never have type %s", k)
}
if IsPrimitiveKind(k) {
if emptyVal, ok := KindToType[k]; ok {
r.skipKind()
return emptyVal.readFrom(nbf, &r.binaryNomsReader)
}
}
return nil, ErrUnknownType
}
@@ -339,79 +314,55 @@ func (r *valueDecoder) skipValue(nbf *NomsBinFormat) error {
switch k {
case BlobKind:
err := r.skipBlob(nbf)
if err != nil {
return err
}
case BoolKind:
r.skipKind()
r.skipBool()
case FloatKind:
r.skipKind()
r.skipFloat(nbf)
case UUIDKind:
r.skipKind()
r.skipUUID()
case NullKind:
r.skipKind()
case IntKind:
r.skipKind()
r.skipInt()
case UintKind:
r.skipKind()
r.skipUint()
case StringKind:
r.skipKind()
r.skipString()
case InlineBlobKind:
r.skipKind()
r.skipInlineBlob()
case ListKind:
err := r.skipList(nbf)
if err != nil {
return err
}
case MapKind:
err := r.skipMap(nbf)
if err != nil {
return err
}
case RefKind:
err := r.skipRef()
if err != nil {
return err
}
case SetKind:
err := r.skipSet(nbf)
if err != nil {
return err
}
case StructKind:
err := r.skipStruct(nbf)
if err != nil {
return err
}
case TupleKind:
err := r.skipTuple(nbf)
if err != nil {
return err
}
case TypeKind:
r.skipKind()
err := r.skipType()
if err != nil {
return err
}
case CycleKind, UnionKind, ValueKind:
d.Panic("A value instance can never have type %s", k)
default:
if IsPrimitiveKind(k) {
if emptyVal, ok := KindToType[k]; ok {
r.skipKind()
emptyVal.skip(nbf, &r.binaryNomsReader)
return nil
}
}
return ErrUnknownType
}
@@ -425,78 +376,46 @@ func (r *valueDecoder) readTypeOfValue(nbf *NomsBinFormat) (*Type, error) {
switch k {
case BlobKind:
err := r.skipBlob(nbf)
if err != nil {
return nil, err
}
return BlobType, nil
case BoolKind:
r.skipKind()
r.skipBool()
return BoolType, nil
case FloatKind:
r.skipKind()
r.skipFloat(nbf)
return FloaTType, nil
case UUIDKind:
r.skipKind()
r.skipUUID()
return UUIDType, nil
case IntKind:
r.skipKind()
r.skipInt()
return IntType, nil
case UintKind:
r.skipKind()
r.skipUint()
return UintType, nil
case InlineBlobKind:
r.skipKind()
r.skipInlineBlob()
return InlineBlobType, nil
case NullKind:
r.skipKind()
return NullType, nil
case StringKind:
r.skipKind()
r.skipString()
return StringType, nil
return PrimitiveTypeMap[BlobKind], nil
case ListKind, MapKind, RefKind, SetKind:
// These do not decode the actual values anyway.
val, err := r.readValue(nbf)
if err != nil {
return nil, err
}
d.Chk.True(val != nil)
return val.typeOf()
case StructKind:
return readStructTypeOfValue(nbf, r)
case TupleKind:
val, err := r.readValue(nbf)
if err != nil {
return nil, err
}
d.Chk.True(val != nil)
return val.typeOf()
case TypeKind:
r.skipKind()
err := r.skipType()
if err != nil {
return nil, err
}
return TypeType, nil
return PrimitiveTypeMap[TypeKind], nil
case CycleKind, UnionKind, ValueKind:
d.Panic("A value instance can never have type %s", k)
}
if IsPrimitiveKind(k) {
if emptyVal, ok := KindToType[k]; ok {
r.skipKind()
emptyVal.skip(nbf, &r.binaryNomsReader)
return PrimitiveTypeMap[k], nil
}
}
return nil, ErrUnknownType
}
@@ -512,13 +431,6 @@ func (r *valueDecoder) isValueSameTypeForSure(nbf *NomsBinFormat, t *Type) (bool
}
switch k {
case BlobKind, BoolKind, FloatKind, StringKind, UUIDKind, IntKind, UintKind, InlineBlobKind, NullKind:
err := r.skipValue(nbf)
if err != nil {
return false, err
}
return true, nil
case ListKind, MapKind, RefKind, SetKind, TupleKind:
// TODO: Maybe do some simple cases here too. Performance metrics should determine
// what is going to be worth doing.
@@ -532,7 +444,17 @@ func (r *valueDecoder) isValueSameTypeForSure(nbf *NomsBinFormat, t *Type) (bool
d.Panic("A value instance can never have type %s", k)
}
panic("not reachable")
// Captures all other types that are not the above special cases
if IsPrimitiveKind(k) {
err := r.skipValue(nbf)
if err != nil {
return false, err
}
return true, nil
}
panic("non-primitive type not special cased")
}
// isStringSame checks if the next string in the decoder matches string. It
@@ -614,7 +536,7 @@ func (r *typedBinaryNomsReader) skipType() error {
func (r *typedBinaryNomsReader) readTypeInner(seenStructs map[string]*Type) (*Type, error) {
k := r.readKind()
if _, supported := SupportedKinds[k]; !supported {
if _, supported := KindToType[k]; !supported {
return nil, ErrUnknownType
}

View File

@@ -176,29 +176,6 @@ func (r *refWalker) walkValue(nbf *NomsBinFormat, cb RefCallback) error {
switch k {
case BlobKind:
return r.walkBlob(nbf, cb)
case BoolKind:
r.skipKind()
r.skipBool()
case FloatKind:
r.skipKind()
r.skipFloat(nbf)
case IntKind:
r.skipKind()
r.skipInt()
case UintKind:
r.skipKind()
r.skipUint()
case UUIDKind:
r.skipKind()
r.skipUUID()
case NullKind:
r.skipKind()
case StringKind:
r.skipKind()
r.skipString()
case InlineBlobKind:
r.skipKind()
r.skipInlineBlob()
case ListKind:
return r.walkList(nbf, cb)
case MapKind:
@@ -217,6 +194,13 @@ func (r *refWalker) walkValue(nbf *NomsBinFormat, cb RefCallback) error {
case CycleKind, UnionKind, ValueKind:
d.Panic("A value instance can never have type %s", k)
default:
if IsPrimitiveKind(k) {
if emptyVal, ok := KindToType[k]; ok {
r.skipKind()
emptyVal.skip(nbf, &r.binaryNomsReader)
return nil
}
}
return ErrUnknownType
}

View File

@@ -129,61 +129,61 @@ func (suite *WalkAllTestSuite) TestWalkMultilevelList() {
func (suite *WalkAllTestSuite) TestWalkType() {
t, err := MakeStructTypeFromFields("TestStruct", FieldMap{
"s": StringType,
"b": BoolType,
"n": FloaTType,
"id": UUIDType,
"bl": BlobType,
"t": TypeType,
"v": ValueType,
"i": IntType,
"u": UintType,
"ua": InlineBlobType,
"s": PrimitiveTypeMap[StringKind],
"b": PrimitiveTypeMap[BoolKind],
"n": PrimitiveTypeMap[FloatKind],
"id": PrimitiveTypeMap[UUIDKind],
"bl": PrimitiveTypeMap[BlobKind],
"t": PrimitiveTypeMap[TypeKind],
"v": PrimitiveTypeMap[ValueKind],
"i": PrimitiveTypeMap[IntKind],
"u": PrimitiveTypeMap[UintKind],
"ib": PrimitiveTypeMap[InlineBlobKind],
})
suite.NoError(err)
suite.assertVisitedOnce(t, t)
suite.assertVisitedOnce(t, BoolType)
suite.assertVisitedOnce(t, FloaTType)
suite.assertVisitedOnce(t, UUIDType)
suite.assertVisitedOnce(t, IntType)
suite.assertVisitedOnce(t, UintType)
suite.assertVisitedOnce(t, StringType)
suite.assertVisitedOnce(t, BlobType)
suite.assertVisitedOnce(t, TypeType)
suite.assertVisitedOnce(t, ValueType)
suite.assertVisitedOnce(t, InlineBlobType)
suite.assertVisitedOnce(t, PrimitiveTypeMap[BoolKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[FloatKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[UUIDKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[IntKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[UintKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[StringKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[BlobKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[TypeKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[ValueKind])
suite.assertVisitedOnce(t, PrimitiveTypeMap[InlineBlobKind])
{
t2 := mustType(MakeListType(BoolType))
t2 := mustType(MakeListType(PrimitiveTypeMap[BoolKind]))
suite.assertVisitedOnce(t2, t2)
suite.assertVisitedOnce(t2, BoolType)
suite.assertVisitedOnce(t2, PrimitiveTypeMap[BoolKind])
}
{
t2 := mustType(MakeSetType(BoolType))
t2 := mustType(MakeSetType(PrimitiveTypeMap[BoolKind]))
suite.assertVisitedOnce(t2, t2)
suite.assertVisitedOnce(t2, BoolType)
suite.assertVisitedOnce(t2, PrimitiveTypeMap[BoolKind])
}
{
t2 := mustType(MakeRefType(BoolType))
t2 := mustType(MakeRefType(PrimitiveTypeMap[BoolKind]))
suite.assertVisitedOnce(t2, t2)
suite.assertVisitedOnce(t2, BoolType)
suite.assertVisitedOnce(t2, PrimitiveTypeMap[BoolKind])
}
t2 := mustType(MakeMapType(FloaTType, StringType))
t2 := mustType(MakeMapType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind]))
suite.assertVisitedOnce(t2, t2)
suite.assertVisitedOnce(t2, FloaTType)
suite.assertVisitedOnce(t2, StringType)
suite.assertVisitedOnce(t2, PrimitiveTypeMap[FloatKind])
suite.assertVisitedOnce(t2, PrimitiveTypeMap[StringKind])
t3 := mustType(MakeUnionType(FloaTType, StringType, BoolType, UUIDType))
t3 := mustType(MakeUnionType(PrimitiveTypeMap[FloatKind], PrimitiveTypeMap[StringKind], PrimitiveTypeMap[BoolKind], PrimitiveTypeMap[UUIDKind]))
suite.assertVisitedOnce(t3, t3)
suite.assertVisitedOnce(t3, BoolType)
suite.assertVisitedOnce(t3, FloaTType)
suite.assertVisitedOnce(t3, StringType)
suite.assertVisitedOnce(t3, UUIDType)
suite.assertVisitedOnce(t3, IntType)
suite.assertVisitedOnce(t3, UintType)
suite.assertVisitedOnce(t3, PrimitiveTypeMap[BoolKind])
suite.assertVisitedOnce(t3, PrimitiveTypeMap[FloatKind])
suite.assertVisitedOnce(t3, PrimitiveTypeMap[StringKind])
suite.assertVisitedOnce(t3, PrimitiveTypeMap[UUIDKind])
suite.assertVisitedOnce(t3, PrimitiveTypeMap[IntKind])
suite.assertVisitedOnce(t3, PrimitiveTypeMap[UintKind])
t4 := MakeCycleType("ABC")
suite.assertVisitedOnce(t4, t4)

View File

@@ -46,7 +46,7 @@ type DateTime struct {
// The field secSinceEpoch may contain fractions in cases where seconds are
// not sufficient.
var DateTimeType, _ = types.MakeStructTypeFromFields(datetypename, types.FieldMap{
"secSinceEpoch": types.FloaTType,
"secSinceEpoch": types.PrimitiveTypeMap[types.FloatKind],
})
var dateTimeTemplate = types.MakeStructTemplate(datetypename, []string{"secSinceEpoch"})