Thursday, September 25, 2014

Unix & stdlog

I wish Unix had also added stdlog.  It seems to be a basic part of building software, but we have to cram it into stderr, which makes no sense.  Someone got lazy.  Fortran had the right idea.

http://www.gfdl.noaa.gov/~vb/mpp.html#stdunits

stdin, stdout, stderr, stdlog

function stdin()
  integer :: stdin
function stdout()
  integer :: stdout
function stderr()
  integer :: stderr
function stdlog()
  integer :: stdlog 
 

Monday, July 7, 2014

Gorilla Mux and multiple routers

Looking at Gorilla's Mux, I wanted to come up with a simple example of how to bind multiple routers to different base URLs.  For the first router, I'm not using the PathPrefix() method, so the handler function must contain the prefix that you're interested ('/v1').  The second doesn't require that as we're using the Subrouter() that utilizes the prefix for all handlers.


package main

import (
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    rtr := mux.NewRouter()
    rtr.HandleFunc("/v1/user/{name:[a-z]+}/profile", profile).Methods("GET")

    rtrv2 := mux.NewRouter().PathPrefix("/v2").Subrouter()
    rtrv2.HandleFunc("/user/{name:[a-z]+}/profile", profilev2).Methods("GET")
 

    http.Handle("/v1/", rtr)    // need trailing slash     
    http.Handle("/v2/", rtrv2)  // need trailing slash

    log.Println("Listening...")
    http.ListenAndServe(":3000", nil)
}

func profile(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    name := params["name"]
    w.Write([]byte("Hello " + name))
}

func profilev2(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    name := params["name"]
    w.Write([]byte("Goodbye " + name))
}

Tuesday, April 22, 2014

Converting To and From UUIDs in MySQL

DELIMITER |

CREATE FUNCTION uuid_from_bin(b BINARY(16))
RETURNS CHAR(36) DETERMINISTIC
BEGIN
DECLARE hex CHAR(32);
SET hex = LOWER(HEX(b));
RETURN CONCAT(LEFT(hex, 8), '-', MID(hex, 9,4), '-', MID(hex, 13,4), '-', MID(hex, 17,4), '-', RIGHT(hex, 12));
END
|

CREATE FUNCTION uuid_to_bin(s CHAR(36))
RETURNS BINARY(16) DETERMINISTIC
RETURN UNHEX(CONCAT(LEFT(s, 8), MID(s, 10, 4), MID(s, 15, 4), MID(s, 20, 4), RIGHT(s, 12)))
|

DELIMITER ;


Source:  https://gist.github.com/159151/e66f0cc0a643c86041df3adb81d5758611e82096

Wednesday, March 19, 2014

NodeJS: How to return a JSON parsed object from v8

  string s = jvalue.toStyledString();
  v8::Handle<v8::Value> vs = cvv8::CastToJS(s);

    v8::Handle<v8::Context> context = v8::Context::GetCurrent();
    v8::Handle<v8::Object> global = context->Global();

    v8::Handle<v8::Object> JSON = global->Get(v8::String::New("JSON"))->ToObject();
    v8::Handle<v8::Function> JSON_parse = v8::Handle<v8::Function>::Cast(JSON->Get(v8::String::New("parse")));

    // return JSON.parse.apply(JSON, jsonString);
    return scope.Close(JSON_parse->Call(JSON, 1, &vs));

Wednesday, December 7, 2011

Apache Thrift: Converting from simple JSON

Since I haven't seen this code posted anywhere on the internet, I figured this would help a few people out:


#include <jsoncpp/json/json.h>
#include <transport/TBufferTransports.h>
#include <protocol/TJSONProtocol.h>


template<class T>
void DeserializeThriftObjFromJson(std::string jsonStr, T &obj) {

  boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> membuffer(new apache::thrift::transport::TMemoryBuffer(sizeof(T)));
  membuffer->write(((const uint8_t*)jsonStr.c_str()), jsonStr.length());

  apache::thrift::protocol::TJSONProtocol proto(membuffer);

  obj.read(&proto);

}

template<class T>
void SerializeThriftObjToJson(T &obj, std::string &jsonStr) {

  boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> membuffer(new apache::thrift::transport::TMemoryBuffer(sizeof(T)));
  apache::thrift::protocol::TJSONProtocol proto(membuffer);
  obj.write(&proto);

  jsonStr = membuffer->getBufferAsString();
}


template<class T>
static void SerializeThriftVectorToJson(std::vector<T> &thriftVec, std::string &jsonStr) {
  // Since the vector object doesn't support ->write / ->read, we have to
  // put the data in some other serialized format.  JSON is the easiest.
  Json::Value jsonVec;
  for (size_t i = 0; i < thriftVec.size(); i++) {
   std::string thriftJson;
   SerializeThriftObjToJson< T >(thriftVec[i], thriftJson);
   jsonVec[i] = thriftJson;
  }

  jsonStr = jsonVec.write();
}

template<class T>
static void DeserializeThriftVectorFromJson(std::string &jsonStr, std::vector< T > &thriftVec) {

  Json::Value json = Json::Parse(jsonStr);
  Json::Value::UInt size = json.size();

  for (Json::Value::UInt i = 0; i < size; i++) {
    std::string thriftJson = json[i].asString();
    T e;
    DeserializeThriftObjFromJson< T >(thriftJson, e);
    thriftVec.push_back(e);
  }

}