Skip to content

Commit

Permalink
Kt remove nullability (PolymerLabs#4278)
Browse files Browse the repository at this point in the history
* Remove nullable entity fields in kt. Update testparticle.

* Fix Tutorial 6

* Fix lint issue with TestParticle

* Fix ttt pt1

* fix README and lint with TTT pt1

* Fix TTT pt2.

* Update TTT total and Tutorial 4

* Fix up reset in TTT.

* Fix SpecialSchemaFieldsTest

* Fix SingletonApiTest

* Fix ServicesTest

* Fix RenderTest

* Fix HandleSyncUpdateTest

* Fix EventsTest

* Fix EntityClassApiTest.kt

* Fix CollectionApiTest

* Get all tests passing

* Remove null checking in tests.
  • Loading branch information
SHeimlich authored Dec 13, 2019
1 parent 335b1f0 commit fb06d42
Show file tree
Hide file tree
Showing 27 changed files with 572 additions and 246 deletions.
44 changes: 34 additions & 10 deletions particles/Native/Wasm/source/TestParticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package arcs.test

import arcs.addressable.toAddress
import arcs.Collection
import arcs.Handle
import arcs.Particle
Expand All @@ -18,10 +17,11 @@ import arcs.TestParticle_Data
import arcs.TestParticle_Info
import arcs.TestParticle_Res
import arcs.abort
import arcs.addressable.toAddress
import arcs.log
import kotlin.Exception
import kotlin.native.internal.ExportForCppRuntime
import kotlin.native.Retain
import kotlin.native.internal.ExportForCppRuntime

/**
* Sample WASM Particle.
Expand Down Expand Up @@ -107,26 +107,50 @@ class TestParticle : Particle() {
</table>""".trimIndent()
}

private val data = Singleton(this, "data") { TestParticle_Data() }
private val res = Singleton(this, "res") { TestParticle_Res() }
private val info = Collection(this, "info") { TestParticle_Info() }
private val defaultData = TestParticle_Data(
num = 0.0,
txt = "",
lnk = "",
flg = false
)
private val defaultRes = TestParticle_Res(
num = 0.0,
txt = "",
lnk = "",
flg = false
)
private val defaultInfo = TestParticle_Info(
for_ = "",
val_ = 0.0
)
private val data = Singleton(this, "data") { defaultData }
private val res = Singleton(this, "res") { defaultRes }
private val info = Collection(this, "info") { defaultInfo }
private var updated = 0
private var storeCount = 0

init {
eventHandler("add") {
val newData = data.get() ?: TestParticle_Data()
newData.num = newData.num?.let { it + 2 } ?: 0.0
newData.txt = "${newData.txt}!!!!!!"
this.data.set(newData)
val newData = data.get() ?: TestParticle_Data(
num = 0.0,
txt = "",
lnk = "",
flg = false
)
newData.num = newData.num.let { it + 2 }
newData.txt = "${newData.txt}!!!!!!"
this.data.set(newData)
}

eventHandler("dataclear") {
data.clear()
}

eventHandler("store") {
val info = TestParticle_Info()
val info = TestParticle_Info(
for_ = "",
val_ = 0.0
)
info.internalId = "wasm" + (++storeCount)
info.val_ = (this.info.size + storeCount).toDouble()
this.info.store(info)
Expand Down
6 changes: 3 additions & 3 deletions particles/Tutorial/Kotlin/4_Handles/DisplayGreeting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@

package arcs.tutorials

import arcs.addressable.toAddress
import arcs.Handle
import arcs.Particle
import arcs.Singleton
import kotlin.native.internal.ExportForCppRuntime
import arcs.addressable.toAddress
import kotlin.native.Retain
import kotlin.native.internal.ExportForCppRuntime

/**
* Sample WASM Particle.
*/
class DisplayGreetingParticle : Particle() {
private val person = Singleton(this, "person") { DisplayGreeting_Person() }
private val person = Singleton(this, "person") { DisplayGreeting_Person("Human") }

override fun getTemplate(slotName: String) = "Hello, <span>{{name}}</span>!"

Expand Down
8 changes: 4 additions & 4 deletions particles/Tutorial/Kotlin/4_Handles/GetPerson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@

package arcs.tutorials

import arcs.addressable.toAddress
import arcs.Particle
import arcs.Singleton
import kotlin.native.internal.ExportForCppRuntime
import arcs.addressable.toAddress
import kotlin.native.Retain
import kotlin.native.internal.ExportForCppRuntime

/**
* Sample WASM Particle.
*/
class GetPersonParticle : Particle() {
private val person = Singleton(this, "person") { GetPerson_Person() }
private val person = Singleton(this, "person") { GetPerson_Person("Human") }

override fun getTemplate(slotName: String) = """
<input placeholder="Enter your name" spellcheck="false" on-change="onNameInputChange">
<div slotid="greetingSlot"></div>""".trimIndent()

init {
eventHandler("onNameInputChange") { eventData ->
val p = person.get() ?: GetPerson_Person()
val p = person.get() ?: GetPerson_Person("Human")
p.name = eventData["value"] ?: "Human"
person.set(p)
}
Expand Down
9 changes: 6 additions & 3 deletions particles/Tutorial/Kotlin/5_Collections/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@

package arcs.tutorials

import arcs.addressable.toAddress
import arcs.Collection
import arcs.CollectionsParticle_InputData
import arcs.Particle
import kotlin.native.internal.ExportForCppRuntime
import arcs.addressable.toAddress
import kotlin.native.Retain
import kotlin.native.internal.ExportForCppRuntime

/**
* Sample Kotlin-WASM Particle to use a JSON store.
*/
class CollectionsParticle : Particle() {
private val people = Collection(this, "inputData") { CollectionsParticle_InputData() }
private val people = Collection(this, "inputData") { CollectionsParticle_InputData(
name = "",
age = 0.0
) }

override fun populateModel(slotName: String, model: Map<String, Any?>): Map<String, Any?> {
val peopleList = mutableListOf<Map<String, Comparable<*>?>>()
Expand Down
9 changes: 6 additions & 3 deletions particles/Tutorial/Kotlin/6_JsonStore/JsonStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@

package arcs.tutorials

import arcs.addressable.toAddress
import arcs.Particle
import arcs.Singleton
import kotlin.native.internal.ExportForCppRuntime
import arcs.addressable.toAddress
import kotlin.native.Retain
import kotlin.native.internal.ExportForCppRuntime

/**
* Sample Kotlin-WASM Particle to use a JSON store.
*/
class JsonStoreParticle : Particle() {

private val res = Singleton(this, "inputData") { JsonStoreParticle_InputData() }
private val res = Singleton(this, "inputData") { JsonStoreParticle_InputData(
name = "",
age = 0.0
) }

override fun populateModel(slotName: String, model: Map<String, Any?>): Map<String, Any?> {
val person = res.get() ?: JsonStoreParticle_InputData("", 0.0)
Expand Down
51 changes: 26 additions & 25 deletions particles/Tutorial/Kotlin/Demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,21 @@ import kotlin.native.Retain
import kotlin.native.internal.ExportForCppRuntime

class TTTGame : Particle() {
private val gameState = Singleton(this, "gameState") { TTTGame_GameState() }
private val events = Collection(this, "events") { TTTGame_Events() }

// We represent the board as a comma seperated string
private val defaultGame = TTTGame_GameState(board = ",,,,,,,,")

private val gameState = Singleton(this, "gameState") { defaultGame }
private val events = Collection(this, "events") { TTTGame_Events(
type = "",
move = -1.0,
time = -1.0
) }

override fun onHandleSync(handle: Handle, allSynced: Boolean) {
// If the gameState doesn't exist, set it.
if (gameState.get()?.board == null) {
if (gameState.get() == null) {
gameState.set(defaultGame)
}
}

// Provide the boardSlot
override fun getTemplate(slotName: String): String = """
<div slotid="boardSlot"></div>
""".trimIndent()
Expand Down Expand Up @@ -228,16 +229,22 @@ import kotlin.native.internal.ExportForCppRuntime

class TTTBoard : Particle() {

private val gameState = Singleton(this, "gameState") { TTTBoard_GameState() }
private val events = Collection(this, "events") { TTTBoard_Events() }
private val defaultGameState = TTTBoard_GameState(
board = ",,,,,,,,"
)

private val defaultEvent = TTTBoard_Events(
type = "",
move = -1.0,
time = -1.0
)

// We use clicks as the way to sort Events in other particles.
private val gameState = Singleton(this, "gameState") { defaultGameState }
private val events = Collection(this, "events") { defaultEvent }
private var clicks = 0.0
// The empty board will be used in multiple null checks.
private val emptyBoard = listOf("", "", "", "", "", "", "", "", "")

init {
// When a cell is clicked, add the click to the Events.
eventHandler("onClick") { eventData ->
events.store(TTTBoard_Events(
type = "move",
Expand All @@ -247,33 +254,27 @@ class TTTBoard : Particle() {
clicks++
}

// When the reset button is clicked, add it to the Events.
eventHandler("reset") {
events.store(TTTBoard_Events(type = "reset", time = clicks))
events.store(TTTBoard_Events(type = "reset", time = clicks, move = -1.0))
clicks++
}
}

// When a handle is updated, we want to update the board.
override fun onHandleUpdate(handle: Handle) = renderOutput()

override fun populateModel(slotName: String, model: Map<String, Any?>): Map<String, Any?> {
// We use template interpolation to easily create the board. To
// do this, we need to create a model of the board to return.
val boardList = gameState.get()?.board?.split(",") ?: emptyBoard
val gs = gameState.get() ?: defaultGameState
val boardList = gs.board.split(",")
val boardModel = mutableListOf<Map<String, String?>>()
boardList.forEachIndexed { index, cell ->
// Map what should be displayed in the cell and the index.
// This is what lets the onClick work as "value" becomes
// the move.
boardModel.add(mapOf("cell" to cell, "value" to index.toString()))
}

return mapOf(
"buttons" to mapOf(
"\$template" to "button",
"models" to boardModel
)
"buttons" to mapOf(
"\$template" to "button",
"models" to boardModel
)
)
}

Expand Down
27 changes: 19 additions & 8 deletions particles/Tutorial/Kotlin/Demo/src/pt1/TTTBoard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ import kotlin.native.internal.ExportForCppRuntime

class TTTBoard : Particle() {

private val gameState = Singleton(this, "gameState") { TTTBoard_GameState() }
private val events = Collection(this, "events") { TTTBoard_Events() }
private val defaultGameState = TTTBoard_GameState(
board = ",,,,,,,,"
)

private val defaultEvent = TTTBoard_Events(
type = "",
move = -1.0,
time = -1.0
)

private val gameState = Singleton(this, "gameState") { defaultGameState }
private val events = Collection(this, "events") { defaultEvent }
private var clicks = 0.0
private val emptyBoard = listOf("", "", "", "", "", "", "", "", "")

Expand All @@ -39,25 +49,26 @@ class TTTBoard : Particle() {
}

eventHandler("reset") {
events.store(TTTBoard_Events(type = "reset", time = clicks))
events.store(TTTBoard_Events(type = "reset", time = clicks, move = -1.0))
clicks++
}
}

override fun onHandleUpdate(handle: Handle) = renderOutput()

override fun populateModel(slotName: String, model: Map<String, Any?>): Map<String, Any?> {
val boardList = gameState.get()?.board?.split(",") ?: emptyBoard
val gs = gameState.get() ?: defaultGameState
val boardList = gs.board.split(",")
val boardModel = mutableListOf<Map<String, String?>>()
boardList.forEachIndexed { index, cell ->
boardModel.add(mapOf("cell" to cell, "value" to index.toString()))
}

return mapOf(
"buttons" to mapOf(
"\$template" to "button",
"models" to boardModel
)
"buttons" to mapOf(
"\$template" to "button",
"models" to boardModel
)
)
}

Expand Down
12 changes: 8 additions & 4 deletions particles/Tutorial/Kotlin/Demo/src/pt1/TTTGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ import kotlin.native.Retain
import kotlin.native.internal.ExportForCppRuntime

class TTTGame : Particle() {
private val gameState = Singleton(this, "gameState") { TTTGame_GameState() }
private val events = Collection(this, "events") { TTTGame_Events() }

private val defaultGame = TTTGame_GameState(board = ",,,,,,,,")

private val gameState = Singleton(this, "gameState") { defaultGame }
private val events = Collection(this, "events") { TTTGame_Events(
type = "",
move = -1.0,
time = -1.0
) }

override fun onHandleSync(handle: Handle, allSynced: Boolean) {
if (gameState.get()?.board == null) {
if (gameState.get() == null) {
gameState.set(defaultGame)
}
}
Expand Down
Loading

0 comments on commit fb06d42

Please sign in to comment.