Giter VIP home page Giter VIP logo

protogen's Introduction

Code generator for protobuf

  • Generates RealmObject classes that creates from Proto models and transforms into Proto models
  • Generates Kotlin extension functions than puts Proto models into ReactNative WritableMap and creates Proto model from ReadableMap

Kotlin support only now.

Example proto model:

syntax = "proto2";

package models;

option java_package = "com.company.project.models";


message User {
    required int64 id = 1;
    required string firstName = 2;
    required string lastName = 3;
    optional string middleName = 4;
    
    repeated User contacts = 5;
}

Protogen will create Realm model:

open class RealmUser: RealmObject {

    var id: Long = 0
    
    var firstName: String = ""
    
    var lastName: String = ""
    
    var middleName: String? = null
    
    var contacts: RealmList<com.company.project.models.RealmUser>? = null
    
    constructor()
    
    constructor(protoModel: com.company.project.models.User) {
        id = protoModel.id
        firstName = protoModel.firstName
        lastName = protoModel.lastName
        if (protoModel.hasMiddleName()) {
            middleName = protoModel.middleName
        }
        if (protoModel.contactsCount > 0) {
            contacts = io.realm.RealmList()
            contacts!!.addAll(protoModel.contactsList.map { com.company.project.models.RealmUser(it) })
        }
    }
    
    fun toProto(): com.company.project.models.User {
        val p = com.company.project.models.User.newBuilder()
        p.id = id
        p.firstName = firstName
        p.lastName = lastName
        middleName?.let { p.middleName = it }
        contacts?.let { p.addAllContacts(it.map { it.toProto() }) }
        return p.build()
    }
}

For ReactNative protogen will create two extension functions:

fun com.company.project.models.User.toWritableMap(): com.facebook.react.bridge.WritableMap =
      com.facebook.react.bridge.Arguments.createMap().apply {
      	putString("id", id)
      	putString("firstName", firstName)
      	putString("lastName", lastName)
      	if (hasMiddleName())
      	    putBoolean("middleName", middleName)
      	if (contactsList.isNotEmpty()) {
        	val contactsArray = com.facebook.react.bridge.Arguments.createArray()
        	contactsList.forEach { contactsArray.pushString(it) }
        	putArray("contacts", contactsArray)
        }
      }


fun com.company.project.models.User.Builder.fromReadableMap(map: com.facebook.react.bridge.ReadableMap): com.company.project.models.User {
    id = map.getString("id").toLong()
    firstName = map.getString("firstName")
    lastName = map.getString("lastName")
    if (map.hasKey("middleName")) 
          middleName = map.getString("middleName")
    if (map.hasKey("contacts")) {
    	val contactsArray = map.getArray("contacts")
    	for (i in 0 until contactsArray.size()) {
    		val element = com.company.project.models.User.newBuilder().fromReadableMap(contactsArray.getMap(i))
    		addContactsArray(element)
    	}
    }
    return build()
}

protogen's People

Contributors

bagrusss avatar

Watchers

 avatar  avatar

protogen's Issues

Retrofit code generation support

Code generation that creates retrofit interfaces like this one:

interface ApiService {

    @Get("api/v1/gift")
    fun giftDetails(GiftDetails.Request): Single<GiftDetails.Response>

}

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.