Skip to content

Instantly share code, notes, and snippets.

Avatar
🤞
Oh God, please, don't crash now!

Andrii Matkivskyi amatkivskiy

🤞
Oh God, please, don't crash now!
View GitHub Profile
View nested-fix.kt
val child = parentObj.child?.child?.child
if (child != null) {
doSomethingOnBooleanValue(child.isValid)
} else {
// handle null value here
}
fun doSomethingOnBooleanValue(valid: Boolean) {
if (valid) {
// `valid` is really true
View nesting-complex.kt
data class Parent(val child: Nested?)
class Nested(val child: Nested?, val isValid: Boolean = false)
// Assume you got this object from the dark and deep internals of your app
val parentObj = getParentObjFromInternals()
doSomethingOnBooleanValue(parentObj.child?.child?.child?.isValid)
fun doSomethingOnBooleanValue(valid: Boolean?) {
if (valid == true) {
View nesting-simple.kt
// Arrange
data class Parent(val child: Nested?)
class Nested(val child: Nested?, val isValid : Boolean = false)
// Assume you got this object from the dark and deep internals of your app
val parentObj = getParentObjFromInternals()
// This is straightforward but it won't work
if (parentObj.child?.child?.child?.isValid) {
// Error.
View worst-child-parent.kt
fun childFunction(parentObj: Parent) {
// Do whatever needed with child obj
println("child = ${parentObj.child.child?.child?.child}")
}
View child-parent.kt
fun parentFunction() {
val parentObj = Parent(Child())
childFunction(parentObj)
}
fun childFunction(parentObj: Parent) {
// Do whatever needed with child obj
println("child = ${parentObj.child}")
}
data class Parent(val child: Child)
class Child
View jacoco.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE report
PUBLIC '-//JACOCO//DTD Report 1.1//EN'
'report.dtd'>
<report name="app">
<sessioninfo dump="1575989429103" id="fv-az606-3e43c913" start="1575989418154"/>
<package name=" com/java/android/example/domain/model">
<class name=" com/java/android/example/domain/model/TripEvents" sourcefilename="Trip.kt">
<method desc="()Ljava/lang/String;" line="40" name="getDistance">
<counter covered="0" missed="3" type="INSTRUCTION"/>
@amatkivskiy
amatkivskiy / TokenDataProviderTest.kt
Created Nov 25, 2018
medium_TokenDataProviderTest
View TokenDataProviderTest.kt
class TokenDataProviderTest {
@Test
fun `cast issue`() {
val result = TokenDataProvider()
.getToken()
.blockingFirst()
result.fold({
assertEquals(it, "token")
}, {
@amatkivskiy
amatkivskiy / TokenDataProvider.kt
Last active Nov 25, 2018
medium_TokenDataProvider
View TokenDataProvider.kt
class TokenDataProvider {
fun getToken(): Observable<Result<String>> {
// Just return sample string
return Observable.just("token")
.toResult()
}
}
/**
* Just an handful extension function which wraps any value or error produced by [Observable] into [Result].
View circle-ci-build.sh
#!/bin/bash
if [[ -z "${CI_PULL_REQUEST}" ]]; then
echo "---> Making build outside of Pull Request (building single commit or branch)"
./gradlew gnagCheck
else
echo "---> Making build for Pull Request"
# In the env variable CI_PULL_REQUEST CircleCI provides the URL of the PR (like https://github.com/amatkivskiy/sample/pull/3)
# But for the Gnag task we need PR number (simply 3)
# ${CI_PULL_REQUEST##*/} means that we simply get '3' from the URL provided in CI_PULL_REQUEST/
@amatkivskiy
amatkivskiy / maping-teamplate.txt
Created Jun 19, 2017
Slack incoming body mapping template (for AWS Gateway)
View maping-teamplate.txt
## convert HTML POST data or HTTP GET query string to JSON
## get the raw post data from the AWS built-in variable and give it a nicer name
#if ($context.httpMethod == "POST")
#set($rawAPIData = $input.path('$'))
## escape any quotes
#set($rawAPIData = $rawAPIData.replace('"', '\"'))
#elseif ($context.httpMethod == "GET")
#set($rawAPIData = $input.params().querystring)
#set($rawAPIData = $rawAPIData.toString())