Skip to content

Commit

Permalink
Sample. Also make pom.xml more uniform.
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Sep 19, 2015
1 parent f61aa88 commit af22aa8
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 9 deletions.
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
<packaging>pom</packaging>
<version>2.0.0-SNAPSHOT</version>

<name>Wire Protocol Buffers</name>
<description>Parent module for Wire protocol buffers</description>
<inceptionYear>2013</inceptionYear>

<scm>
Expand Down Expand Up @@ -44,11 +42,12 @@
</licenses>

<modules>
<module>sample</module>
<module>wire-compiler</module>
<module>wire-java-generator</module>
<module>wire-runtime</module>
<module>wire-gson-support</module>
<module>wire-java-generator</module>
<module>wire-maven-plugin</module>
<module>wire-runtime</module>
<module>wire-schema</module>
</modules>

Expand Down
48 changes: 48 additions & 0 deletions sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup.wire</groupId>
<artifactId>wire</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>wire-sample</artifactId>

<dependencies>
<dependency>
<groupId>com.squareup.wire</groupId>
<artifactId>wire-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.squareup.wire</groupId>
<artifactId>wire-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate-sources</goal>
</goals>
<configuration>
<protoFiles>
<param>squareup/dinosaurs/dinosaur.proto</param>
<param>squareup/geology/period.proto</param>
</protoFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
54 changes: 54 additions & 0 deletions sample/src/main/java/com/squareup/dinosaurs/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2015 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.dinosaurs;

import com.squareup.geology.Period;
import java.io.IOException;
import java.util.Arrays;
import okio.ByteString;

public final class Sample {
public void run() throws IOException {
// Create an immutable value object with the Builder API.
Dinosaur stegosaurus = new Dinosaur.Builder()
.name("Stegosaurus")
.period(Period.JURASSIC)
.length_meters(9.0)
.mass_kilograms(5_000.0)
.picture_urls(Arrays.asList("http://goo.gl/LD5KY5", "http://goo.gl/VYRM67"))
.build();

// Encode that value to bytes, and print that as base64.
byte[] stegosaurusEncoded = Dinosaur.ADAPTER.encode(stegosaurus);
System.out.println(ByteString.of(stegosaurusEncoded).base64());

// Decode base64 bytes, and decode those bytes as a dinosaur.
ByteString tyrannosaurusEncoded = ByteString.decodeBase64("Cg1UeXJhbm5vc2F1cnVzEmhodHRwOi8vdmln"
+ "bmV0dGUxLndpa2lhLm5vY29va2llLm5ldC9qdXJhc3NpY3BhcmsvaW1hZ2VzLzYvNmEvTGVnbzUuanBnL3Jldmlz"
+ "aW9uL2xhdGVzdD9jYj0yMDE1MDMxOTAxMTIyMRJtaHR0cDovL3ZpZ25ldHRlMy53aWtpYS5ub2Nvb2tpZS5uZXQv"
+ "anVyYXNzaWNwYXJrL2ltYWdlcy81LzUwL1JleHlfcHJlcGFyaW5nX2Zvcl9iYXR0bGVfd2l0aF9JbmRvbWludXNf"
+ "cmV4LmpwZxmamZmZmZkoQCEAAAAAAJC6QCgB");
Dinosaur tyrannosaurus = Dinosaur.ADAPTER.decode(tyrannosaurusEncoded.toByteArray());

// Print both of our dinosaurs.
System.out.println(stegosaurus.name + " is " + stegosaurus.length_meters + " meters long!");
System.out.println(tyrannosaurus.name + " weighs " + tyrannosaurus.mass_kilograms + " kilos!");
}

public static void main(String[] args) throws IOException {
new Sample().run();
}
}
19 changes: 19 additions & 0 deletions sample/src/main/proto/squareup/dinosaurs/dinosaur.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto2";

package squareup.dinosaurs;

option java_package = "com.squareup.dinosaurs";

import "squareup/geology/period.proto";

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

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

optional double length_meters = 3;
optional double mass_kilograms = 4;
optional squareup.geology.Period period = 5;
}
16 changes: 16 additions & 0 deletions sample/src/main/proto/squareup/geology/period.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto2";

package squareup.geology;

option java_package = "com.squareup.geology";

enum Period {
/** 145.5 million years ago — 66.0 million years ago. */
CRETACEOUS = 1;

/** 201.3 million years ago — 145.0 million years ago. */
JURASSIC = 2;

/** 252.17 million years ago — 201.3 million years ago. */
TRIASSIC = 3;
}
1 change: 0 additions & 1 deletion wire-compiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</parent>

<artifactId>wire-compiler</artifactId>
<name>Wire Protocol Buffer Compiler</name>

<dependencies>
<dependency>
Expand Down
1 change: 0 additions & 1 deletion wire-gson-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</parent>

<artifactId>wire-gson-support</artifactId>
<name>Wire Protocol Buffer Gson Support</name>

<dependencies>
<dependency>
Expand Down
1 change: 0 additions & 1 deletion wire-java-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</parent>

<artifactId>wire-java-generator</artifactId>
<name>Wire Protocol Buffer Java Generator</name>

<dependencies>
<dependency>
Expand Down
1 change: 0 additions & 1 deletion wire-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</parent>

<artifactId>wire-maven-plugin</artifactId>
<name>Wire Protocol Buffer Maven Plugin</name>
<packaging>maven-plugin</packaging>

<dependencies>
Expand Down
1 change: 0 additions & 1 deletion wire-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</parent>

<artifactId>wire-runtime</artifactId>
<name>Wire Protocol Buffer Runtime</name>

<dependencies>
<dependency>
Expand Down

0 comments on commit af22aa8

Please sign in to comment.