github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/types/nullable/nullable.go (about)

     1  // Used internally by GoDynamo for nullable primitives
     2  package nullable
     3  
     4  import (
     5  	"encoding/json"
     6  )
     7  
     8  // NullableString is a string that when empty is marshaled as null
     9  // use this when there is an *optional* string parameter.
    10  type NullableString string
    11  
    12  func (n NullableString) MarshalJSON() ([]byte, error) {
    13  	sn := string(n)
    14  	if sn == "" {
    15  		var i interface{}
    16  		return json.Marshal(i)
    17  	}
    18  	return json.Marshal(sn)
    19  }
    20  
    21  // NullableUInt64 is a uint64 that when empty (0) is marshaled as null
    22  // use this when there is an *optional* uint64 parameter
    23  type NullableUInt64 uint64
    24  
    25  func (n NullableUInt64) MarshalJSON() ([]byte, error) {
    26  	in := uint64(n)
    27  	if in == 0 {
    28  		var i interface{}
    29  		return json.Marshal(i)
    30  	}
    31  	return json.Marshal(in)
    32  }