Ernest Micklei's Blog

things I create and problems I solve, to remember and share

Android Animated Chain

Android Animated Chain

Hiding complexity of Android animators when chaining dependencies.

Thanks to our guest software artist: [Jamie Craane](http://jcraane.blogspot.nl/)

JSONPath

JSONPath

Implementation of an XPath-like function for JSON documents to test REST api’s using forest.

2016 - together, we celebrate

This short (2 min) video was created using a simple interactive Ubanita script that translated touch events into Triangle generation. See below the interesting part of that program.

For each touchMove event, a random colored Triangle sprite is added. For each touchEnd event, such Triangles are created with a outwards radial velocity.

// touchMove is called when the game detects a movement in a touch
function touchMove(event) {
        var dus = {};
        dust.x = event.data.x;
        dust.y = event.data.y;
        dust.fill = randomColor();
        dust.radius = 12;
        dust.vx = _.random(-20, 20);
        dust.vy = _.random(-20, 20);
        _addTriangle(0, dust)
    }
    // when a player moves a (single) touch then call this function
_on("touchMove", touchMove);

// touchEnd is called when the game no longer detects a touch
function touchEnd(event) {
    var dus = {};
    dust.fill = randomColor();
    dust.radius = 4;
    for (var e = 0; e < 360; e += 5) {
        var rad = Math.PI * e / 180.0
        var cx = Math.cos(rad)
        var cy = Math.sin(rad)

        dust.x = event.data.x + (5 * cx);
        dust.y = event.data.y + (5 * cy);

        dust.vx = 200 * cx
        dust.vy = 200 * cy
        _addTriangle(0, dust)
    }
}

Kimtato, a simple physics playground

Kimtato is the name of a simple interactive demo that runs on the Ubanita platform. It is a playground of physical objects which are created by players using touch gestures on the controller. Once created, the objects can be pushed around using the two-finger gesture, we call edge input mode.

The following will discuss the making of this demo using the Ubanita Script API.

get dimensions

To position objects on the 2D scene, it can be useful to known the actual dimensions of that scene. The scene has two properties for the width (pixels) and height respectively. Using this snippet, we assign those property values to global variables W and H.

Testing your REST api in Go with forest

Go package forest was created to simplify the code needed to write real tests of a REST api. Such tests need to setup Http Requests, send them and inspect Http Responses. The Go standard library has all that is needed to achieve this flow but the required amount of code, especially the error handling, makes tests harder to read and longer to write.

forestview

Testing github

Let's create a few tests that examine the response of the public github API call.

package main

import (
	"net/http"
	"testing"

	. "github.com/emicklei/forest"
)

var github = NewClient("https://api.github.com", new(http.Client))

func TestForestProjectExists(t *testing.T) {
	cfg := NewConfig("/repos/emicklei/{repo}", "forest").Header("Accept", "application/json")
	r := github.GET(t, cfg)
	ExpectStatus(t, r, 200)
}

The package variable github (line 10) is a forest Http client wrapper and provides the api to send Http requests (such as GET). On line 13, the cfg variable is a RequestConfig value that is used to build a http.Request with method,url,headers,body. The variable r is a *http.Response. The ExpectStatus function checks the status code. If the code does not match, then the following will be reported.

Artreyu, an artifact assembly tool

Artreyu is an open-source command line tool for software build pipelines that need to create artifacts which are composed of multiple versioned build parts. The design of this tool is inspired by the Apache Maven project which provides assembly support for Java projects. I created Artreyu to support a project that delivers compositions of many non-Java artifacts of which some are operating system dependent.

Artreyu works with a repository to archive, fetch and assemble artifacts. To support remote repositories such as Artifactory and Nexus, Artreyu has a simple plugin architecture ; each plugin is just a binary that gets invoked by the artreyu tool. Sources for the nexus plugin is on github.com/emicklei/artreyu-nexus.

Line scanning in Go

Today, I needed to keep track of the linenumber while scanning table driven tests. The standard Go bufio.Scanner does not provide such information. Fortunately, in Go you can create your own by embedding the standard type and overriding the right function. That’s it.

import (
	"bufio"
	"io"
)

type linescanner struct {
	*bufio.Scanner
	line int
}

func newScanner(reader io.Reader) *linescanner {
	return &linescanner{bufio.NewScanner(reader), 0}
}

func (l *linescanner) Scan() bool {
	ok := l.Scanner.Scan()
	if ok {
		l.line++
	}
	return ok
}

Guava-like EventBus for Go

I needed a simple solution to notify components about changes in some global settings. In my Java days, I have been using the Guava Eventbus to solve similar problems, so I decided to cook something that does the basics.

From Guava: “The EventBus allows publish-subscribe-style communication between components requiring the components to explicitly register with one another (and thus be aware of each other).”

The Java implementation uses annotations to register components and subscribe its methods to event types. In my Go version, with lack of such annotations, I decided to keep it simpler; just subscribe a function to an event by name. The singleton EventBus value will be an exposed package variable.