-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle different types of timestamp representations
- Loading branch information
Showing
7 changed files
with
155 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package tapestry | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestComment_UnmarshalJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
want Comment | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "regular integer timestamp", | ||
input: `{"namespace":"test","created_at":1234567890,"text":"hello","id":"123"}`, | ||
want: Comment{ | ||
Namespace: "test", | ||
CreatedAt: 1234567890, | ||
Text: "hello", | ||
ID: "123", | ||
}, | ||
}, | ||
{ | ||
name: "low/high timestamp", | ||
input: `{"namespace":"test","created_at":{"low":-188638304,"high":402},"text":"hello","id":"456"}`, | ||
want: Comment{ | ||
Namespace: "test", | ||
CreatedAt: 1730683181984, | ||
Text: "hello", | ||
ID: "456", | ||
}, | ||
}, | ||
{ | ||
name: "invalid timestamp format", | ||
input: `{"namespace":"test","created_at":"invalid","text":"hello","id":"789"}`, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var got Comment | ||
err := json.Unmarshal([]byte(tt.input), &got) | ||
|
||
if (err != nil) != tt.wantErr { | ||
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if !tt.wantErr && got != tt.want { | ||
t.Errorf("UnmarshalJSON() = %+v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tapestry | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// UnixTimestamp represents a unix timestamp that can be unmarshaled from either | ||
// an integer or a 64 bit integer representation in object format | ||
type UnixTimestamp int64 | ||
|
||
func (t *UnixTimestamp) UnmarshalJSON(data []byte) error { | ||
// Try regular int64 first | ||
var timestamp int64 | ||
if err := json.Unmarshal(data, ×tamp); err == nil { | ||
*t = UnixTimestamp(timestamp) | ||
return nil | ||
} | ||
|
||
// Try 64 bit integer representation | ||
var tsObj struct { | ||
Low int64 `json:"low"` | ||
High int64 `json:"high"` | ||
} | ||
if err := json.Unmarshal(data, &tsObj); err != nil { | ||
return fmt.Errorf("failed to unmarshal timestamp: %w", err) | ||
} | ||
|
||
*t = UnixTimestamp((tsObj.High << 32) | (tsObj.Low & 0xFFFFFFFF)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tapestry | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestUnixTimestamp_UnmarshalJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
want UnixTimestamp | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "regular integer timestamp", | ||
input: "1234567890", | ||
want: UnixTimestamp(1234567890), | ||
}, | ||
{ | ||
name: "low/high timestamp", | ||
input: `{"low":-188638304,"high":402}`, | ||
want: UnixTimestamp(1730683181984), | ||
}, | ||
{ | ||
name: "invalid format", | ||
input: `"invalid"`, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var got UnixTimestamp | ||
err := json.Unmarshal([]byte(tt.input), &got) | ||
|
||
if (err != nil) != tt.wantErr { | ||
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if !tt.wantErr && got != tt.want { | ||
t.Errorf("UnmarshalJSON() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |