iTranslated by AI
Simplifying Data Transformation by Moving Logic to YAML
Moving Transformation Logic to YAML Made Things Easier
Data transformation logic tends to end up scattered throughout the codebase before you even realize it.
Processing like "concatenate these fields..." or "change this value based on these conditions..." gets mixed into your business logic.
Then, when you hear "the external data format has changed," this happens:
It is painful to run through this whole cycle just because a field name changed.
So, I created a tool that allows you to extract transformation rules into a YAML file.
Playground: You can try it out immediately. GitHub:
Why it makes things easier
Extracting transformation logic to YAML provides the following benefits:
| Traditional | rulemorph |
|---|---|
| Logic scattered in code | Consolidated in YAML |
| Build/deploy on every change | Just replace the file |
- Transformation rules are centralized - No longer scattered in code
- Rules serve as specifications - YAML is human-readable
- No build needed for rule changes - Just swap the YAML file
Usage
Installation
# Homebrew (Recommended)
brew install vinhphatfsg/tap/rulemorph
# Or via Cargo
cargo install rulemorph_cli
Basic Workflow
With input data:
{ "users": [{ "user_id": 1, "full_name": "Alice", "username": "alice" }] }
Write the transformation rules in YAML:
version: 1
input:
format: json
json:
records_path: "users"
mappings:
- target: "id"
source: "input.user_id"
- target: "name"
source: "input.full_name"
- target: "email"
expr:
concat:
- { ref: "input.username" }
- "@example.com"
Run the command:
rulemorph transform -r rules.yaml -i input.json
And you get the result:
[{ "id": 1, "name": "Alice", "email": "alice@example.com" }]
Transformation Rules in YAML
Field Copying
# input: { "user_id": 123 }
# output: { "id": 123 }
- target: "id"
source: "input.user_id"
Setting Fixed Values
# input: { "name": "Alice" }
# output: { "name": "Alice", "version": "1.0" }
- target: "name"
source: "input.name"
- target: "version"
value: "1.0"
String Concatenation
# input: { "first": "Taro", "last": "Yamada" }
# output: { "name": "Yamada Taro" }
- target: "name"
expr:
concat: [{ ref: "input.last" }, " ", { ref: "input.first" }]
Conditional Mapping
# input: { "score": 80 }
# output: { "status": "pass" }
# input: { "score": 50 }
# output: { } (not output because when is false)
- target: "status"
value: "pass"
when:
">=": [{ ref: "input.score" }, 60]
Calculation and Reuse
# input: { "price": 1000, "quantity": 3 }
# output: { "subtotal": 3000, "tax": 300, "total": 3300 }
- target: "subtotal"
expr:
"*": [{ ref: "input.price" }, { ref: "input.quantity" }]
- target: "tax"
expr:
"*": [{ ref: "out.subtotal" }, 0.1]
- target: "total"
expr:
"+": [{ ref: "out.subtotal" }, { ref: "out.tax" }]
Chaining (Pipeline Style)
# input: { "name": " ALICE " }
# output: { "name": "alice" }
- target: "name"
expr:
chain:
- { ref: "input.name" }
- { op: trim }
- { op: lowercase }
Array Filtering and Mapping
# input: { "items": [{ "name": "A", "price": 500 }, { "name": "B", "price": 1500 }] }
# output: { "expensive_items": ["B"] }
- target: "expensive_items"
expr:
chain:
- { ref: "input.items" }
- op: filter
args:
- { ">": [{ ref: "item.price" }, 1000] }
- op: map
args:
- ref: "item.name"
Date Formatting
# input: { "date": "2024-01-15" }
# output: { "date_formatted": "2024/01/15" }
- target: "date_formatted"
expr:
date_format:
- { ref: "input.date" }
- "%Y/%m/%d"
- "%Y-%m-%d"
Lookup
# input: { "category_id": 2 }
# context: { "categories": [{ "id": 1, "name": "Food" }, { "id": 2, "name": "Book" }] }
# output: { "category": "Book" }
- target: "category"
expr:
lookup:
- { ref: "context.categories" }
- "id"
- { ref: "input.category_id" }
- "name"
CSV Support
version: 1
input:
format: csv
csv:
has_header: true
mappings:
- target: "amount"
source: "input.amount"
type: float
DTO Automatic Generation
You can generate type definitions from your rules:
rulemorph generate -r rules.yaml -l typescript
export interface Record {
id: number;
name: string;
email: string;
}
Supported languages: Rust, TypeScript, Python, Go, Java, Kotlin, Swift
MCP Support
You can integrate with Claude Desktop:
claude mcp add rulemorph -- rulemorph-mcp
Using as a Library
You can also incorporate it into your Rust project:
use rulemorph::{parse_rule_file, transform};
let rule = parse_rule_file(&std::fs::read_to_string("rules.yaml")?)?;
let output = transform(&rule, &std::fs::read_to_string("input.json")?, None)?;
Summary
By moving transformation logic to YAML:
- Logic is centralized, making it easier to manage.
- YAML serves directly as the specification document.
- Changes to rules can be handled simply by replacing the file.
GitHub:
I look forward to your feedback and contributions.
Discussion