Skip to content

Commit

Permalink
Nullable type wraps optional fields of API input objects
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Dec 27, 2024
1 parent 3274a99 commit 0d83852
Show file tree
Hide file tree
Showing 38 changed files with 1,342 additions and 1,159 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Refactor-20241227-103359.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Refactor
body: 'BREAKING CHANGE: "Nullable" type now wraps all optional struct fields of API input objects. This "Nullable" type enables fields to be set to the JSON "null" value.'
time: 2024-12-27T10:33:59.906375-06:00
4 changes: 2 additions & 2 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ func (client *Client) CreateTriggerDefinition(input CustomActionsTriggerDefiniti
} `graphql:"customActionsTriggerDefinitionCreate(input: $input)"`
}
if input.AccessControl == nil {
input.AccessControl = RefOf(CustomActionsTriggerDefinitionAccessControlEnumEveryone)
input.AccessControl = NewNullableFrom(CustomActionsTriggerDefinitionAccessControlEnumEveryone)
}
if input.EntityType == nil {
input.EntityType = RefOf(CustomActionsEntityTypeEnumService)
input.EntityType = NewNullableFrom(CustomActionsEntityTypeEnumService)
}
v := PayloadVariables{
"input": input,
Expand Down
82 changes: 41 additions & 41 deletions actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestCreateWebhookAction(t *testing.T) {
// Act
action, err := client.CreateWebhookAction(ol.CustomActionsWebhookActionCreateInput{
Name: "Deploy Rollback",
LiquidTemplate: ol.RefOf("{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"rollback\"}"),
Headers: &ol.JSON{"Content-Type": "application/json"},
LiquidTemplate: ol.NewNullableFrom("{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"rollback\"}"),
Headers: ol.NewNullableFrom(ol.JSON{"Content-Type": "application/json"}),
HttpMethod: ol.CustomActionsHttpMethodEnumPost,
WebhookUrl: "https://gitlab.com/api/v4/projects/1/trigger/pipeline",
})
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestUpdateWebhookAction(t *testing.T) {
// Act
action, err := client.UpdateWebhookAction(ol.CustomActionsWebhookActionUpdateInput{
Id: *newID,
HttpMethod: ol.RefOf(ol.CustomActionsHttpMethodEnumPut),
HttpMethod: ol.NewNullableFrom(ol.CustomActionsHttpMethodEnumPut),
})

// Assert
Expand All @@ -83,7 +83,7 @@ func TestUpdateWebhookAction2(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation WebhookActionUpdate($input:CustomActionsWebhookActionUpdateInput!){customActionsWebhookActionUpdate(input: $input){webhookAction{{ template "custom_actions_request" }},errors{message,path}}}`,
`{"input":{"id": "123456789","description":"","headers":"{\"Accept\":\"application/json\"}"}}`,
`{"input":{"id": "123456789","description":null,"headers":"{\"Accept\":\"application/json\"}"}}`,
`{"data": {"customActionsWebhookActionUpdate": { "webhookAction": {{ template "custom_action1" }}, "errors": [] }}}`,
)

Expand All @@ -92,8 +92,8 @@ func TestUpdateWebhookAction2(t *testing.T) {
// Act
action, err := client.UpdateWebhookAction(ol.CustomActionsWebhookActionUpdateInput{
Id: *newID,
Description: ol.RefOf(""),
Headers: &ol.JSON{"Accept": "application/json"},
Description: ol.NewNull(),
Headers: ol.NewNullableFrom(ol.JSON{"Accept": "application/json"}),
})

// Assert
Expand Down Expand Up @@ -122,21 +122,21 @@ func TestCreateTriggerDefinition(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation TriggerDefinitionCreate($input:CustomActionsTriggerDefinitionCreateInput!){customActionsTriggerDefinitionCreate(input: $input){triggerDefinition{{ template "custom_actions_trigger_request" }},errors{message,path}}}`,
`{"input":{"actionId":"123456789", "description":"Disables the Deploy Freeze","entityType":"SERVICE","filterId":"987654321","manualInputsDefinition":"", "name":"Deploy Rollback","ownerId":"123456789", "accessControl": "everyone", "responseTemplate": ""}}`,
`{"input":{"actionId":"123456789", "description":"Disables the Deploy Freeze","entityType":"SERVICE","filterId":"987654321","manualInputsDefinition":null, "name":"Deploy Rollback","ownerId":"123456789", "accessControl": "everyone", "responseTemplate": null}}`,
`{"data": {"customActionsTriggerDefinitionCreate": { "triggerDefinition": {{ template "custom_action_trigger1" }}, "errors": [] }}}`,
)
client := BestTestClient(t, "custom_actions/create_trigger", testRequest)

// Act
trigger, err := client.CreateTriggerDefinition(ol.CustomActionsTriggerDefinitionCreateInput{
Name: "Deploy Rollback",
AccessControl: ol.RefOf(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
Description: ol.RefOf("Disables the Deploy Freeze"),
ResponseTemplate: ol.RefOf(""),
ManualInputsDefinition: ol.RefOf(""),
ActionId: newID,
AccessControl: ol.NewNullableFrom(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
Description: ol.NewNullableFrom("Disables the Deploy Freeze"),
ResponseTemplate: ol.NewNull(),
ManualInputsDefinition: ol.NewNull(),
ActionId: ol.NewNullableFrom(*newID),
OwnerId: *newID,
FilterId: ol.NewID("987654321"),
FilterId: ol.NewNullableFrom(ol.ID("987654321")),
})
// Assert
autopilot.Ok(t, err)
Expand All @@ -147,26 +147,26 @@ func TestCreateTriggerDefinitionWithGlobalEntityType(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation TriggerDefinitionCreate($input:CustomActionsTriggerDefinitionCreateInput!){customActionsTriggerDefinitionCreate(input: $input){triggerDefinition{{ template "custom_actions_trigger_request" }},errors{message,path}}}`,
`{"input":{"actionId":"123456789", "description":"Disables the Deploy Freeze","entityType":"GLOBAL","extendedTeamAccess":[{"alias":"example_1"},{"alias":"example_1"}],"filterId":"987654321","manualInputsDefinition":"", "name":"Deploy Rollback","ownerId":"123456789", "accessControl": "everyone", "responseTemplate": ""}}`,
`{"input":{"actionId":"123456789", "description":"Disables the Deploy Freeze","entityType":"GLOBAL","extendedTeamAccess":[{"alias":"example_1"},{"alias":"example_1"}],"filterId":"987654321","manualInputsDefinition":null, "name":"Deploy Rollback","ownerId":"123456789", "accessControl": "everyone", "responseTemplate": null}}`,
`{"data": {"customActionsTriggerDefinitionCreate": { "triggerDefinition": {{ template "custom_action_trigger1" }}, "errors": [] }}}`,
)
client := BestTestClient(t, "custom_actions/create_trigger_with_global_entity", testRequest)

// Act
trigger, err := client.CreateTriggerDefinition(ol.CustomActionsTriggerDefinitionCreateInput{
Name: "Deploy Rollback",
AccessControl: ol.RefOf(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
Description: ol.RefOf("Disables the Deploy Freeze"),
ActionId: newID,
ManualInputsDefinition: ol.RefOf(""),
ResponseTemplate: ol.RefOf(""),
AccessControl: ol.NewNullableFrom(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
Description: ol.NewNullableFrom("Disables the Deploy Freeze"),
ActionId: ol.NewNullableFrom(*newID),
ManualInputsDefinition: ol.NewNull(),
ResponseTemplate: ol.NewNull(),
OwnerId: *newID,
FilterId: ol.NewID("987654321"),
EntityType: ol.RefOf(ol.CustomActionsEntityTypeEnumGlobal),
ExtendedTeamAccess: &[]ol.IdentifierInput{
FilterId: ol.NewNullableFrom(ol.ID("987654321")),
EntityType: ol.NewNullableFrom(ol.CustomActionsEntityTypeEnumGlobal),
ExtendedTeamAccess: ol.NewNullableFrom([]ol.IdentifierInput{
*ol.NewIdentifier("example_1"),
*ol.NewIdentifier("example_1"),
},
}),
})
// Assert
autopilot.Ok(t, err)
Expand All @@ -177,22 +177,22 @@ func TestCreateTriggerDefinitionWithNullExtendedTeams(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation TriggerDefinitionCreate($input:CustomActionsTriggerDefinitionCreateInput!){customActionsTriggerDefinitionCreate(input: $input){triggerDefinition{{ template "custom_actions_trigger_request" }},errors{message,path}}}`,
`{"input":{"actionId":"123456789", "description":"Disables the Deploy Freeze","entityType":"SERVICE","extendedTeamAccess":[],"filterId":"987654321","manualInputsDefinition":"", "name":"Deploy Rollback","ownerId":"123456789", "accessControl": "everyone", "responseTemplate": ""}}`,
`{"input":{"actionId":"123456789", "description":"Disables the Deploy Freeze","entityType":"SERVICE","extendedTeamAccess":[],"filterId":"987654321","manualInputsDefinition":null, "name":"Deploy Rollback","ownerId":"123456789", "accessControl": "everyone", "responseTemplate": null}}`,
`{"data": {"customActionsTriggerDefinitionCreate": { "triggerDefinition": {{ template "custom_action_trigger1" }}, "errors": [] }}}`,
)

client := BestTestClient(t, "custom_actions/create_trigger_with_null_extended_teams", testRequest)
// Act
trigger, err := client.CreateTriggerDefinition(ol.CustomActionsTriggerDefinitionCreateInput{
Name: "Deploy Rollback",
Description: ol.RefOf("Disables the Deploy Freeze"),
AccessControl: ol.RefOf(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
ManualInputsDefinition: ol.RefOf(""),
ResponseTemplate: ol.RefOf(""),
ActionId: newID,
Description: ol.NewNullableFrom("Disables the Deploy Freeze"),
AccessControl: ol.NewNullableFrom(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
ManualInputsDefinition: ol.NewNull(),
ResponseTemplate: ol.NewNull(),
ActionId: ol.NewNullableFrom(*newID),
OwnerId: *newID,
FilterId: ol.NewID("987654321"),
ExtendedTeamAccess: &[]ol.IdentifierInput{},
FilterId: ol.NewNullableFrom(ol.ID("987654321")),
ExtendedTeamAccess: ol.NewNullableFrom([]ol.IdentifierInput{}),
})
// Assert
autopilot.Ok(t, err)
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestUpdateTriggerDefinition(t *testing.T) {
// Act
trigger, err := client.UpdateTriggerDefinition(ol.CustomActionsTriggerDefinitionUpdateInput{
Id: *newID,
FilterId: ol.NewID(),
FilterId: ol.NewNullOf[ol.ID](),
})
// Assert
autopilot.Ok(t, err)
Expand All @@ -269,17 +269,17 @@ func TestUpdateTriggerDefinition2(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation TriggerDefinitionUpdate($input:CustomActionsTriggerDefinitionUpdateInput!){customActionsTriggerDefinitionUpdate(input: $input){triggerDefinition{{ template "custom_actions_trigger_request" }},errors{message,path}}}`,
`{"input":{"id":"123456789", "name":"test", "description": "", "extendedTeamAccess": []}}`,
`{"input":{"id":"123456789", "name":"test", "description": null, "extendedTeamAccess": []}}`,
`{"data": {"customActionsTriggerDefinitionUpdate": { "triggerDefinition": {{ template "custom_action_trigger1" }}, "errors": [] }}}`,
)
client := BestTestClient(t, "custom_actions/update_trigger2", testRequest)

// Act
trigger, err := client.UpdateTriggerDefinition(ol.CustomActionsTriggerDefinitionUpdateInput{
Id: *newID,
Name: ol.RefOf("test"),
Description: ol.RefOf(""),
ExtendedTeamAccess: &[]ol.IdentifierInput{},
Name: ol.NewNullableFrom("test"),
Description: ol.NewNull(),
ExtendedTeamAccess: ol.NewNullableFrom([]ol.IdentifierInput{}),
})
// Assert
autopilot.Ok(t, err)
Expand All @@ -290,20 +290,20 @@ func TestUpdateTriggerDefinition3(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation TriggerDefinitionUpdate($input:CustomActionsTriggerDefinitionUpdateInput!){customActionsTriggerDefinitionUpdate(input: $input){triggerDefinition{{ template "custom_actions_trigger_request" }},errors{message,path}}}`,
`{"input":{"id":"123456789", "name":"test", "description": "", "extendedTeamAccess": [{"alias": "123456789"}, { {{ template "id1" }} } ]}}`,
`{"input":{"id":"123456789", "name":"test", "description": null, "extendedTeamAccess": [{"alias": "123456789"}, { {{ template "id1" }} } ]}}`,
`{"data": {"customActionsTriggerDefinitionUpdate": { "triggerDefinition": {{ template "custom_action_trigger1" }}, "errors": [] }}}`,
)
client := BestTestClient(t, "custom_actions/update_trigger3", testRequest)

// Act
trigger, err := client.UpdateTriggerDefinition(ol.CustomActionsTriggerDefinitionUpdateInput{
Id: *newID,
Name: ol.RefOf("test"),
Description: ol.RefOf(""),
ExtendedTeamAccess: &[]ol.IdentifierInput{
Name: ol.NewNullableFrom("test"),
Description: ol.NewNull(),
ExtendedTeamAccess: ol.NewNullableFrom([]ol.IdentifierInput{
*ol.NewIdentifier(string(*newID)),
*ol.NewIdentifier(string(id1)),
},
}),
})
// Assert
autopilot.Ok(t, err)
Expand Down
2 changes: 1 addition & 1 deletion alert_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCreateAlertSourceService(t *testing.T) {
// Act
result, _ := client.CreateAlertSourceService(ol.AlertSourceServiceCreateInput{
Service: *ol.NewIdentifier("example"),
AlertSourceExternalIdentifier: ol.NewAlertSource(ol.AlertSourceTypeEnumDatadog, "QWERTY"),
AlertSourceExternalIdentifier: ol.NewNullableFrom(*ol.NewAlertSource(ol.AlertSourceTypeEnumDatadog, "QWERTY")),
})
// Assert
autopilot.Equals(t, "example", result.Service.Aliases[0])
Expand Down
2 changes: 1 addition & 1 deletion aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (client *Client) CreateAliases(ownerId ID, aliases []string) ([]string, err
for _, alias := range aliases {
input := AliasCreateInput{
Alias: alias,
OwnerId: ownerId,
OwnerId: ID(ownerId),
}
result, err := client.CreateAlias(input)
allErrors = errors.Join(allErrors, err)
Expand Down
2 changes: 1 addition & 1 deletion category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestUpdateRubricCategory(t *testing.T) {
// Act
result, err := client.UpdateCategory(ol.CategoryUpdateInput{
Id: id4,
Name: ol.RefOf("Emily"),
Name: ol.NewNullableFrom("Emily"),
})
// Assert
autopilot.Ok(t, err)
Expand Down
36 changes: 18 additions & 18 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ type CheckCreateInputProvider interface {
}

type CheckCreateInput struct {
Name string `json:"name" yaml:"name" mapstructure:"name"`
Enabled *bool `json:"enabled" yaml:"enabled" mapstructure:"enabled"`
EnableOn *iso8601.Time `json:"enableOn,omitempty" yaml:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Category ID `json:"categoryId" yaml:"categoryId" mapstructure:"categoryId"`
Level ID `json:"levelId" yaml:"levelId" mapstructure:"levelId"`
Owner *ID `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
Filter *ID `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Notes *string `json:"notes" yaml:"notes" default:"Notes on Example Check" mapstructure:"notes"`
Category ID `json:"categoryId" yaml:"categoryId" mapstructure:"categoryId"`
EnableOn *Nullable[iso8601.Time] `json:"enableOn,omitempty" yaml:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Enabled *Nullable[bool] `json:"enabled,omitempty" yaml:"enabled,omitempty" mapstructure:"enabled"`
Filter *Nullable[ID] `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Level ID `json:"levelId" yaml:"levelId" mapstructure:"levelId"`
Name string `json:"name" yaml:"name" mapstructure:"name"`
Notes *Nullable[string] `json:"notes,omitempty" yaml:"notes,omitempty" mapstructure:"notes,omitempty"`
Owner *Nullable[ID] `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
}

func NewCheckCreateInputTypeOf[T any](checkCreateInput CheckCreateInput) *T {
Expand All @@ -113,15 +113,15 @@ type CheckUpdateInputProvider interface {
}

type CheckUpdateInput struct {
Id ID `json:"id" mapstructure:"id"`
Name string `json:"name,omitempty" mapstructure:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty" mapstructure:"enabled,omitempty"`
EnableOn *iso8601.Time `json:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Category ID `json:"categoryId,omitempty" mapstructure:"categoryId,omitempty"`
Level ID `json:"levelId,omitempty" mapstructure:"levelId,omitempty"`
Owner *ID `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
Filter *ID `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Notes *string `json:"notes,omitempty" mapstructure:"notes,omitempty"`
Category *Nullable[ID] `json:"categoryId,omitempty" mapstructure:"categoryId,omitempty"`
EnableOn *Nullable[iso8601.Time] `json:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Enabled *Nullable[bool] `json:"enabled,omitempty" mapstructure:"enabled,omitempty"`
Filter *Nullable[ID] `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Id ID `json:"id" mapstructure:"id"`
Level *Nullable[ID] `json:"levelId,omitempty" mapstructure:"levelId,omitempty"`
Name *Nullable[string] `json:"name,omitempty" mapstructure:"name,omitempty"`
Notes *Nullable[string] `json:"notes,omitempty" mapstructure:"notes,omitempty"`
Owner *Nullable[ID] `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
}

func NewCheckUpdateInputTypeOf[T any](checkUpdateInput CheckUpdateInput) *T {
Expand Down Expand Up @@ -275,7 +275,7 @@ func (client *Client) DeleteCheck(id ID) error {
} `graphql:"checkDelete(input: $input)"`
}
v := PayloadVariables{
"input": CheckDeleteInput{Id: &id},
"input": CheckDeleteInput{Id: NewNullableFrom(id)},
}
err := client.Mutate(&m, v, WithName("CheckDelete"))
return HandleErrors(err, m.Payload.Errors)
Expand Down
6 changes: 3 additions & 3 deletions check_manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func NewManualCheckFrequencyInput(startingDate string, timeScale FrequencyTimeSc
func NewManualCheckFrequencyUpdateInput(startingDate string, timeScale FrequencyTimeScale, value int) *ManualCheckFrequencyUpdateInput {
startingDateIso := NewISO8601Date(startingDate)
return &ManualCheckFrequencyUpdateInput{
StartingDate: &startingDateIso,
FrequencyTimeScale: &timeScale,
FrequencyValue: &value,
StartingDate: NewNullableFrom(startingDateIso),
FrequencyTimeScale: NewNullableFrom(timeScale),
FrequencyValue: NewNullableFrom(value),
}
}

Expand Down
Loading

0 comments on commit 0d83852

Please sign in to comment.