Skip to content

Commit

Permalink
define custom marshaller
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan committed Dec 16, 2023
1 parent a0eb7e6 commit 6789d91
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
14 changes: 7 additions & 7 deletions sentry/metric_alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestMetricAlertService_List(t *testing.T) {
AlertRuleTriggerID: String("12345"),
Type: String("slack"),
TargetType: String("specific"),
TargetIdentifier: &Int64OrString{IsString: true, String: "#alert-rule-alerts"},
TargetIdentifier: &Int64OrString{IsString: true, StringVal: "#alert-rule-alerts"},
InputChannelID: String("C038NF00X4F"),
IntegrationID: Int(123),
DateCreated: Time(mustParseTime("2022-04-07T16:46:49.154638Z")),
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestMetricAlertService_Get(t *testing.T) {
AlertRuleTriggerID: String("56789"),
Type: String("slack"),
TargetType: String("specific"),
TargetIdentifier: &Int64OrString{IsString: true, String: "#alert-rule-alerts"},
TargetIdentifier: &Int64OrString{IsString: true, StringVal: "#alert-rule-alerts"},
InputChannelID: String("C0XXXFKLXXX"),
IntegrationID: Int(111),
DateCreated: Time(mustParseTime("2022-04-15T15:06:01.087054Z")),
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestMetricAlertsService_CreateWithAsyncTask(t *testing.T) {
AlertRuleTriggerID: String("56789"),
Type: String("slack"),
TargetType: String("specific"),
TargetIdentifier: &Int64OrString{IsString: true, String: "#alert-rule-alerts"},
TargetIdentifier: &Int64OrString{IsString: true, StringVal: "#alert-rule-alerts"},
InputChannelID: String("C0XXXFKLXXX"),
IntegrationID: Int(123),
DateCreated: Time(mustParseTime("2022-04-15T15:06:01.087054Z")),
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestMetricAlertsService_CreateWithAsyncTask(t *testing.T) {
AlertRuleTriggerID: String("56789"),
Type: String("slack"),
TargetType: String("specific"),
TargetIdentifier: &Int64OrString{IsString: true, String: "#alert-rule-alerts"},
TargetIdentifier: &Int64OrString{IsString: true, StringVal: "#alert-rule-alerts"},
InputChannelID: String("C0XXXFKLXXX"),
IntegrationID: Int(111),
DateCreated: Time(mustParseTime("2022-04-15T15:06:01.087054Z")),
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestMetricAlertService_Create(t *testing.T) {
AlertRuleTriggerID: String("56789"),
Type: String("slack"),
TargetType: String("specific"),
TargetIdentifier: &Int64OrString{IsString: true, String: "#alert-rule-alerts"},
TargetIdentifier: &Int64OrString{IsString: true, StringVal: "#alert-rule-alerts"},
InputChannelID: String("C0XXXFKLXXX"),
IntegrationID: Int(123),
DateCreated: Time(mustParseTime("2022-04-15T15:06:01.087054Z")),
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestMetricAlertService_Create(t *testing.T) {
AlertRuleTriggerID: String("56789"),
Type: String("slack"),
TargetType: String("specific"),
TargetIdentifier: &Int64OrString{IsString: true, String: "#alert-rule-alerts"},
TargetIdentifier: &Int64OrString{IsString: true, StringVal: "#alert-rule-alerts"},
InputChannelID: String("C0XXXFKLXXX"),
IntegrationID: Int(111),
DateCreated: Time(mustParseTime("2022-04-15T15:06:01.087054Z")),
Expand Down Expand Up @@ -668,7 +668,7 @@ func TestMetricAlertService_Update(t *testing.T) {
AlertRuleTriggerID: String("56789"),
Type: String("slack"),
TargetType: String("specific"),
TargetIdentifier: &Int64OrString{IsString: true, String: "#alert-rule-alerts"},
TargetIdentifier: &Int64OrString{IsString: true, StringVal: "#alert-rule-alerts"},
InputChannelID: String("C0XXXFKLXXX"),
IntegrationID: Int(111),
DateCreated: Time(mustParseTime("2022-04-15T15:06:01.087054Z")),
Expand Down
26 changes: 21 additions & 5 deletions sentry/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type BoolOrStringSlice struct {
}

var _ json.Unmarshaler = (*BoolOrStringSlice)(nil)
var _ json.Marshaler = (*BoolOrStringSlice)(nil)

// UnmarshalJSON implements json.Unmarshaler.
func (bos *BoolOrStringSlice) UnmarshalJSON(data []byte) error {
Expand All @@ -40,16 +41,24 @@ func (bos *BoolOrStringSlice) UnmarshalJSON(data []byte) error {
return fmt.Errorf("unable to unmarshal as bool or string slice: %s", string(data))
}

func (bos BoolOrStringSlice) MarshalJSON() ([]byte, error) {
if bos.IsBool {
return json.Marshal(bos.BoolVal)
}
return json.Marshal(bos.StringSliceVal)
}

// Int64OrString is a type that can be unmarshaled from either an int64 or a
// string.
type Int64OrString struct {
IsInt64 bool
IsString bool
Int64Val int64
String string
IsInt64 bool
IsString bool
Int64Val int64
StringVal string
}

var _ json.Unmarshaler = (*Int64OrString)(nil)
var _ json.Marshaler = (*Int64OrString)(nil)

func (ios *Int64OrString) UnmarshalJSON(data []byte) error {
// Try to unmarshal as an int64
Expand All @@ -66,10 +75,17 @@ func (ios *Int64OrString) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &stringVal); err == nil {
ios.IsInt64 = false
ios.IsString = true
ios.String = stringVal
ios.StringVal = stringVal
return nil
}

// If neither worked, return an error
return fmt.Errorf("unable to unmarshal as int64 or string: %s", string(data))
}

func (ios Int64OrString) MarshalJSON() ([]byte, error) {
if ios.IsInt64 {
return json.Marshal(ios.Int64Val)
}
return json.Marshal(ios.StringVal)
}

0 comments on commit 6789d91

Please sign in to comment.