Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Add type convertion operations + type coertions samples (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: Hernand Azevedo <[email protected]>

Signed-off-by: Hernand Azevedo <[email protected]>
  • Loading branch information
hernandazevedozup authored Nov 23, 2022
1 parent 60cd067 commit 4533bc3
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import br.com.zup.beagle.widget.context.expressionOf
const val REMOVE_AT_SIGN_AND_OPEN_KEYS = 2
const val REMOVE_KEYS_END = 1
/** Number **/
fun sum(vararg params: Bind<Number>): Bind.Expression<Number> = createOperation("sum", params)
fun subtract(vararg params: Bind<Number>): Bind.Expression<Number> = createOperation("subtract", params)
fun divide(vararg params: Bind<Number>): Bind.Expression<Number> = createOperation("divide", params)
fun multiply(vararg params: Bind<Number>): Bind.Expression<Number> = createOperation("multiply", params)
fun sum(vararg params: Bind<Any>): Bind.Expression<Any> = createOperation("sum", params)
fun subtract(vararg params: Bind<Any>): Bind.Expression<Any> = createOperation("subtract", params)
fun divide(vararg params: Bind<Any>): Bind.Expression<Any> = createOperation("divide", params)
fun multiply(vararg params: Bind<Any>): Bind.Expression<Any> = createOperation("multiply", params)

/** String **/
fun capitalize(param: Bind<String>): Bind.Expression<String> = createOperation("capitalize", arrayOf(param))
Expand All @@ -46,19 +46,19 @@ fun substring(param: Bind<String>, startIndex: Bind<Int>, endIndex: Bind<Int>? =
createOperation("substr", arrayOf(param, startIndex, endIndex))

/** comparison **/
fun <I> eq(firstParam: Bind<I>, secondParam: Bind<I>): Bind.Expression<Boolean> =
fun eq(firstParam: Bind<Any>, secondParam: Bind<Any>): Bind.Expression<Boolean> =
createOperation("eq", arrayOf(firstParam, secondParam))

fun gt(firstParam: Bind<Number>, secondParam: Bind<Number>): Bind.Expression<Boolean> =
fun gt(firstParam: Bind<Any>, secondParam: Bind<Any>): Bind.Expression<Boolean> =
createOperation("gt", arrayOf(firstParam, secondParam))

fun gte(firstParam: Bind<Number>, secondParam: Bind<Number>): Bind.Expression<Boolean> =
fun gte(firstParam: Bind<Any>, secondParam: Bind<Any>): Bind.Expression<Boolean> =
createOperation("gte", arrayOf(firstParam, secondParam))

fun lt(firstParam: Bind<Number>, secondParam: Bind<Number>): Bind.Expression<Boolean> =
fun lt(firstParam: Bind<Any>, secondParam: Bind<Any>): Bind.Expression<Boolean> =
createOperation("lt", arrayOf(firstParam, secondParam))

fun lte(firstParam: Bind<Number>, secondParam: Bind<Number>): Bind.Expression<Boolean> =
fun lte(firstParam: Bind<Any>, secondParam: Bind<Any>): Bind.Expression<Boolean> =
createOperation("lte", arrayOf(firstParam, secondParam))

/** logic **/
Expand Down Expand Up @@ -90,6 +90,9 @@ fun <I> union(firstArray: Bind<Array<I>>, secondArray: Bind<Array<I>>): Bind.Exp
fun isEmpty(param: Bind<Boolean>): Bind.Expression<Boolean> = createOperation("isEmpty", arrayOf(param))
fun isNull(param: Bind<Boolean>): Bind.Expression<Boolean> = createOperation("isNull", arrayOf(param))
fun length(param: Bind<Array<*>>): Bind.Expression<Int> = createOperation("length", arrayOf(param))
fun int(param: Bind<Any>): Bind.Expression<Int> = createOperation("int", arrayOf(param))
fun double(param: Bind<Any>): Bind.Expression<Double> = createOperation("double", arrayOf(param))
fun string(param: Bind<Any>): Bind.Expression<String> = createOperation("string", arrayOf(param))

fun <O> createOperation(operationType: String, params: Array<out Any?>): Bind.Expression<O> {
val values = params.filterNotNull().map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkSumExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{sum(1,2)}")
val expected = Bind.Expression<Any>(value = "@{sum(1,2)}")

// WHEN
val result = sum(constant(1), constant(2))
Expand All @@ -47,7 +47,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkSumDoubleExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{sum(2.5,2.5)}")
val expected = Bind.Expression<Any>(value = "@{sum(2.5,2.5)}")

// WHEN
val result = sum(constant(2.5), constant(2.5))
Expand All @@ -65,7 +65,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkSubtractExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{subtract(10,5)}")
val expected = Bind.Expression<Any>(value = "@{subtract(10,5)}")

// WHEN
val result = subtract(constant(10), constant(5))
Expand All @@ -78,7 +78,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkSubtractDoubleExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{subtract(10.5,5.5)}")
val expected = Bind.Expression<Any>(value = "@{subtract(10.5,5.5)}")

// WHEN
val result = subtract(constant(10.5), constant(5.5))
Expand All @@ -95,7 +95,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkMultiplyExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{multiply(2,2)}")
val expected = Bind.Expression<Any>(value = "@{multiply(2,2)}")

// WHEN
val result = multiply(constant(2), constant(2))
Expand All @@ -108,7 +108,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkMultiplyDoubleExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{multiply(2.5,2.5)}")
val expected = Bind.Expression<Any>(value = "@{multiply(2.5,2.5)}")

// WHEN
val result = multiply(constant(2.5), constant(2.5))
Expand All @@ -125,7 +125,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkDivideExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{divide(10,5)}")
val expected = Bind.Expression<Any>(value = "@{divide(10,5)}")

// WHEN
val result = divide(constant(10), constant(5))
Expand All @@ -138,7 +138,7 @@ internal class NumberOperationTest {
@DisplayName("Then should return correct expression")
fun checkDivideDoubleExpression() {
// GIVEN
val expected = Bind.Expression<Number>(value = "@{divide(10.5,5.25)}")
val expected = Bind.Expression<Any>(value = "@{divide(10.5,5.25)}")

// WHEN
val result = divide(constant(10.5), constant(5.25))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import br.com.zup.beagle.sample.constants.SCREEN_TEXT_ENDPOINT
import br.com.zup.beagle.sample.constants.SCREEN_TEXT_INPUT_ENDPOINT
import br.com.zup.beagle.sample.constants.SCREEN_TOUCHABLE_ENDPOINT
import br.com.zup.beagle.sample.constants.SCREEN_WEB_VIEW_ENDPOINT
import br.com.zup.beagle.sample.constants.OPERATIONS_ENDPOINT
import br.com.zup.beagle.widget.action.Navigate
import br.com.zup.beagle.widget.action.Route
import br.com.zup.beagle.widget.core.EdgeValue
Expand Down Expand Up @@ -80,6 +81,7 @@ object ComponentScreenBuilder : ScreenBuilder {
createMenu("ScrollView", SCREEN_SCROLL_VIEW_ENDPOINT),
createMenu("PageView", SCREEN_PAGE_VIEW_ENDPOINT),
createMenu("Action", SCREEN_ACTION_ENDPOINT),
createMenu("Operations", OPERATIONS_ENDPOINT),
createMenu("ScreenBuilder", SCREEN_BUILDER_ENDPOINT),
createMenu("LazyComponent", SCREEN_LAZY_COMPONENT_ENDPOINT),
createMenu("NavigationBar", SCREEN_NAVIGATION_BAR_ENDPOINT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import br.com.zup.beagle.operations.builtin.concat
import br.com.zup.beagle.operations.builtin.condition
import br.com.zup.beagle.operations.builtin.contains
import br.com.zup.beagle.operations.builtin.divide
import br.com.zup.beagle.operations.builtin.double
import br.com.zup.beagle.operations.builtin.eq
import br.com.zup.beagle.operations.builtin.gt
import br.com.zup.beagle.operations.builtin.gte
import br.com.zup.beagle.operations.builtin.insert
import br.com.zup.beagle.operations.builtin.int
import br.com.zup.beagle.operations.builtin.isEmpty
import br.com.zup.beagle.operations.builtin.isNull
import br.com.zup.beagle.operations.builtin.length
Expand All @@ -39,6 +41,7 @@ import br.com.zup.beagle.operations.builtin.or
import br.com.zup.beagle.operations.builtin.plus
import br.com.zup.beagle.operations.builtin.remove
import br.com.zup.beagle.operations.builtin.removeIndex
import br.com.zup.beagle.operations.builtin.string
import br.com.zup.beagle.operations.builtin.substring
import br.com.zup.beagle.operations.builtin.subtract
import br.com.zup.beagle.operations.builtin.sum
Expand Down Expand Up @@ -75,6 +78,7 @@ object OperationScreenBuilder : ScreenBuilder {
children = listOf(
stringOperations(),
numberOperations(),
conversionOperations(),
comparisonOperations(),
logicOperations(),
arrayOperations(),
Expand Down Expand Up @@ -103,7 +107,42 @@ object OperationScreenBuilder : ScreenBuilder {
Text(substring(constant("testing"), constant(3)).toBindString())
)
)
private fun conversionOperations() = Container(
children = listOf(
Container(
context = ContextData("number", 4),
children = listOf(
Text("Conversion operations", textColor = "#00c91b"),
Text("Int"),
Text(int(expressionOf("@{number}")).toBindString()),
Text(int(constant(1)).toBindString()),
Text(int(constant(1.1)).toBindString()),
Text(int(constant("1")).toBindString()),
Text(int(constant("1.1")).toBindString()),
Text(int(constant("string")).toBindString()),
Text("Double"),
Text(double(expressionOf("@{number}")).toBindString()),
Text(double(constant(1)).toBindString()),
Text(double(constant(1.1)).toBindString()),
Text(double(constant("1")).toBindString()),
Text(double(constant("1.1")).toBindString()),
Text(double(constant("string")).toBindString()),
Text("String"),
Text(string(expressionOf("@{number}")).toBindString()),
Text(string(constant(1)).toBindString()),
Text(string(constant(1.1)).toBindString()),
Text(string(constant("1")).toBindString()),
Text(string(constant("1.1")).toBindString()),
Text(string(constant("string")).toBindString()),
)
).setStyle {
margin =
EdgeValue(top = UnitValue.Companion.real(10), bottom = UnitValue.Companion.real(10))
}
)
)

@Suppress("LongMethod")
private fun numberOperations() = Container(
children = listOf(
Container(
Expand All @@ -123,7 +162,67 @@ object OperationScreenBuilder : ScreenBuilder {

Text(subtract(constant(1), constant(2), expressionOf("@{number}")).toBindString()),
Text(multiply(constant(1), constant(2), expressionOf("@{number}")).toBindString()),
Text(divide(constant(10.0), constant(2.0), expressionOf("@{number}")).toBindString())
Text(divide(constant(10.0), constant(2.0), expressionOf("@{number}")).toBindString()),
Text("Type Coercions", textColor = "#00c91b"),
Text("Subtract"),
Text(subtract(constant(4), constant(4)).toBindString()),
Text(subtract(constant(4.5), constant(6.0)).toBindString()),
Text(subtract(constant(4), constant(6), constant(4)).toBindString()),
Text(subtract(constant(2.5), constant(4.5), constant(6.0)).toBindString()),
Text(subtract(constant(1), constant(1.5)).toBindString()),
Text(subtract(constant(2.0), constant(1)).toBindString()),
Text(subtract(constant("1"), constant(1.0)).toBindString()),
Text(subtract(constant(2.5), constant("1.0")).toBindString()),
Text(subtract(constant("1"), constant("1")).toBindString()),
Text(subtract(constant("2"), constant(1)).toBindString()),
Text(subtract(constant("3.5"), constant(2)).toBindString()),
Text(subtract(constant(1), constant(true)).toBindString()),
Text(subtract().toBindString()),

Text("Sum"),
Text(sum(constant(4), constant(4)).toBindString()),
Text(sum(constant(4.5), constant(6.0)).toBindString()),
Text(sum(constant(4), constant(6), constant(4)).toBindString()),
Text(sum(constant(2.5), constant(4.5), constant(6.0)).toBindString()),
Text(sum(constant(1), constant(1.5)).toBindString()),
Text(sum(constant(2.0), constant(1)).toBindString()),
Text(sum(constant("1"), constant(1.0)).toBindString()),
Text(sum(constant(2.5), constant("1.0")).toBindString()),
Text(sum(constant("1"), constant("1")).toBindString()),
Text(sum(constant("2"), constant(1)).toBindString()),
Text(sum(constant("3.5"), constant(2)).toBindString()),
Text(sum(constant(1), constant(true)).toBindString()),
Text(sum().toBindString()),

Text("Divide"),
Text(divide(constant(4), constant(4)).toBindString()),
Text(divide(constant(4.5), constant(6.0)).toBindString()),
Text(divide(constant(4), constant(6), constant(4)).toBindString()),
Text(divide(constant(2.5), constant(4.5), constant(6.0)).toBindString()),
Text(divide(constant(1), constant(1.5)).toBindString()),
Text(divide(constant(2.0), constant(1)).toBindString()),
Text(divide(constant("1"), constant(1.0)).toBindString()),
Text(divide(constant(2.5), constant("1.0")).toBindString()),
Text(divide(constant("1"), constant("1")).toBindString()),
Text(divide(constant("2"), constant(1)).toBindString()),
Text(divide(constant("3.5"), constant(2)).toBindString()),
Text(divide(constant(1), constant(true)).toBindString()),
Text(divide().toBindString()),

Text("Multiply"),
Text(multiply(constant(4), constant(4)).toBindString()),
Text(multiply(constant(4.5), constant(6.0)).toBindString()),
Text(multiply(constant(4), constant(6), constant(4)).toBindString()),
Text(multiply(constant(2.5), constant(4.5), constant(6.0)).toBindString()),
Text(multiply(constant(1), constant(1.5)).toBindString()),
Text(multiply(constant(2.0), constant(1)).toBindString()),
Text(multiply(constant("1"), constant(1.0)).toBindString()),
Text(multiply(constant(2.5), constant("1.0")).toBindString()),
Text(multiply(constant("1"), constant("1")).toBindString()),
Text(multiply(constant("2"), constant(1)).toBindString()),
Text(multiply(constant("3.5"), constant(2)).toBindString()),
Text(multiply(constant(1), constant(true)).toBindString()),
Text(multiply().toBindString()),
)
).setStyle {
margin =
Expand All @@ -147,6 +246,41 @@ object OperationScreenBuilder : ScreenBuilder {

Text(lte(constant(2), expressionOf("@{comparison}")).toBindString()),
Text(lte(expressionOf("@{comparison}"), expressionOf("@{comparison}")).toBindString()),
Text("Type Coercions", textColor = "#00c91b"),
//gt
Text(constant("3 gt true = ").plus(gt(expressionOf("@{comparison}"), constant("true")).toBindString())),
Text(constant("`2` gt 1 = ").plus(gt(constant("2"), constant(1)).toBindString())),
Text(constant("`2.0` gt 1 = ").plus(gt(constant("2.0"), constant(1)).toBindString())),
Text(constant("`2.0` gt 2 = ").plus(gt(constant("2.0"), constant(2)).toBindString())),
Text(constant("`2` gt 2 = ").plus(gt(constant("2"), constant(2)).toBindString())),

//eq
Text(constant("3 eq true = ").plus(eq(expressionOf("@{comparison}"), constant("true")).toBindString())),
Text(constant("`2` eq 1 = ").plus(eq(constant("2"), constant(1)).toBindString())),
Text(constant("`2.0` eq 1 = ").plus(eq(constant("2.0"), constant(1)).toBindString())),
Text(constant("`2.0` eq 2 = ").plus(eq(constant("2.0"), constant(2)).toBindString())),
Text(constant("`2` eq 2 = ").plus(eq(constant("2"), constant(2)).toBindString())),

//gte
Text(constant("3 gte true = ").plus(gte(expressionOf("@{comparison}"), constant("true")).toBindString())),
Text(constant("`2` gte 1 = ").plus(gte(constant("2"), constant(1)).toBindString())),
Text(constant("`2.0` gte 1 = ").plus(gte(constant("2.0"), constant(1)).toBindString())),
Text(constant("`2.0` gte 2 = ").plus(gte(constant("2.0"), constant(2)).toBindString())),
Text(constant("`2` gte 2 = ").plus(gte(constant("2"), constant(2)).toBindString())),

//lte
Text(constant("3 lte true = ").plus(lte(expressionOf("@{comparison}"), constant("true")).toBindString())),
Text(constant("`2` lte 1 = ").plus(lte(constant("2"), constant(1)).toBindString())),
Text(constant("`2.0` lte 1 = ").plus(lte(constant("2.0"), constant(1)).toBindString())),
Text(constant("`2.0` lte 2 = ").plus(lte(constant("2.0"), constant(2)).toBindString())),
Text(constant("`2` lte 2 = ").plus(lte(constant("2"), constant(2)).toBindString())),

//lt
Text(constant("3 lt true = ").plus(lt(expressionOf("@{comparison}"), constant("true")).toBindString())),
Text(constant("`2` lt 1 = ").plus(lt(constant("2"), constant(1)).toBindString())),
Text(constant("`2.0` lt 1 = ").plus(lt(constant("2.0"), constant(1)).toBindString())),
Text(constant("`2.0` lt 2 = ").plus(lt(constant("2.0"), constant(2)).toBindString())),
Text(constant("`2` lt 2 = ").plus(lt(constant("2"), constant(2)).toBindString())),
)
).setStyle {
margin = EdgeValue(top = UnitValue.real(10))
Expand Down

0 comments on commit 4533bc3

Please sign in to comment.