Skip to content

Commit

Permalink
fix(dictionary): fix all relevant fields to be pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Nov 29, 2023
1 parent 5308969 commit 4efb289
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 169 deletions.
30 changes: 5 additions & 25 deletions fastly/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,19 @@ package fastly
import (
"fmt"
"net/url"
"sort"
"time"
)

// Dictionary represents a dictionary response from the Fastly API.
type Dictionary struct {
CreatedAt *time.Time `mapstructure:"created_at"`
DeletedAt *time.Time `mapstructure:"deleted_at"`
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
ServiceID string `mapstructure:"service_id"`
ServiceVersion int `mapstructure:"version"`
ID *string `mapstructure:"id"`
Name *string `mapstructure:"name"`
ServiceID *string `mapstructure:"service_id"`
ServiceVersion *int `mapstructure:"version"`
UpdatedAt *time.Time `mapstructure:"updated_at"`
WriteOnly bool `mapstructure:"write_only"`
}

// dictionariesByName is a sortable list of dictionaries.
type dictionariesByName []*Dictionary

// Len implement the sortable interface.
func (s dictionariesByName) Len() int {
return len(s)
}

// Swap implement the sortable interface.
func (s dictionariesByName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// Less implement the sortable interface.
func (s dictionariesByName) Less(i, j int) bool {
return s[i].Name < s[j].Name
WriteOnly *bool `mapstructure:"write_only"`
}

// ListDictionariesInput is used as input to the ListDictionaries function.
Expand Down Expand Up @@ -65,7 +46,6 @@ func (c *Client) ListDictionaries(i *ListDictionariesInput) ([]*Dictionary, erro
if err := decodeBodyMap(resp.Body, &bs); err != nil {
return nil, err
}
sort.Stable(dictionariesByName(bs))
return bs, nil
}

Expand Down
14 changes: 6 additions & 8 deletions fastly/dictionary_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
// DictionaryInfo represents a dictionary metadata response from the Fastly API.
type DictionaryInfo struct {
// Digest is the hash of the dictionary content.
Digest string `mapstructure:"digest"`
Digest *string `mapstructure:"digest"`
// ItemCount is the number of items belonging to the dictionary.
ItemCount int `mapstructure:"item_count"`
ItemCount *int `mapstructure:"item_count"`
// LastUpdated is the Time-stamp (GMT) when the dictionary was last updated.
LastUpdated *time.Time `mapstructure:"last_updated"`
}

// GetDictionaryInfoInput is used as input to the GetDictionary function.
type GetDictionaryInfoInput struct {
// ID is the alphanumeric string identifying a dictionary.
// ID is the alphanumeric string identifying a dictionary (required).
ID string
// ServiceID is the ID of the service Dictionary belongs to (required).
ServiceID string
Expand All @@ -27,18 +27,16 @@ type GetDictionaryInfoInput struct {

// GetDictionaryInfo retrieves the specified resource.
func (c *Client) GetDictionaryInfo(i *GetDictionaryInfoInput) (*DictionaryInfo, error) {
if i.ID == "" {
return nil, ErrMissingID
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}

if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}

if i.ID == "" {
return nil, ErrMissingID
}

path := fmt.Sprintf("/service/%s/version/%d/dictionary/%s/info", i.ServiceID, i.ServiceVersion, i.ID)
resp, err := c.Get(path, nil)
if err != nil {
Expand Down
37 changes: 17 additions & 20 deletions fastly/dictionary_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ func TestClient_GetDictionaryInfo(t *testing.T) {
record(t, fixtureBase+"create_dictionary_items", func(c *Client) {
err = c.BatchModifyDictionaryItems(&BatchModifyDictionaryItemsInput{
ServiceID: testService.ID,
DictionaryID: testDictionary.ID,
DictionaryID: *testDictionary.ID,
Items: []*BatchDictionaryItem{
{
Operation: CreateBatchOperation,
ItemKey: "test-dictionary-item-0",
ItemValue: "value",
Operation: ToPointer(CreateBatchOperation),
ItemKey: ToPointer("test-dictionary-item-0"),
ItemValue: ToPointer("value"),
},
{
Operation: CreateBatchOperation,
ItemKey: "test-dictionary-item-1",
ItemValue: "value",
Operation: ToPointer(CreateBatchOperation),
ItemKey: ToPointer("test-dictionary-item-1"),
ItemValue: ToPointer("value"),
},
},
})
Expand All @@ -47,41 +47,38 @@ func TestClient_GetDictionaryInfo(t *testing.T) {
info, err = c.GetDictionaryInfo(&GetDictionaryInfoInput{
ServiceID: testService.ID,
ServiceVersion: testVersion.Number,
ID: testDictionary.ID,
ID: *testDictionary.ID,
})
})
if err != nil {
t.Fatal(err)
}

if info.ItemCount != 2 {
t.Errorf("bad item_count: %d", info.ItemCount)
if *info.ItemCount != 2 {
t.Errorf("bad item_count: %d", *info.ItemCount)
}
}

func TestClient_GetDictionaryInfo_validation(t *testing.T) {
var err error
_, err = testClient.GetDictionaryInfo(&GetDictionaryInfoInput{
ServiceID: "",
})
if err != ErrMissingServiceID {
_, err = testClient.GetDictionaryInfo(&GetDictionaryInfoInput{})
if err != ErrMissingID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetDictionaryInfo(&GetDictionaryInfoInput{
ServiceID: "foo",
ServiceVersion: 0,
ID: "123",
})
if err != ErrMissingServiceVersion {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetDictionaryInfo(&GetDictionaryInfoInput{
ID: "123",
ServiceID: "foo",
ServiceVersion: 1,
ID: "",
ServiceVersion: 0,
})
if err != ErrMissingID {
if err != ErrMissingServiceVersion {
t.Errorf("bad error: %s", err)
}
}
51 changes: 30 additions & 21 deletions fastly/dictionary_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const dictionaryItemsPath = "/service/%s/dictionary/%s/items"
type DictionaryItem struct {
CreatedAt *time.Time `mapstructure:"created_at"`
DeletedAt *time.Time `mapstructure:"deleted_at"`
DictionaryID string `mapstructure:"dictionary_id"`
ItemKey string `mapstructure:"item_key"`
ItemValue string `mapstructure:"item_value"`
ServiceID string `mapstructure:"service_id"`
DictionaryID *string `mapstructure:"dictionary_id"`
ItemKey *string `mapstructure:"item_key"`
ItemValue *string `mapstructure:"item_value"`
ServiceID *string `mapstructure:"service_id"`
UpdatedAt *time.Time `mapstructure:"updated_at"`
}

Expand All @@ -24,37 +24,46 @@ type GetDictionaryItemsInput struct {
// DictionaryID is the ID of the dictionary to retrieve items for (required).
DictionaryID string
// Direction is the direction in which to sort results.
Direction string
Direction *string
// Page is the current page.
Page int
Page *int
// PerPage is the number of records per page.
PerPage int
PerPage *int
// ServiceID is the ID of the service (required).
ServiceID string
// Sort is the field on which to sort.
Sort string
Sort *string
}

// GetDictionaryItems returns a ListPaginator for paginating through the resources.
func (c *Client) GetDictionaryItems(i *GetDictionaryItemsInput) *ListPaginator[DictionaryItem] {
return newPaginator[DictionaryItem](c, &listInput{
Direction: i.Direction,
Sort: i.Sort,
Page: i.Page,
PerPage: i.PerPage,
}, fmt.Sprintf(dictionaryItemsPath, i.ServiceID, i.DictionaryID))
input := &listInput{}
if i.Direction != nil {
input.Direction = *i.Direction
}
if i.Sort != nil {
input.Sort = *i.Sort
}
if i.Page != nil {
input.Page = *i.Page
}
if i.PerPage != nil {
input.PerPage = *i.PerPage
}
path := fmt.Sprintf(dictionaryItemsPath, i.ServiceID, i.DictionaryID)
return newPaginator[DictionaryItem](c, input, path)
}

// ListDictionaryItemsInput is used as input to the ListDictionaryItems function.
type ListDictionaryItemsInput struct {
// DictionaryID is the ID of the dictionary to retrieve items for (required).
DictionaryID string
// Direction is the direction in which to sort results.
Direction string
Direction *string
// ServiceID is the ID of the service (required).
ServiceID string
// Sort is the field on which to sort.
Sort string
Sort *string
}

// ListDictionaryItems retrieves all resources. Not suitable for large
Expand Down Expand Up @@ -86,9 +95,9 @@ func (c *Client) ListDictionaryItems(i *ListDictionaryItemsInput) ([]*Dictionary
// CreateDictionaryItemInput is used as input to the CreateDictionaryItem function.
type CreateDictionaryItemInput struct {
// ItemKey is the dictionary item key, maximum 256 characters.
ItemKey string `url:"item_key,omitempty"`
ItemKey *string `url:"item_key,omitempty"`
// ItemValue is the dictionary item value, maximum 8000 characters.
ItemValue string `url:"item_value,omitempty"`
ItemValue *string `url:"item_value,omitempty"`
// ServiceID is the ID of the service (required).
ServiceID string `url:"-"`
// DictionaryID is the ID of the dictionary to retrieve items for (required).
Expand Down Expand Up @@ -220,11 +229,11 @@ type BatchModifyDictionaryItemsInput struct {
// BatchDictionaryItem represents a dictionary item.
type BatchDictionaryItem struct {
// ItemKey is an item key (maximum 256 characters).
ItemKey string `json:"item_key"`
ItemKey *string `json:"item_key"`
// ItemValue is an item value (maximum 8000 characters).
ItemValue string `json:"item_value"`
ItemValue *string `json:"item_value"`
// Operation is a batching operation variant.
Operation BatchOperation `json:"op"`
Operation *BatchOperation `json:"op"`
}

// BatchModifyDictionaryItems bulk updates dictionary items.
Expand Down
Loading

0 comments on commit 4efb289

Please sign in to comment.