From 6789d91d01d54d76d63ec96e5923c03a84a531d5 Mon Sep 17 00:00:00 2001 From: Jian Yuan Lee Date: Sat, 16 Dec 2023 01:49:34 +0000 Subject: [PATCH] define custom marshaller --- sentry/metric_alerts_test.go | 14 +++++++------- sentry/types.go | 26 +++++++++++++++++++++----- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/sentry/metric_alerts_test.go b/sentry/metric_alerts_test.go index 221aabb..8412700 100644 --- a/sentry/metric_alerts_test.go +++ b/sentry/metric_alerts_test.go @@ -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")), @@ -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")), @@ -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")), @@ -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")), @@ -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")), @@ -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")), @@ -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")), diff --git a/sentry/types.go b/sentry/types.go index ebc6c4f..937caab 100644 --- a/sentry/types.go +++ b/sentry/types.go @@ -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 { @@ -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 @@ -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) +}