GithubHelp home page GithubHelp logo

xavijimenezmulet / savenullextensionkotlin Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 1.0 156 KB

This library is used to control an attribute if it comes to null, what it will do is remove that null and give it a default value. Ideal for dealing with business models where attributes can be nullable.

License: Apache License 2.0

Kotlin 100.00%

savenullextensionkotlin's Introduction

SAVE NULL EXTENSION

Download API Build Status Language ktlint Open Source Love License

ko-fi

This library is used to control an attribute if it comes to null, what it will do is remove that null and give it a default value. Ideal for dealing with business models where attributes can be nullable.

💥Installation

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency:

dependencies {
	implementation "com.github.xavijimenezmulet:SaveNullExtensionKotlin:$last_version"
}

💻Usage

If you have a class where null attributes can come. The library helps prevent them in a single line without having to check them. Let's add an example class:

data class Example(
    val byte: Byte?,
    val short: Short?,
    val int: Int?,
    val long: Long?,
    val float: Float?,
    val double: Double?,
    val char: Char?,
    val string: String?,
    val boolean: Boolean?,
    val list: List<String>?,
    val mutableList: MutableList<String>?,
    val array: Array<String>?
)

Now we are going to show an example of an extension, for example of string:

fun String?.saveNull(defaultValue: String = ""): String {
    return this ?: defaultValue
}

If we look at the extension it controls that a string can be nullable. If the string is not nullable, it returns the attribute itself. But on the contrary, if it is nullable, it will return the default value (By default it will be empty string). All extensions have a default value that we can modify to our liking if we want.

Now let's show an example with the "Example" class:

private fun initExample() {
  val example = Example()
  val text: String = example.string.saveNull("No available")
  textView.text = text
}

The value of "text" will always have a value. It can be the string attribute of the Example class if it is not null. If it is, we have added a default value that says "not available" so the textView will always show a result.

✔️Changelog

Version: 1.0

  • Initial Build

License

APACHE LICENSE 2.0

savenullextensionkotlin's People

Contributors

xavijimenezmulet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

julienishh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.