Skip to content

Commit

Permalink
feat: add Validate to timestamp
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Stewart <christian@aperture.us>
  • Loading branch information
paralin committed Apr 22, 2024
1 parent 503e9d2 commit 2771ae1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions testproto/wkt/wkt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ func TestWellKnownTypes(t *testing.T) {
err = umc.Unmarshal(jdata, jparsed)
require.NoError(t, err)
require.True(t, jparsed.EqualVT(m))
require.True(t, jparsed.EqualMessageVT(m))
}
17 changes: 17 additions & 0 deletions types/known/timestamppb/timestamp.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package timestamppb

import (
"errors"
"strconv"
"strings"

"github.com/aperturerobotics/protobuf-go-lite/json"
)

// ErrEmptyTimestamp is returned from Validate if the timestamp was empty.
var ErrEmptyTimestamp = errors.New("empty timestamp")

// Validate is an alias to CheckValid.
func (x *Timestamp) Validate(allowEmpty bool) error {
isEmpty := x.SizeVT() == 0
if isEmpty {
if allowEmpty {
return nil
}
return ErrEmptyTimestamp
}

return x.CheckValid()
}

// MarshalJSON marshals the Timestamp to JSON.
func (x *Timestamp) MarshalJSON() ([]byte, error) {
return json.DefaultMarshalerConfig.Marshal(x)
Expand Down

0 comments on commit 2771ae1

Please sign in to comment.