Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Created August 15, 2024 18:12
Show Gist options
  • Save mariocesar/b3aba91763626a55f97244f39ffe3559 to your computer and use it in GitHub Desktop.
Save mariocesar/b3aba91763626a55f97244f39ffe3559 to your computer and use it in GitHub Desktop.
trasnform language
atom dimension {
width: int?
height: int?
depth: int?
}
atom money {
amount: int
currency: string
}
# input schema
input {
orders: list {
orderId: string
productList: list {
productName: string
productID: string? # ProductID is a string that needs to be parsed to an int, nullable
sku: string
description: {
color: string?
dimensions: dimension
weight: string? # Weight is a string that needs to be parsed to a float, nullable
}
price: string # Price is a string that needs to be parsed to a float
quantity: string? # Quantity is a string that needs to be parsed to an int, nullable
}
orderDate: string # OrderDate is a string that needs to be parsed to a datetime
} optional
}
# Define output schema
output {
orders: list {
order_id: string
product_list: list {
product_name: string
product_id: int?
sku: string
description: {
color: string?
dimensions: dimension
weight: float?
}
price: money
quantity: int?
}
order_date: datetime
} optional
totalWeight: float?
}
# Define functions for parsing values
function parseInt(s: string) -> int? {
return s != null ? int(s) : null
}
function parseFloat(s: string) -> float? {
return s != null ? float(s) : null
}
function parseDateTime(s: string) -> datetime? {
return s != null ? datetime.strptime(s, "%Y-%m-%dT%H:%M:%S") : null
}
function parseCurrency(s: string) -> money {
local parts = s.split(' ')
local amount = parseFloat(parts[0]) * 100
local currency = parts[1]
return {amount: int(amount), currency: currency}
}
# Define transformations
transform transformDimensions {
dimensions: {
width: parseInt(.width)
height: parseInt(.height)
depth: parseInt(.depth)
}
}
transform transformWeight {
weight: parseFloat(.weight)
}
transform transformPrice {
price: parseCurrency(.price)
}
transform transformQuantity {
quantity: parseInt(.quantity)
}
transform transformDescription {
description: {
color: .color,
dimensions: transformDimensions(.dimensions),
weight: transformWeight(.weight)
}
}
transform transformProduct {
product_name: .productName
product_id: parseInt(.productID)
sku: .sku
description: transformDescription(.description)
price: transformPrice(.price)
quantity: transformQuantity(.quantity)
}
transform transformOrder {
order_id: .orderId
product_list: .productList map transformProduct
order_date: parseDateTime(.orderDate)
}
transform renameAndParse {
orders: input.orders map transformOrder
}
transform calculateTotalWeight {
totalWeight: renameAndParse.orders[].product_list[].description.weight reduce sum
}
transform finalTransform {
renameAndParse
calculateTotalWeight
}
export finalTransform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment