AgentSkillsCN

sling-transforms

代理浏览器

SKILL.md
--- frontmatter
name: sling-transforms
description: >
  Data transformation functions for Sling replications, pipelines, and API specs.
  Use when transforming data, applying string/date/numeric functions, type casting, cleaning data, computing fields, or using expressions like trim, upper, lower, date_format, cast, coalesce, hash.

Data Transformation Functions

Sling provides 50+ built-in functions for data transformation in replications, pipelines, and API specs.

Transform Syntax

Array (Global)

Apply to all columns:

yaml
transforms: ['trim_space', 'remove_diacritics']

Map (Column-Specific)

Apply to specific columns:

yaml
transforms:
  name: ['trim_space', 'upper']
  email: ['lower']

Staged (Multi-Stage)

Multiple stages with expressions:

yaml
transforms:
  - name: 'trim_space(value)'
    email: 'lower(value)'

  - full_name: 'record.first_name + " " + record.last_name'

  - tier: 'record.total_spent >= 1000 ? "premium" : "standard"'

String Functions

FunctionDescriptionExample
upper(s)Uppercaseupper("hello") -> "HELLO"
lower(s)Lowercaselower("HELLO") -> "hello"
trim(s)Remove whitespacetrim(" hi ") -> "hi"
replace(s, old, new)Replace textreplace("a-b", "-", "_") -> "a_b"
substring(s, start, end)Extract substringsubstring("hello", 0, 2) -> "he"
split(s, sep)Split to arraysplit("a,b", ",") -> ["a","b"]
split_part(s, sep, i)Get split partsplit_part("a,b,c", ",", 1) -> "b"
join(arr, sep)Join arrayjoin(["a","b"], "-") -> "a-b"
length(s)String lengthlength("hello") -> 5
contains(s, sub)Check containscontains("hello", "ell") -> true
snake(s)Snake casesnake("Hello World") -> "hello_world"
slugify(s)URL slugslugify("Hello World!") -> "hello-world"
remove_diacritics(s)Remove accentsremove_diacritics("cafe") -> "cafe"
replace_non_printable(s)Clean stringRemoves control chars
replace_0x00(s)Remove null bytesCleans null chars

Numeric Functions

FunctionDescriptionExample
int_parse(v)Parse integerint_parse("42") -> 42
float_parse(v)Parse floatfloat_parse("3.14") -> 3.14
bool_parse(v)Parse booleanbool_parse("true") -> true
int_format(n, fmt)Format integerint_format(42, "%05d") -> "00042"
float_format(n, fmt)Format floatfloat_format(3.14, "%.1f") -> "3.1"
greatest(...)Maximum valuegreatest(1, 5, 3) -> 5
least(...)Minimum valueleast(1, 5, 3) -> 1
is_greater(a, b)Compareis_greater(5, 3) -> true
is_less(a, b)Compareis_less(3, 5) -> true
int_range(start, end)Generate rangeint_range(1, 3) -> [1,2,3]

Date Functions

FunctionDescriptionExample
now()Current timeReturns datetime
date_parse(s, fmt)Parse datedate_parse("2024-01-15", "auto")
date_format(d, fmt)Format datedate_format(now(), "%Y-%m-%d")
date_add(d, n, unit)Add durationdate_add(now(), -7, "day")
date_diff(d1, d2, unit)Differencedate_diff(d1, d2, "hour")
date_trunc(d, unit)Truncatedate_trunc(now(), "month")
date_extract(d, part)Extract partdate_extract(now(), "year")
date_first(d, period)First of perioddate_first(now(), "month")
date_last(d, period)Last of perioddate_last(now(), "month")
date_range(s, e, step)Date rangedate_range("2024-01-01", "2024-01-03", "1d")
date_timezone(d, tz)Convert TZdate_timezone(now(), "America/New_York")

Date units: year, month, week, day, hour, minute, second

Format syntax: Uses strftime (e.g., %Y-%m-%d %H:%M:%S)

Value Functions

FunctionDescriptionExample
coalesce(...)First non-nullcoalesce(null, "default")
first_valid(...)First non-emptyfirst_valid("", "value")
is_null(v)Check nullis_null(value)
is_empty(v)Check emptyis_empty("") -> true
cast(v, type)Convert typecast("42", "int")
try_cast(v, type)Safe converttry_cast("abc", "int") -> null
if(cond, t, f)Conditionalif(x > 0, "pos", "neg")
equals(a, b)Deep equalityequals(obj1, obj2)
require(v, msg)Assert non-nullrequire(v, "Required!")

Collection Functions

FunctionDescriptionExample
array(...)Create arrayarray(1, 2, 3)
object(k, v, ...)Create objectobject("a", 1, "b", 2)
keys(map)Get keyskeys({"a": 1}) -> ["a"]
values(map)Get valuesvalues({"a": 1}) -> [1]
exists(col, item)Check existsexists([1,2], 2) -> true
element(arr, i)Get elementelement([1,2,3], 1) -> 2
filter(arr, expr)Filter arrayfilter([1,2,3], "element > 1")
sort(arr, desc?)Sort arraysort([3,1,2]) -> [1,2,3]
pluck(arr, key)Extract fieldpluck([{a:1},{a:2}], "a") -> [1,2]
jmespath(obj, expr)JMESPath queryjmespath(data, "items[].name")
get_path(obj, path)Get by pathget_path(obj, "a.b[0]")
object_merge(...)Merge objectsobject_merge({a:1}, {b:2})
chunk(arr, size)Split chunkschunk([1,2,3,4], 2)

Encoding Functions

FunctionDescriptionExample
encode_url(s)URL encodeencode_url("a b") -> "a%20b"
decode_url(s)URL decodedecode_url("a%20b") -> "a b"
encode_base64(s)Base64 encodeencode_base64("hi")
decode_base64(s)Base64 decodedecode_base64("aGk=")
hash(s, algo)Hash stringhash("hello", "sha256")
json_parse(s)Parse JSONjson_parse('{"a":1}')

Utility Functions

FunctionDescriptionExample
uuid()Generate UUIDReturns UUID v4
log(msg)Log messagePrints and returns msg
regex_match(s, pat)Test patternregex_match("a1", "\\d")
regex_extract(s, pat, i)Extract matchregex_extract("id=123", "id=(\\d+)", 1)
regex_replace(s, pat, r)Replace patternregex_replace("a1b2", "\\d", "X")
type_of(v)Get typetype_of(42) -> "integer"

Transform Examples

Clean and Normalize

yaml
transforms:
  - name: 'trim_space(upper(value))'
    email: 'lower(trim_space(value))'
    phone: 'replace(value, "[^0-9]", "")'

Computed Fields

yaml
transforms:
  - full_name: 'record.first_name + " " + record.last_name'
    domain: 'split_part(record.email, "@", 1)'
    age: 'date_diff(now(), record.birth_date, "year")'

Conditional Logic

yaml
transforms:
  - status: 'record.active ? "active" : "inactive"'
    tier: |
      record.total >= 1000 ? "gold" : (
        record.total >= 500 ? "silver" : "bronze"
      )

Hash/Anonymize

yaml
transforms:
  - email_hash: 'hash(record.email, "sha256")'
    masked_ssn: '"XXX-XX-" + substring(record.ssn, -4)'

Full Documentation

See https://docs.slingdata.io/concepts/functions.md for complete reference.