예제 #1
0
func NewChannelMapper(fnSource string) *ChannelMapper {
	return &ChannelMapper{
		JSServer: walrus.NewJSServer(fnSource, kTaskCacheSize,
			func(fnSource string) (walrus.JSServerTask, error) {
				return NewSyncRunner(fnSource)
			}),
	}
}
예제 #2
0
func NewJSEventFunction(fnSource string) *JSEventFunction {

	base.LogTo("Events", "Creating new JSEventFunction")
	return &JSEventFunction{
		JSServer: walrus.NewJSServer(fnSource, kTaskCacheSize,
			func(fnSource string) (walrus.JSServerTask, error) {
				return newJsEventTask(fnSource)
			}),
	}
}
예제 #3
0
func NewChannelMapper(funcSource string) (*ChannelMapper, error) {
	funcSource = fmt.Sprintf(funcWrapper, funcSource)
	mapper := &ChannelMapper{}
	var err error
	mapper.js, err = walrus.NewJSServer(funcSource)
	mapper.Src = funcSource
	if err != nil {
		return nil, err
	}

	// Implementation of the 'channel()' callback:
	mapper.js.DefineNativeFunction("channel", func(call otto.FunctionCall) otto.Value {
		for _, arg := range call.ArgumentList {
			array := ottoValueToStringArray(arg)
			if array != nil {
				mapper.channels = append(mapper.channels, array...)
			}
		}
		return otto.UndefinedValue()
	})

	// Implementation of the 'access()' callback:
	mapper.js.DefineNativeFunction("access", func(call otto.FunctionCall) otto.Value {
		username := call.Argument(0)
		channels := call.Argument(1)
		usernameArray := ottoValueToStringArray(username)
		channelsArray := ottoValueToStringArray(channels)
		for _, name := range usernameArray {
			mapper.access[name] = append(mapper.access[name], channelsArray...)
		}
		return otto.UndefinedValue()
	})

	// Implementation of the 'reject()' callback:
	mapper.js.DefineNativeFunction("reject", func(call otto.FunctionCall) otto.Value {
		if mapper.output.Rejection == nil {
			if status, err := call.Argument(0).ToInteger(); err == nil && status >= 400 {
				var message string
				if len(call.ArgumentList) > 1 {
					message = call.Argument(1).String()
				}
				mapper.output.Rejection = &base.HTTPError{int(status), message}
			}
		}
		return otto.UndefinedValue()
	})

	mapper.js.Before = func() {
		mapper.output = &ChannelMapperOutput{}
		mapper.channels = []string{}
		mapper.access = map[string][]string{}
	}
	mapper.js.After = func(result otto.Value, err error) (interface{}, error) {
		output := mapper.output
		mapper.output = nil
		if err == nil {
			output.Channels, err = SetFromArray(mapper.channels, ExpandStar)
			if err == nil {
				output.Access = make(AccessMap, len(mapper.access))
				for username, channels := range mapper.access {
					output.Access[username], err = SetFromArray(channels, RemoveStar)
					if err != nil {
						break
					}
				}
			}
		}
		return output, err
	}
	return mapper, nil
}