Overview
Smartype is a powerful code generation tool that allows you to get the most out of the time you spend organizing and defining the structure of your data.
It aims to completely eliminate an entire class of data quality bugs
Example
The following JSON schema describes a coffee order with a few required parameters:
- item: An string value with a predefined set of allowed values
- quantity: A numeric value indicating how many coffees were ordered
- milk: A boolean value indicating if you want milk in your coffee
Smartype does the following with this:
- Consumes the JSON schema and generates Kotlin
data
classes - Uses Kotlin Multiplatform to translate that Kotlin code into other languages and generate consumable binaries
- Surfaces an API to send and receive these "messages", which can be consumed by any analytics provider or your own system
Supported Environments
Smartype supports the following language environments today:
- Any JVM environment, including Kotlin and Java for Android and server-side contexts
- iOS Swift and Objective-C
- Web browsers via TypeScript and JavaScript
mParticle Customers
Smartype is designed to be used by anyone, but support today is primarily for mParticle's Events API and SDKs by way of the mParticle Smartype receiver.
Navigate to the mParticle docs for more specific docs related to mParticle Data Plans and SDKs.
Workflow
Smartype is shipped as a CLI tool, and so a the typical workflow would be:
- Assemble your JSON schema into a file for consumption by Smartype
- Run Smartype to generate your libraries.
- Incorporate and use those libraries in any number of environments
Smartype CLI Usage
Smartype is deployed as an executable jar CLI, and you can download the latest release from the Github releases page.
The CLI provides two key commands:
init
: Initialize a configuration file that's used by Smartype to generate code.generate
: Generates strongly-type libraries based on your data model
init
Smartype Smartype init
will ask you a series of questions and then create a Smartype configuration file.
java -jar smartype.jar init
generate
Smartype Smartype generate
will read your configuration file and output binaries that are ready for consumption in an application.
java -jar smartype.jar generate
If this is your first call to 'generate', you may want to go grab a cup of coffee while it downloads dependencies. (It will be much faster the second time!)
Integrating Generated Code
To use the objects created by Smartype, you'll want to add the generated code to your projects. You will also want to initialize Smartype when the app starts up, and register any receivers that you would like to get notified for each message that gets logged.
The following code snippets use the mParticle receiver as an example, but receivers can be created to wrap any interface to which you want to send data, including for your own inhouse processing.
You can also (optionally) add yourself as a receiver, and then implement a receive
function to get a copy of all JSON messages that are sent. See the example projects for details of how this is done per platform.
iOS
Smartype generate
will create a "fat" dynamic framework that you can include directly with your projects.
- To use Smartype on iOS, start by adding
Smartype.framework
to your Xcode project - Next, import and initialize Smartype prior to use, and register any receivers
- The
SmartypeApi
object will surface a series of methods which each represent the top-level items in your schema - Pass the fully constructed objects into your
SmartypeApi
instance for all receivers
import Smartype
...
let api = SmartypeApi()
api.addReceiver(receiver: MParticleReceiver())
api.addReceiver(receiver: self)
let chooseCustomAttributes = ChooseItemDataCustomAttributes
.init(quantity: 5,
milk: true,
item: .cortado
)
let itemData = ChooseItemData.init(customAttributes: chooseCustomAttributes)
let chooseItem = api.chooseItem(data: itemData)
api.send(message: chooseItem)
Android
Smartype generate
will create an aar
file that you can include directly with your projects.
To use Smartype on Android, start by adding the generated smartype.aar
to your project and any 3rd-party receivers that you plan on using:
dependencies {
implementation "com.mparticle:smartype-api:1.2.4"
implementation "com.mparticle:smartype-mparticle:1.2.4"
implementation fileTree(dir: 'libs', include: ['**/*.aar'])
}
The Smartype API dependencies are deployed as a multiplatform library leveraging Gradle Module metadata, and in order for Android projects to resolve the right dependency, you may need to add the following to ensure debug builds use the "release" artifact.
buildTypes {
debug {
matchingFallbacks = ["release"]
}
}
- Import and initialize Smartype prior to use, and register your receivers
- The
SmartypeApi
object will surface a series of methods which each represent the top-level items in your schema - Pass the fully constructed objects into your
SmartypeApi
instance for all receivers
val api = SmartypeApi()
api.addReceiver(MParticleReceiver())
api.addReceiver(this)
val message = api.chooseItem(
ChooseItemData(
ChooseItemDataCustomAttributes(
quantity = 5.0,
milk = true,
item = ChooseItemDataCustomAttributesItem.CORTADO
)
)
)
//the message object will now be sent to all receivers
api.send(message)
Web
Smartype generate
will create a set of .js
and .d.ts
files that you can include directly with your projects. Our example uses webpack to concatenate and minify the source files.
To use Smartype on Web, start by adding the generated smartype-dist
directory to your project and any 3rd-party receivers that you plan on using, then include the relevant files in your typescript or javascript sources:
import * as smartype from "../smartype-dist/smartype.js"
- Import and initialize Smartype prior to use, and register your receivers
- The
SmartypeApi
object will surface a series of methods which each represent the top-level items in your schema - Pass the fully constructed objects into your
SmartypeApi
instance for all receivers
import * as smartype from "../smartype-dist/smartype.js"
var api = new smartype.SmartypeApi()
api.addReceiver(smartype.mParticleReceiver())
var message = smartype.chooseItem(
new smartype.ChooseItemData(
new smartype.ChooseItemDataCustomAttributes(
1, true, new smartype.ChooseItemDataCustomAttributesItem().CORTADO()
)
)
)
//the message object will now be sent to all receivers
api.send(message)
Example Projects
Contributing
At mParticle, we are proud of our code and like to keep things open source. If you'd like to contribute, simply fork this repo, push any code changes to your fork, and submit a Pull Request against the master
branch.
License
Apache 2.0