Пример #1
0
func TestSlice_make(t *testing.T) {
	l := []string{"a", "b"}
	x := testMarshal(t, l)
	var res []string
	err := Unmarshal(x, &res)
	if err != nil {
		t.Fatal(err)
	}
	if e := len(l); e != len(res) {
		t.Errorf("got: %d wanted: %d", len(res), e)
	}
	if e := l[0]; e != res[0] {
		t.Fatalf("got: %s wanted: %s", res[0], e)
	}

	// Now test new fancy libs.Slice()
	res = []string{}
	libs.Slice(x.Val(), &res)
	if e := len(l); e != len(res) {
		t.Errorf("got: %d wanted: %d", len(res), e)
	}
	if e := l[0]; e != res[0] {
		t.Fatalf("got: %s wanted: %s", res[0], e)
	}
}
Пример #2
0
func TestSlice_mixedtypes(t *testing.T) {
	infs := []interface{}{"a", "b", libs.SassNumber{Value: 1, Unit: "mm"}}
	sv := testMarshal(t, infs)
	var res []interface{}
	libs.Slice(sv.Val(), &res)

	if !reflect.DeepEqual(res, infs) {
		t.Errorf("got: % #v wanted: % #v\n", res, infs)
	}

}
Пример #3
0
func unmarshal(arg SassValue, v interface{}) error {
	sv := arg.Val()
	//Get the underlying value of v and its kind
	f := reflect.ValueOf(v)

	if f.Kind() == reflect.Ptr {
		f = f.Elem()
	}

	k := f.Kind()
	t := f.Type()

	if k == reflect.Interface {
		switch {
		case libs.IsNil(sv):
			f.Set(reflect.ValueOf("<nil>"))
			return nil
		case libs.IsString(sv):
			k = reflect.String
		case libs.IsBool(sv):
			k = reflect.Bool
		case libs.IsNumber(sv):
			k = reflect.Struct
		case libs.IsList(sv):
			k = reflect.Slice
			t = reflect.SliceOf(t)
		case libs.IsError(sv):
			// This should get implemented as type error
			k = reflect.String
		case libs.IsColor(sv):
			k = reflect.Struct
		default:
			return errors.New("Uncovertable interface value.")
		}
	}

	switch k {
	case reflect.Invalid:
		return errors.New("Invalid SASS Value - Taylor Swift")
	case reflect.String:
		if libs.IsString(sv) || libs.IsError(sv) {
			gc := libs.String(sv)
			//drop quotes
			if t, err := strconv.Unquote(gc); err == nil {
				gc = t
			}
			if strings.HasPrefix(gc, "'") && strings.HasSuffix(gc, "'") {
				gc = gc[1 : len(gc)-1]
			}
			if !f.CanSet() {
				return errors.New("Can not set string")
			}

			switch t := f.Kind(); t {
			case reflect.String:
				f.SetString(gc)
			case reflect.Interface:
				f.Set(reflect.ValueOf(gc))
			}
		} else {
			return throwMisMatchTypeError(arg, "string")
		}
	case reflect.Bool:
		if libs.IsBool(sv) {
			b := libs.Bool(sv)
			f.Set(reflect.ValueOf(b))
		} else {
			return throwMisMatchTypeError(arg, "bool")
		}
	case reflect.Struct:
		switch {
		case libs.IsColor(sv):
			col := libs.Color(sv)
			f.Set(reflect.ValueOf(col))
		case libs.IsNumber(sv):
			u, err := getSassNumberUnit(arg)
			if err != nil {
				return err
			}
			sn := libs.SassNumber{
				Value: libs.Float(sv),
				Unit:  u,
			}
			f.Set(reflect.ValueOf(sn))
		default:
			return throwMisMatchTypeError(arg, "color.RGBA or SassNumber")
		}
	case reflect.Slice:
		if !libs.IsList(sv) {
			return throwMisMatchTypeError(arg, "slice")
		}
		libs.Slice(arg.Val(), v)
	default:
		return errors.New("Unsupported SassValue")
	}
	return nil
}