func SassHandler(h SassFunc) libs.SassCallback { return func(v interface{}, usv libs.UnionSassValue, rsv *libs.UnionSassValue) error { if *rsv == nil { *rsv = libs.MakeNil() } libCtx, ok := v.(*compctx) if !ok { return errors.New("libsass Context not found") } ctx := NewCompilerContext(libCtx.compiler) // Cast to exported Go types req := SassValue{value: usv} res, err := h(ctx, req) if err != nil { // Returns the error to libsass Compiler *rsv = libs.MakeError(err.Error()) // Returning an error does nothing as libsass is in charge of // reporting error to user return err } *rsv = res.Val() return err } }
// make is needed to create types for use by test func makevalue(v interface{}) (SassValue, error) { f := reflect.ValueOf(v) var err error switch f.Kind() { default: return SassValue{value: libs.MakeNil()}, nil case reflect.Bool: b := v.(bool) return SassValue{value: libs.MakeBool(b)}, nil case reflect.String: s := v.(string) return SassValue{value: libs.MakeString(s)}, nil case reflect.Struct: //only SassNumber and color.RGBA are supported if sn, ok := v.(libs.SassNumber); ok { return SassValue{ value: libs.MakeNumber(sn.Float(), sn.UnitOf()), }, err } else if sc, ok := v.(color.RGBA); ok { return SassValue{value: libs.MakeColor(sc)}, nil } else { err = errors.New(fmt.Sprintf("The struct type %s is unsupported for marshalling", reflect.TypeOf(v).String())) return SassValue{value: libs.MakeNil()}, err } case reflect.Slice: // Initialize the list lst := libs.MakeList(f.Len()) for i := 0; i < f.Len(); i++ { t, er := makevalue(f.Index(i).Interface()) if err == nil && er != nil { err = er } libs.SetIndex(lst, i, t.Val()) } return SassValue{value: lst}, err } }
// Handler accepts a HandlerFunc and returns SassCallback for sending // to libsass. The third argument must be a pointer and the function // must return an error. func Handler(h HandlerFunc) libs.SassCallback { return func(v interface{}, usv libs.UnionSassValue, rsv *libs.UnionSassValue) error { if *rsv == nil { *rsv = libs.MakeNil() } req := SassValue{value: usv} res := SassValue{value: *rsv} err := h(v, req, &res) if rsv != nil { *rsv = res.Val() } return err } }
// Handler accepts a HandlerFunc and returns SassCallback for sending // to libsass. The third argument must be a pointer and the function // must return an error. func Handler(h HandlerFunc) libs.SassCallback { return func(v interface{}, usv libs.UnionSassValue, rsv *libs.UnionSassValue) error { if *rsv == nil { *rsv = libs.MakeNil() } req := SassValue{value: usv} res := SassValue{value: *rsv} err := h(v, req, &res) // FIXME: This shouldn't be happening, handler should assign // to the address properly. if rsv != nil { *rsv = res.Val() } return err } }