简体   繁体   English

编译时间检查,同时将值传递给 Kotlin Android 中的 function

[英]Compile time check while passing values to a function in Kotlin Android

I am taking a JSON file as input for a class and parsing the values using gson through respective data classes.我将JSON文件作为 class 的输入,并通过相应的数据类使用 gson 解析值。

I want to call a function that takes a String value as an argument.我想调用一个将String值作为参数的 function。

The string value allowed is decided from the values parsed from JSON file.允许的字符串值由从JSON文件解析的值决定。 Can I somehow check for that string value passed to the function at compile-time & give an error at compile-time?我可以在编译时检查传递给 function 的字符串值并在编译时给出错误吗?

Or If I can allow only certain values in the argument for the function based on the values from JSON或者如果我只能根据 JSON 的值在 function 的参数中允许某些值

Detailed Explanation of use case:用例详解:

I am building a SDK in which a the person using sdk inputs json String.我正在构建一个 SDK,其中使用 sdk 的人输入 json 字符串。 The json is standardised and is parsed in my code. json 是标准化的,并在我的代码中进行了解析。

{
    "name": "Test",
    "objects": [
        {
            "name": "object1",
            "type": "object1"
         }
      ]
}

Here name values and other values may vary based on the input by the developer using it but key remains same.这里的名称值和其他值可能会根据开发人员使用它的输入而有所不同,但密钥保持不变。 But we need to call a function using the value in objects name parameter.但是我们需要使用对象名称参数中的值调用 function。

fun testMethod(objectName:String)

So developer calls the testMethod as testMethod(object1) .因此开发人员将 testMethod 称为testMethod(object1)

I need to validate object1 parameter based on json but is there any way possible restricting the test method parameter to object1 only & give error at compile time if the developer calls testMethod(obj1)我需要根据 json 验证object1参数,但是如果开发人员调用testMethod(obj1) ,是否可以将测试方法参数仅限制为object1并在编译时给出错误

Right now I parse JSON & have checks inside the testMethod()现在我解析 JSON 并检查testMethod()

Sure it's possible to do, but somehow in different way, that you described.当然可以做到,但是以您所描述的方式不同。 First of all, as you already mentioned this behavior could be done easily.首先,正如您已经提到的,这种行为可以很容易地完成。 For this purpose we have Objects.requireNotNull() or Guava.Preconditions() .为此,我们有Objects.requireNotNull()Guava.Preconditions() At the same way you can define you checking but this will work on runtime only.同样,您可以定义您的检查,但这仅适用于运行时。

To do in compile time, you need to create Annotation Preprocessor .要在编译时执行,您需要创建 Annotation Preprocessor The same, as did in different libraries, and one of them, could be Lombok , with their NotNull and Nullable .与不同库中的相同,其中之一可能是Lombok ,具有NotNullNullable Android annotation just provide mark and bound for IDE warning, but in their case they adding NotNull checking and throw exception for every annotation usage during compile time. Android 注释只是为 IDE 警告提供标记和界限,但在他们的情况下,他们添加了NotNull检查并在编译期间为每个注释使用抛出异常。

It's not an easy way, but it's what you are looking for.这不是一个简单的方法,但它是你正在寻找的。

No, it's impossible check it in compiler time.不,不可能在编译器时检查它。 It's string handling, as numeric calculation.它是字符串处理,作为数字计算。

In my app, I convert string to JSON and JSON to string, passing class descriptor.在我的应用程序中,我将字符串转换为 JSON 和 JSON 到字符串,传递 class 描述符。 My aim is record JSON string in a text file to load in SQLite database.我的目标是在文本文件中记录 JSON 字符串以加载到 SQLite 数据库中。 This code I've run in my desktop computer not in Android .我在台式计算机上运行的这段代码不在Android中。

data class calcDescr (  
   ...
)
val calc = CalcDescr(...)

 //    toJson:  internal Kotlin data to JSON
val content = Gson().toJson(calc)   

  //==================
  // Testing validity
  // ================

 //    fromJson: JSON to internal Kotlin data. 
 // It needs pass the class descriptor. Uses *Java* token, but it's *Kotlin*
var testModel = Gson().fromJson(content, CalcDescr::class.java) 
 //    toJson:  internal Kotlin data to JSON again
var contentAgain = Gson().toJson(testModel)
//    shoul be equal!
if (content == contentAgain)  println("***ok***")

In my file, I write the variable content in a file在我的文件中,我将变量content写入文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM