0 0
Read Time:2 Minute, 2 Second

编程应该就是个 “数据转换的旅程”

elixir pipeline

举个简单的例子:

在某个 Web 的应用系统中, 有一个如下的转换数据的过程; 将 request 的数据转换为 response 的数据:

# request 的数据
request = """
GET /wildthings HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*

"""
# response 的数据
expected_response = """
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 20

Bears, Lions, Tigers
"""

我们为了要将上述的 request 的数据转换为 response 的数据, 便先建立了 elixir module; Servy.Handler。

defmodule Servy.Handler do

end

然后, 在 module; Servy.Handler; 中建立了以下的 functions (各 functions 的 elixir 代码, 将会于后续的文章中逐步的完善) :

def parse(request) do
     # TODO: Parse the request string into a map:

end

function parse; 对 request 的数据, 进行了第一次的转换, 將 request 数据中的:

  • Command: GET /wildthings HTTP/1.1
  • Head: Host: example.com User-Agent: ExampleBrowser/1.0 Accept: */*

转换为 key:value 形式的 map。此时的 map 中并没有 response body。

def route(conv) do
     # TODO: Create a new map that also has the response body:

end

function route; 对 request 的数据, 进行了第二次的转换: 将已转换为 map 的 request 的数据, 加入 response body。并将含有 response body 的 request 数据, 置入另一个新的 map。以使 map 能符合函数式编程中的 “不可变更” 的要求。

def format_response(conv) do
     # TODO: Use values in the map to create an HTTP response string:

end

function format_response; 对 request 的数据, 进行了第三次的转换; 將含有 response body 的 request 数据, 格式化成为 response 的数据。

def handle(request) do
    conv = parse(request)
    conv = route(conv)
    format_response(conv)
end

function handle; 经由调用 function parse, route, format_response, 完成将 request 的数据转换为 response 的数据。

elixir 有一个相当简洁的写法; pipeline; 可有效的提升我们代码的可读性。

def handle(request) do
     request
     |> parse
     |> route
     |> format_response
end

pipeline |>:

  • 在 pipeline 上所要传入到 function 的参数, 就将会是 function 的第一个参数。例如, 在 function handle 中, 要传入参数 request 到 function parse 中, 参数 request 的值就将会是 function parse 的第一个参数的值。
  • 在 pipeline 上的 function, 会自动的将其会回传的值, 直接的传入到 pipeline 上的下一个 function。当然, 其所传入的值, 也将会成为 pipeline 上的下一个 function 的第一个的参数的值。例如:function parse, 会将其回传的值; key:value 形式 (map) 的 Request 信息, 直接的传入到 function route。

样例代码如下:

defmodule Servy.Handler do
  def handle(request) do
    request
    |> parse
    |> route
    |> format_response
  end

  def parse(request) do
    # TODO: Parse the request string into a map:

  end

  def route(conv) do
    # TODO: Create a new map that also has the response body:

  end

  def format_response(conv) do
    # TODO: Use values in the map to create an HTTP response string:

  end

end


request = """
GET /wildthings HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*

"""

response = Servy.Handler.handle(request)
IO.puts response

elixir 相当的适合开发云端分布式的应用系统, 因为:

  • elixir 特有的简洁的语法; 将能因提升代码的可读性, 进而提升了开发的效率。
  • elixir 更可使我们在开发的阶段, 就能保证运维时应用系统的质量。

我将逐步的与大家分享。

About Post Author

方俊贤; Ken Fang

专利号: 201910652769.4; 一种深度学习的算法, 预测微服务持续发布、持续部署后对产品整体质量的影响, 获得国家知识财产局专利; 符合专利法实施细则第 44 条的规定。
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据