All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gesellix.docker.compose.adapters.StringToServiceNetworksAdapter.kt Maven / Gradle / Ivy

package de.gesellix.docker.compose.adapters

import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonReader
import com.squareup.moshi.ToJson
import de.gesellix.docker.compose.types.ServiceNetwork

class StringToServiceNetworksAdapter {

    @ToJson
    fun toJson(@ServiceNetworksType networks: Map): List {
        throw  UnsupportedOperationException()
    }

    @FromJson
    @ServiceNetworksType
    fun fromJson(reader: JsonReader): Map {
        val result = hashMapOf()
        when (reader.peek()) {
            JsonReader.Token.BEGIN_OBJECT -> {
                reader.beginObject()
                while (reader.hasNext()) {
                    val name = reader.nextName()
                    when (reader.peek()) {
                        JsonReader.Token.NULL -> result[name] = reader.nextNull()
                        JsonReader.Token.STRING -> throw UnsupportedOperationException("didn't expect a String value for network $name")

                        //                    result[name] = reader.nextString()
                        JsonReader.Token.BEGIN_OBJECT -> {
                            val serviceNetwork = ServiceNetwork()
                            reader.beginObject()
                            while (reader.hasNext()) {
                                when (reader.nextName()) {
                                    "ipv4_address" -> serviceNetwork.ipv4Address = reader.nextString()
                                    "ipv6_address" -> serviceNetwork.ipv6Address = reader.nextString()
                                    "aliases" -> {
                                        val aliases = arrayListOf()
                                        reader.beginArray()
                                        while (reader.hasNext()) {
                                            aliases.add(reader.nextString())
                                        }
                                        reader.endArray()
                                        serviceNetwork.aliases = aliases
                                    }
                                    else -> {
                                        // ...
                                    }
                                }
                            }
                            reader.endObject()
                            result[name] = serviceNetwork
                        }
                        else -> {
                            // ...
                        }
                    }
                }
                reader.endObject()
            }
            JsonReader.Token.BEGIN_ARRAY -> {
                reader.beginArray()
                while (reader.hasNext()) {
                    val name = reader.nextString()
                    //            def value = reader.nextNull()
                    result[name] = null
                }
                reader.endArray()
            }
            else -> {
                // ...
            }
        }
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy