コード例 #1
0
ファイル: context.go プロジェクト: crackcomm/go-core
// Clear - Removes all context variables except those defined in `context` argument.
func Clear(ctx action.Map) (action.Map, error) {
	leave := ctx.Pull("context")

	// If `context` argument is empty -- clear entire context
	if leave.IsNil() {
		return action.Map{}, nil
	}

	// If `context` is a map -- it's a new context
	if clean, ok := leave.Map(); ok {
		return clean, nil
	}

	// If `context` is a string -- leave one variable
	if name, ok := leave.String(); ok {
		// Pop value
		value := ctx.Pop(name).Value

		// Close old context
		ctx.Close()
		return action.Map{name: value}, nil
	}

	// If `context` is a list -- create a new context and close old one
	list, _ := ctx.Pull("context").StringList()
	clean := make(action.Map)

	// Save all values from list
	for _, name := range list {
		value := ctx.Pop(name).Value
		clean.Add(name, value)
	}

	// Close old context
	ctx.Close()

	return clean, nil
}