profile
viewpoint
Zac Sweers ZacSweers @slackhq New York https://zacsweers.dev Mobile @ Slack

uber/AutoDispose 3022

Automatic binding+disposal of RxJava streams.

square/kotlinpoet 2530

A Kotlin API for generating .kt source files.

androidx/androidx 2256

Development environment for Jetpack Android extension libraries. Synchronized with Jetpack's primary development branch on AOSP.

rharter/auto-value-parcel 657

An Android Parcelable extension for Google's AutoValue.

uber/RxDogTag 590

Automatic tagging of RxJava 2+ originating subscribe points for onError() investigation.

rharter/auto-value-gson 584

AutoValue Extension to add Gson De/Serializer support

vanniktech/gradle-maven-publish-plugin 329

Gradle plugin that configures an uploadArchives task to automatically upload all of your Java, Kotlin or Android libraries to any Maven instance.

slackhq/EitherNet 248

A pluggable sealed API result type for modeling Retrofit responses.

rharter/auto-value-moshi 186

AutoValue: Moshi Extension

slackhq/keeper 128

A Gradle plugin that infers Proguard/R8 keep rules for androidTest sources.

issue openedsquare/moshi

How to parse fields with the same key but different values

This is driving me crazy. I spent the whole day and I still cannot figure out the issue I'm facing.

Our backend sends a json response that can contain different values for the same key every time.

Example of First variation:

{
  "title": "2",
  "profile_image": {
    "profile_image_id": "4581542",
    "sized": "media/up/2020/48/460e689e286ca46b1006b62269ee21a22b6bdabf2496506e34e5d07c5d42c20f_user_photo_160674563705555.sized.JPG",
    "sized_w": "556",
    "sized_h": "555",
    "thumb": "media/up/2020/48/460e689e286ca46b1006b62269ee21a22b6bdabf2496506e34e5d07c5d42c20f_user_photo_160674563705555.thumb.JPG",
    "thumb_w": "90",
    "thumb_h": "90"
  },
  "profile": {
    "title": "2",
    "first_name": "John",
    "last_name": "Doe",
    "user_online_status": false
  }
}

And below is an another variation of the same response. Notice the profile_image field is now an Array type and user_online_status can also be a string or a boolean.

{
  "title": "2",
  "profile_image": [
    
  ],
  "profile": {
    "title": "2",
    "first_name": "John",
    "last_name": "Doe",
    "user_online_status": "0"
  }
}

How can I parse such responses using Retrofit and Moshi without creating extra model classes? Can I use a custom adapter Maybe? Could you give me an example? I tried to make an adapter of my own but didn't managed to get far

class SkipEmptyProfileAdapter {
    

    @FromJson
    fun fromJson(reader: JsonReader): UserProfileDataResponse? {
        val moshi = Moshi.Builder().build()

        reader.beginObject()
        while (reader.hasNext()) {
            if (reader.nextName() == "profile_image") {
                Timber.i("PROFILE IMAGE")
                val pls = reader.peek()
                if (pls == JsonReader.Token.BEGIN_ARRAY)
                    return null
            }
        }
        reader.endObject()
        return null
    }

}

Can you help me out?

created time in 6 minutes

issue closedsquare/kotlinpoet

How to judge whether the parameter of annotation is nullable?

val isNullable = parameterElement.asType().asTypeName().isNullable

When I do, it always returns null

closed time in 33 minutes

plumcookingwine

issue openedsquare/kotlinpoet

How to judge whether the parameter of annotation is nullable?

val isNullable = parameterElement.asType().asTypeName().isNullable

When I do, it always returns null

created time in 40 minutes

startedstevemk14ebr/PolyHook_2_0

started time in 3 hours

startedZacSweers/blackmirror

started time in 3 hours

created repositoryJakeWharton/dockerfile-shebang

Treat your Dockerfiles as self-contained, editable scripts

created time in 4 hours

fork geota/domain

A DNS library for Rust.

fork in 5 hours

fork geota/domain-core

A DNS library for Rust.

fork in 5 hours

issue commentsquare/moshi

Expected BEGIN_OBJECT but was BEGIN_ARRAY

@ZacSweers please help!

franquicidad

comment created time in 6 hours

startedhelmetjs/helmet

started time in 7 hours

startedAnonymousPlanet/thgtoa

started time in 7 hours

startededgar-zigis/CoroutineRecipes

started time in 7 hours

startedsriram0339/csci3155_notebooks

started time in 8 hours

startedruffle-rs/ruffle

started time in 9 hours

push eventsquare/kotlinpoet

Tomasz Krakowiak

commit sha a61e88f6f3e050d601145f88ca149299f1423b14

#1016 Delegated constructor call in external class is not allowed

view details

Tomasz Krakowiak

commit sha 27835f26081b1e6c540d2c612057f86d9e2bddb2

#1018 Expect classes cannot initialize supertypes

view details

Egor Andreevich

commit sha f028a95776d0b58eda5b2492d157b8d39c88a939

Merge pull request #1017 from NiematojakTomasz/issue-1016 #1016 Delegated constructor call in external class is not allowed

view details

push time in 11 hours

issue closedsquare/kotlinpoet

Expect classes cannot initialize supertypes

Sample incorrectly generated code:

public expect open abstract class AbortSignal : EventTarget() {
  public open val aborted: Boolean?
}

Error: "Expected classes cannot initialize supertypes"

closed time in 11 hours

NiematojakTomasz

issue closedsquare/kotlinpoet

Delegated constructor call in external class is not allowed

Sample of invalid generated code:

public open abstract external class AbortSignal : EventTarget() {
  public val aborted: Boolean?
}

closed time in 11 hours

NiematojakTomasz

startedZacSweers/FB_Mod_Bot

started time in 13 hours

startedpsycopg/psycopg3

started time in 17 hours

Pull request review commentsquare/kotlinpoet

#1016 Delegated constructor call in external class is not allowed

+package com.squareup.kotlinpoet++import com.google.common.truth.Truth.assertThat+import kotlin.test.Test++class DelegatedConstructorCallTest {+  @Test+  fun defaultPresentInClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public class Test : testpackage.TestSuper()+        |""".trimMargin()+    )+  }++  @Test+  fun defaultPresentInObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public object Test : testpackage.TestSuper()+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExternalClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.addModifiers(KModifier.EXTERNAL)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public external class Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExpectClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.addModifiers(KModifier.EXPECT)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public expect class Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExpectObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.addModifiers(KModifier.EXPECT)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public expect object Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExternalObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.addModifiers(KModifier.EXTERNAL)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public external object Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun allowedInClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    builder.addSuperclassConstructorParameter("anything")+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public class Test : testpackage.TestSuper(anything)+        |""".trimMargin()+    )+  }++  @Test+  fun allowedInObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    builder.addSuperclassConstructorParameter("anything")+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public object Test : testpackage.TestSuper(anything)+        |""".trimMargin()+    )+  }++  @Test+  fun allowedInClassSecondary() {+    val builder = TypeSpec.classBuilder("Test")+    val primaryConstructorBuilder = FunSpec.constructorBuilder()+    val primaryConstructor = primaryConstructorBuilder.build()+    builder.primaryConstructor(primaryConstructor)+    val secondaryConstructorBuilder = FunSpec.constructorBuilder()+    secondaryConstructorBuilder.addParameter(ParameterSpec("foo", ClassName("kotlin", "String")))+    secondaryConstructorBuilder.callThisConstructor()+    builder.addFunction(secondaryConstructorBuilder.build())+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public class Test {+        |  public constructor(foo: kotlin.String) : this()+        |}+        |""".trimMargin()+    )+  }++  @Test+  fun notAllowedInExternalClass() {+    val builder = TypeSpec.classBuilder("Test")

@Egorand Done. CLA also signed.

NiematojakTomasz

comment created time in 19 hours

startedSupereg/secure-video-specification

started time in a day

startedACINQ/bitcoin-kmp

started time in a day

startedZacSweers/CatchUp

started time in a day

push eventZacSweers/ZacSweers

README-bot

commit sha 300baadbb48f1acf68e4dd49eea5d8b77f0d7958

Updated content

view details

push time in a day

Pull request review commentsquare/kotlinpoet

#1016 Delegated constructor call in external class is not allowed

+package com.squareup.kotlinpoet++import com.google.common.truth.Truth.assertThat+import kotlin.test.Test++class DelegatedConstructorCallTest {+  @Test+  fun defaultPresentInClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public class Test : testpackage.TestSuper()+        |""".trimMargin()+    )+  }++  @Test+  fun defaultPresentInObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public object Test : testpackage.TestSuper()+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExternalClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.addModifiers(KModifier.EXTERNAL)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public external class Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExpectClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.addModifiers(KModifier.EXPECT)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public expect class Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExpectObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.addModifiers(KModifier.EXPECT)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public expect object Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun defaultNotPresentInExternalObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.addModifiers(KModifier.EXTERNAL)+    builder.superclass(ClassName("testpackage", "TestSuper"))+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public external object Test : testpackage.TestSuper+        |""".trimMargin()+    )+  }++  @Test+  fun allowedInClass() {+    val builder = TypeSpec.classBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    builder.addSuperclassConstructorParameter("anything")+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public class Test : testpackage.TestSuper(anything)+        |""".trimMargin()+    )+  }++  @Test+  fun allowedInObject() {+    val builder = TypeSpec.objectBuilder("Test")+    builder.superclass(ClassName("testpackage", "TestSuper"))+    builder.addSuperclassConstructorParameter("anything")+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public object Test : testpackage.TestSuper(anything)+        |""".trimMargin()+    )+  }++  @Test+  fun allowedInClassSecondary() {+    val builder = TypeSpec.classBuilder("Test")+    val primaryConstructorBuilder = FunSpec.constructorBuilder()+    val primaryConstructor = primaryConstructorBuilder.build()+    builder.primaryConstructor(primaryConstructor)+    val secondaryConstructorBuilder = FunSpec.constructorBuilder()+    secondaryConstructorBuilder.addParameter(ParameterSpec("foo", ClassName("kotlin", "String")))+    secondaryConstructorBuilder.callThisConstructor()+    builder.addFunction(secondaryConstructorBuilder.build())+    assertThat(builder.build().toString()).isEqualTo(+      """+        |public class Test {+        |  public constructor(foo: kotlin.String) : this()+        |}+        |""".trimMargin()+    )+  }++  @Test+  fun notAllowedInExternalClass() {+    val builder = TypeSpec.classBuilder("Test")

nit: the code could look a bit less verbose if you used method chaining:

val builder = TypeSpec.classBuilder("Test")
  .addModifiers(KModifier.EXTERNAL)
  .superclass(ClassName("testpackage", "TestSuper"))
  .addSuperclassConstructorParameter("anything")

As is is fine though, so not critical to fix.

NiematojakTomasz

comment created time in a day

push eventZacSweers/ZacSweers

README-bot

commit sha 031f7be5ba103ad35207691e29a235cefa79d4a4

Updated content

view details

push time in a day

issue commentsquare/moshi

Expected BEGIN_OBJECT but was BEGIN_ARRAY

@ZacSweers Hello Zac i cant make questions in stack overflow because I didn't know how to edit them the way they wanted i am relatively new in programming and didnt know i could get banned.

franquicidad

comment created time in a day

issue commentZacSweers/redacted-compiler-plugin

Redact The Parameter Name

R8 does not have this functionality hence my need for something like this. No problem, it is quite a specific use case in my project.

StuStirling

comment created time in a day

more