
Eliminates data quality bugs by generating statically-typed API clients from JSON schemas, creating type-safe models with auto-complete. Extensible via plugins, compatible across various platforms, and supports multiple environments.
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 π by generating statically-typed API clients based on the popular JSON schema standard.
π Smartype is built to translate any JSON schema into type-safe models
π It gives you auto-complete for your data model in your IDE of choice
π It's open source and extensible via a plugin system
π And it's built for all and with β€οΈ by mParticle
The following JSON schema describes a coffee order with a few required parameters:
Smartype does the following with this:
data classesSmartype supports the following language environments today:
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.
Smartype is shipped as a CLI tool, and so a the typical workflow would be:
Rather than manually creating a JSON file, mParticle provides these files ready for use by Smartype. There are multiple ways to retrieve them, but for automation purposes using the mParticle CLI tool is the best option.
We provide a ready-to-use Github Actions workflow file here, which can be adapted to other CI systems: https://github.com/mParticle/mparticle-workflows/blob/main/.github/workflows/data-plan-fetch.yml
Individual developers can also manually download the Data Plan JSON by using the mParticle CLI directly from the command line.
npm install -g @mparticle/cli.Once the mParticle CLI has been installed, the Data Plan JSON can be downloaded using a single command.
First you will need the following pieces of information:
You can create and manage your mParticle access tokens for Data Planning with the API Credentials interface.
# Here we'll use environment variables, but you can simply pass the values directly to the mp command if you'd prefer
export $DATA_PLAN_ID=...
export $DATA_PLAN_VERSION=...
export $WORKSPACE_ID=...
export $CLIENT_ID=...
export $CLIENT_SECRET=...
# $OUT_FILE can be any file name you like
export $OUT_FILE=${DATA_PLAN_ID}_${DATA_PLAN_VERSION}.json
# Pull down the JSON file
mp planning:data-plan-versions:fetch --dataPlanId=$DATA_PLAN_ID --versionNumber=$DATA_PLAN_VERSION --workspaceId=$WORKSPACE_ID --clientId=$CLIENT_ID --clientSecret=$CLIENT_SECRET --outFile=$OUT_FILESmartype 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 modelSmartype init will ask you a series of questions and then create a Smartype configuration file.
java -jar smartype.jar initSmartype generate will read your configuration file and output binaries that are ready for consumption in an application.
java -jar smartype.jar generateIf 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!)
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.
Smartype generate will create an "fat" dynamic framework that you can include directly with your projects.
smartype.xcframework to your Xcode projectSmartypeApi object will surface a series of methods which each represent the top-level items in your schemaSmartypeApi instance for all receiversimport 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)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"]
}
}SmartypeApi object will surface a series of methods which each represent the top-level items in your schemaSmartypeApi instance for all receiversval 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)Smartype supports both the mParticle Web SDK as well as the mParticle React Native plugin.
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 with the Web SDK or with React Native, 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"SmartypeApi object will surface a series of methods which each represent the top-level items in your schemaSmartypeApi instance for all receiversimport * 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)In order to enable React Native:
mParticleReceiver:import MParticle from 'react-native-mparticle'
...
var api = new smartype.SmartypeApi()
var receiver = smartype.mParticleReceiver()
receiver.react = MParticle
api.addReceiver(receiver)You will also want to exclude the generated .smartype directory from your React Project by configuring your metro.config.js file:
const exclusionList = require('metro-config/src/defaults/exclusionList');
...
module.exports = {
resolver: {
blockList: exclusionList([/\.smartype\/.*/])
},
};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.
Apache 2.0
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 π by generating statically-typed API clients based on the popular JSON schema standard.
π Smartype is built to translate any JSON schema into type-safe models
π It gives you auto-complete for your data model in your IDE of choice
π It's open source and extensible via a plugin system
π And it's built for all and with β€οΈ by mParticle
The following JSON schema describes a coffee order with a few required parameters:
Smartype does the following with this:
data classesSmartype supports the following language environments today:
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.
Smartype is shipped as a CLI tool, and so a the typical workflow would be:
Rather than manually creating a JSON file, mParticle provides these files ready for use by Smartype. There are multiple ways to retrieve them, but for automation purposes using the mParticle CLI tool is the best option.
We provide a ready-to-use Github Actions workflow file here, which can be adapted to other CI systems: https://github.com/mParticle/mparticle-workflows/blob/main/.github/workflows/data-plan-fetch.yml
Individual developers can also manually download the Data Plan JSON by using the mParticle CLI directly from the command line.
npm install -g @mparticle/cli.Once the mParticle CLI has been installed, the Data Plan JSON can be downloaded using a single command.
First you will need the following pieces of information:
You can create and manage your mParticle access tokens for Data Planning with the API Credentials interface.
# Here we'll use environment variables, but you can simply pass the values directly to the mp command if you'd prefer
export $DATA_PLAN_ID=...
export $DATA_PLAN_VERSION=...
export $WORKSPACE_ID=...
export $CLIENT_ID=...
export $CLIENT_SECRET=...
# $OUT_FILE can be any file name you like
export $OUT_FILE=${DATA_PLAN_ID}_${DATA_PLAN_VERSION}.json
# Pull down the JSON file
mp planning:data-plan-versions:fetch --dataPlanId=$DATA_PLAN_ID --versionNumber=$DATA_PLAN_VERSION --workspaceId=$WORKSPACE_ID --clientId=$CLIENT_ID --clientSecret=$CLIENT_SECRET --outFile=$OUT_FILESmartype 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 modelSmartype init will ask you a series of questions and then create a Smartype configuration file.
java -jar smartype.jar initSmartype generate will read your configuration file and output binaries that are ready for consumption in an application.
java -jar smartype.jar generateIf 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!)
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.
Smartype generate will create an "fat" dynamic framework that you can include directly with your projects.
smartype.xcframework to your Xcode projectSmartypeApi object will surface a series of methods which each represent the top-level items in your schemaSmartypeApi instance for all receiversimport 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)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"]
}
}SmartypeApi object will surface a series of methods which each represent the top-level items in your schemaSmartypeApi instance for all receiversval 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)Smartype supports both the mParticle Web SDK as well as the mParticle React Native plugin.
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 with the Web SDK or with React Native, 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"SmartypeApi object will surface a series of methods which each represent the top-level items in your schemaSmartypeApi instance for all receiversimport * 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)In order to enable React Native:
mParticleReceiver:import MParticle from 'react-native-mparticle'
...
var api = new smartype.SmartypeApi()
var receiver = smartype.mParticleReceiver()
receiver.react = MParticle
api.addReceiver(receiver)You will also want to exclude the generated .smartype directory from your React Project by configuring your metro.config.js file:
const exclusionList = require('metro-config/src/defaults/exclusionList');
...
module.exports = {
resolver: {
blockList: exclusionList([/\.smartype\/.*/])
},
};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.
Apache 2.0