Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

OML Pipe Functions 函数索引

本文档列出了 WP-Motor OML 语言中所有可用的 pipe function(管道函数)。

字段访问函数 (Field Accessors)

函数语法说明文档
taketake(field_name)从输入数据中提取指定字段-
getget(key)从嵌套结构中获取指定键的值-
nthnth(index)从数组中获取指定索引的元素-

字符串匹配函数 (String Matching)

函数语法说明文档
starts_withstarts_with('prefix')检查字符串是否以指定前缀开始,否则转为 ignore📖 详细文档

值转换函数 (Value Transformation)

函数语法说明文档
map_tomap_to(value)将非 ignore 字段映射到指定值(支持多种类型)📖 详细文档
to_strto_str将字段值转换为字符串-
to_jsonto_json将字段值转换为 JSON 字符串-

编码/解码函数 (Encoding/Decoding)

函数语法说明文档
base64_encodebase64_encodeBase64 编码字符串-
base64_decodebase64_decode(encoding)Base64 解码字符串(可指定编码)-
html_escapehtml_escapeHTML 转义字符串-
html_unescapehtml_unescapeHTML 反转义字符串-
json_escapejson_escapeJSON 转义字符串-
json_unescapejson_unescapeJSON 反转义字符串-
str_escapestr_escape通用字符串转义-

时间转换函数 (Time Conversion)

函数语法说明文档
Time::to_tsTime::to_ts将时间转换为秒级时间戳-
Time::to_ts_msTime::to_ts_ms将时间转换为毫秒级时间戳-
Time::to_ts_usTime::to_ts_us将时间转换为微秒级时间戳-
Time::to_ts_zoneTime::to_ts_zone(zone, unit)将时间转换为指定时区的时间戳-

网络函数 (Network)

函数语法说明文档
ip4_to_intip4_to_int将 IPv4 地址转换为整数-

URL/路径解析函数 (URL/Path Parsing)

函数语法说明文档
pathpath(type)从路径字符串中提取指定部分-
urlurl(type)从 URL 字符串中提取指定部分-

控制流函数 (Control Flow)

函数语法说明文档
skip_emptyskip_empty跳过空值字段-

函数分类总览

按功能分类

1. 数据提取函数

从输入数据或嵌套结构中提取字段。

  • take(field): 提取顶层字段
  • get(key): 提取嵌套字段
  • nth(index): 提取数组元素

2. 条件过滤函数

检查条件,不满足时转换为 ignore。

  • starts_with(prefix): 前缀匹配

3. 值映射函数

修改字段值或类型。

  • map_to(value): 通用值映射
  • to_str: 转字符串
  • to_json: 转 JSON

4. 编码转换函数

在不同编码格式之间转换。

  • Base64: base64_encode, base64_decode
  • HTML: html_escape, html_unescape
  • JSON: json_escape, json_unescape

5. 时间处理函数

处理时间相关的转换。

  • 时间戳转换: Time::to_ts*
  • 时区转换: Time::to_ts_zone

6. 网络处理函数

处理网络相关数据。

  • IP 地址: ip4_to_int
  • URL 解析: url(type)
  • 路径解析: path(type)

使用示例

基本管道

name : basic_pipeline
---
# 1. 提取字段
result = pipe take(message)
    # 2. 过滤条件
    | starts_with('ERROR')
    # 3. 值映射
    | map_to('error_type');

复杂转换

name : complex_transformation
---
# 提取并转换时间
timestamp_ms = pipe take(time_str)
    | Time::to_ts_ms;

# Base64 编码
encoded = pipe take(message)
    | base64_encode;

# URL 解析
host = pipe take(url)
    | url(host);

条件分类

name : conditional_classification
---
# 根据 URL 前缀分类安全级别
high_security = pipe take(url)
    | starts_with('https://')
    | map_to(3);

low_security = pipe take(url)
    | starts_with('http://')
    | map_to(1);

数据清洗

name : data_cleaning
---
# 提取并清理数据
clean_message = pipe take(raw_message)
    | html_unescape
    | json_unescape
    | skip_empty;

性能参考

函数类型典型性能说明
字段访问< 100ns基于哈希表查找
字符串匹配< 1μs简单前缀比较
值映射< 100ns直接值替换
Base64 编码1-10μs取决于字符串长度
时间转换1-5μs时间解析和转换
URL 解析1-10μsURL 结构解析

最佳实践

1. 合理使用管道链

# ✅ 推荐:按逻辑顺序组织管道
result = pipe take(field)
    | starts_with('prefix')  # 先过滤
    | map_to('value');      # 再映射

# ⚠️ 避免:不必要的长管道
result = pipe take(field)
    | to_str
    | to_json
    | to_str  # 冗余操作

2. 利用 ignore 传播

# ✅ 推荐:利用 ignore 跳过后续处理
secure_flag = pipe take(url)
    | starts_with('https://')  # 失败返回 ignore
    | map_to(true);           # ignore 会跳过此步

# 这样可以安全地处理条件逻辑

3. 选择合适的函数

# ✅ 推荐:使用专用函数
result = pipe take(field) | map_to(123);  # 自动推断为整数

# ⚠️ 不推荐:手动转换
result = pipe take(field) | to_str | map_to('123');  # 额外开销

4. 避免重复提取

# ✅ 推荐:一次提取,多次使用
url_field = pipe take(url);
host = pipe take(url) | url(host);
path = pipe take(url) | url(path);

# ⚠️ 避免:每次都提取同一字段(性能影响小但不够清晰)

函数对比

starts_with vs 正则表达式

特性starts_with正则表达式
性能极快较慢
功能前缀匹配复杂模式
使用难度简单需要学习
失败行为转为 ignore-

map_to vs to_str

特性map_toto_str
功能值替换类型转换
类型支持多种仅字符串
ignore 保留
用途条件赋值类型转换

相关文档

版本历史

  • 1.13.4 (2026-02-03)

    • 新增 starts_with 函数
    • 新增 map_to 函数,支持多种类型自动推断
    • 完善文档体系
  • 1.13.3 (2026-02-03)

    • 修复编译错误
  • 1.13.2 (2026-02-03)

    • 完善 pipe 函数支持

提示: OML pipe 函数设计用于数据转换和映射。合理使用 ignore 机制可以实现灵活的条件逻辑。