Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

fangzhzh/wire

Repository files navigation

Garena Wire Protocol Buffers

forked from square wire.

Tasks after protocol buffers

After protocol buffer parsed, android applications still got a lot to do. Typically there are three tasks:

  • mapping protocol buffer object to db object for OrmLite.
  • supporting database CRUD operations, taking care of the db structure change if necessary.
  • mapping database object to view model in a MVP model

How to Build Gerena wire

$ cd $WIREHOME
$ mvn clean package

How to Build MVP classed

java  -jar $WIREHOME/garena-wire-compiler-2.0.0-SNAPSHOT-jar-with-dependencies.jar --proto_path=$WIREHOME/protos-repo --java_out=$WIREHOME/out  beeshop_cmd.proto

this commend will read beeshop_cmd.proto in dir $WIREHOME/protos-repo, generate database class, dao class and view model class in dir $WIREHOME/out.

Example

Let's say we have a protocol file

package dinosaur.cmd;
option java_package = "com.dinosaur.app.database.orm.bean";

message Dinosaur {
  // Common name of this dinosaur, like "Stegosaurus".
  optional string name = 1;

  // URLs with images of this dinosaur.
  repeated string picture_urls = 2;

}

then we will generate db file DBDinosaur.java

package com.dinosaur.app.database.orm.bean;


import com.squareup.wire.DatabaseField;
import com.squareup.wire.DatabaseTable;
import java.lang.String;
import java.util.List;


@DatabaseTable(
    tableName = "sp_Dinosaur"
)
public class DBDinosaur {
  /**
   * Common name of this dinosaur, like "Stegosaurus".
   */
  @DatabaseField(
      columnName = COLUMN.NAME
  )
  private String name;


  /**
   * URLs with images of this dinosaur.
   */
  @DatabaseField(
      columnName = COLUMN.PICTURE_URLS
  )
  private List<String> pictureUrls;


  public DBDinosaur() {
  }


  public final String getName() {
    return this.name;
  }


  public void setName(String name) {
    this.name = name;
  }


  public final List<String> getPictureUrls() {
    return this.pictureUrls;
  }


  public void setPictureUrls(List<String> picture_urls) {
    this.pictureUrls = picture_urls;
  }


  public static String createname() {
    return "alter table sp_Dinosaur ADD name VARCHAR;";
  }


  public static String createpictureUrls() {
    return "alter table sp_Dinosaur ADD pictureUrls VARCHAR;";
  }


  public static void map(Dinosaur protoObject, DBDinosaur dbObject) {
    dbObject.setName(DataMapper.getString(name));
    dbObject.setPictureUrls(DataMapper.getString(picture_urls));
  }


  public interface COLUMN {
    String NAME = "name";
  }
}

Dao file : DinosaurDao.java

DinosaurDao.java
// Code generated by Wire protocol buffer compiler, do not edit.
// Source file: /Users/zhenfangzhang/workspace/wire/protos-repo/beeshop_cmd.proto at 4:1
package com.dinosaur.app.database.orm.bean;


import DBDinosaur;
import DatabaseHelper;
import com.squareup.wire.BaseInfoDao;
import java.lang.Object;
import java.util.List;


public class DinosaurDao extends BaseInfoDao<DBDinosaur, Object> {
  public DinosaurDao(DatabaseHelper helper) {
    super(helper, DBDinosaur.class);
  }


  public DBDinosaur get(Object id) {
    try {
      final Dao<DBDinosaur, java.lang.Object> dao = getDao();
      return dao.queryForId(id);
    }
    catch(Exception e) {
      BBAppLogger.e(e);
    }
    return null;
  }


  public List<DBDinosaur> getList(List<Object> idList) {
    try {
      final Dao<DBDinosaur, java.lang.Object> dao = getDao();
      final QueryBuilder<DBDinosaur, java.lang.Object> builder = dao.queryBuilder();
      builder.where().in(DBDinosaur.COLUMN.PRIMARY_KEY, idList);
    }
    catch(Exception e) {
      BBAppLogger.e(e);
    }
    return new ArrayList<>();;
  }


  public void save(final DBDinosaur dbObject) {
    try {
      final Dao<DBDinosaur, java.lang.Object> dao = getDao();
      dao.createOrUpdate(dbObject);;
    }
    catch(Exception e) {
      BBAppLogger.e(e);
    }
  }


  public void save(final List<DBDinosaur> dbObjects) {
    if ( dbObjects.size() <= 0) {
      return;
    }
    try {
      final Dao<DBDinosaur, java.lang.Object> dao = getDao();
      dao.callBatchTasks(new Callable<Object>() {
      	@Override
      	public Object call() throws Exception  {
        	for (DBDinosaur dbObject : dbObjects)  {
          	dao.createOrUpdate(dbObject);
        }
        	return null;
      }
    } );
    } catch(Exception e) {
      BBAppLogger.e(e);
    }
  }
}

View Model file: VMDinosaur.java

VMDinosaur.java
// Code generated by Wire protocol buffer compiler, do not edit.
// Source file: /Users/zhenfangzhang/workspace/wire/protos-repo/beeshop_cmd.proto at 4:1
package com.dinosaur.app.database.orm.bean;


import java.lang.String;
import java.util.List;


public class VMDinosaur {
  private String name;


  private List<String> pictureUrls;


  public VMDinosaur() {
  }


  public final String getName() {
    return this.name;
  }


  public void setName(String name) {
    this.name = name;
  }


  public final List<String> getPictureUrls() {
    return this.pictureUrls;
  }


  public void setPictureUrls(List<String> picture_urls) {
    this.pictureUrls = picture_urls;
  }
}