guregu / null

reasonable handling of nullable values
BSD 2-Clause "Simplified" License
1.86k stars 238 forks source link

Why does null.Time text-marshaling return null string? #47

Closed limoli closed 4 years ago

limoli commented 5 years ago

Testing my models on GraphQL, I detected some issues regarding text-marshaling implementation in this library.

null.Time returns "null"

func (t Time) MarshalText() ([]byte, error) {
    if !t.Valid {
        return []byte("null"), nil
    }
    return t.Time.MarshalText()
}

Other null.X implementations

// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string when this String is null.
func (s String) MarshalText() ([]byte, error) {
    if !s.Valid {
        return []byte{}, nil
    }
    return []byte(s.String), nil
}

// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string if this Int is null.
func (i Int) MarshalText() ([]byte, error) {
    if !i.Valid {
        return []byte{}, nil
    }
    return []byte(strconv.FormatInt(i.Int64, 10)), nil
}

My question/curiosity is: why does null.Time text-marshaling return a "null" string instead of an empty string like the others?

guregu commented 5 years ago

Good question... I don't have a real answer. I should probably release v4 and fix this.

Edit: I said it was through a PR but it's totally my fault.

limoli commented 5 years ago

Thank you so much @guregu ! :)

guregu commented 4 years ago

This is fixed in v4.0.0, just released.