Sunday, October 9, 2011

Erlang JSON

I've got a Erlang web service (built on Spooky) which I want to return JSON. How hard could that be?

The problem is a multitude of half-assed implementations.

What seems to work is mochijson2, which comes as part of mochiweb, but only the version from Mochi, not the Basho branch.

I have an Erlang structure that looks like this:
[{"colin",[3,2,1]},{"dave",[4,3,2]}]

That's an array with two tuples. Shouldn't be rocket science, but apparently what they really want is:
[{struct,[{<<"colin">>,[1,2,3]}]},{struct,[{<<"dave">>,[4,3,2]}]}]
or some shit. Really? If I have to munge it into that format, I might as well just encode it myself.

P.S. After going a few rounds with mochijson2, I ended up just encoding it myself.

Monday, October 3, 2011

Switching Git Repositories

I just had one of those cases where I pulled down a read-only copy of someone else's GitHub repo, thinking that it'll work just fine and I'll never have to change it, and sure enough, I'm doing something weird that breaks it. I figure out what's going on and fix it, so now I want to fork my own copy on GitHub and push my changes to that, but my local copy is cloned from this other person's repo. What do I do?

This is of course exactly the sort of thing Git was designed to deal with. When I cloned this project from GitHub originally, Git set up a local repository for me, and set the GitHub repo as a remote repository named "origin". Unlike SVN, there's nothing magic about the remote repository - you can have several, and push changes to all of them. The easiest thing to do in this case is just forget about your old origin, and set a new one. Here's a slightly edited command history:
$ git remote -v
origin https://github.com/flashingpumpkin/spooky.git (fetch)
origin https://github.com/flashingpumpkin/spooky.git (push)
$ git remote rm origin
$ git remote -v
$ git remote add origin git@github.com:bluegraybox/spooky.git
$ git remote -v
origin git@github.com:bluegraybox/spooky.git (fetch)
origin git@github.com:bluegraybox/spooky.git (push)
$ git push
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 506 bytes, done.
Total 4 (delta 3), reused 0 (delta 0)
To git@github.com:bluegraybox/spooky.git
a34ae51..b15781e master -> master
$

Ta-daa!

Sunday, October 2, 2011

Erlang on Ubuntu

Ubuntu is still (as of Oct. 2011 on Natty) on Erlang R13B3. The release date for that is 2009-11-23. So, nearly two years out of date. I tried installing Nitrogen, but it requires at least R13B4, so it's time to install the new version from source.

The good news is that the source install doesn't stomp on the Ubuntu package. Ubuntu installs it to /usr/lib and /usr/bin, and the source install goes in /usr/local/lib and /usr/local/bin, which should be on your $PATH before /usr/bin.

Here's how to set up the latest version on a fairly vanilla Ubuntu 11.04. First, you need to download the Erlang source.

You may need to install some build tools - I had to install m4.
Then unpack and build the source:
tar xzf release.tgz
cd release
./configure
make
make install