Skip to content

Instantly share code, notes, and snippets.

@serinth
Created April 29, 2018 23:30
Show Gist options
  • Save serinth/83cc7d48fa6efa1057f2761893fbe54d to your computer and use it in GitHub Desktop.
Save serinth/83cc7d48fa6efa1057f2761893fbe54d to your computer and use it in GitHub Desktop.
Golang XORM mapping null values in response and in DB. Uses protocol buffers

model.proto file:

import "google/protobuf/wrappers.proto";

message Entity {
	google.protobuf.UInt32Value myField  = 1;
}

entity.go file:

import (
	"database/sql"
)
type Entity struct {
	MyField		sql.NullInt64
}

Mapper to Insert entity

import (
	"github.com/golang/protobuf/ptypes/wrappers"
	"database/sql"
)
if entity.MyField != nil {
	toinsert.MyField = sql.NullInt64{ Int64: int64(entity.MyField.GetValue()), Valid: true}
}

Mapper to return to FE, empty values will show up with value "null"

if entity.MyField.Valid {
	toReturn.MyField = &wrappers.UInt32Value{Value: uint32(entity.MyField.Int64)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment