Skip to content

Commit

Permalink
fix(go): fix asset unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Mar 5, 2024
1 parent 7ae239f commit 741a322
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 59 deletions.
7 changes: 2 additions & 5 deletions go/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,8 @@ func (a *Asset) ToPublic() *PublicAsset {
return nil
}
return &PublicAsset{
Type: "asset",
ID: a.ID,
URL: a.URL,
ContentType: a.ContentType,
ArchiveExtractionStatus: a.ArchiveExtractionStatus,
Type: "asset",
Asset: *a,
}
}

Expand Down
48 changes: 2 additions & 46 deletions go/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,54 +193,10 @@ func (r PublicAPIListResponse[T]) HasNext() bool {
}

type PublicAsset struct {
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Files []string `json:"files,omitempty"`
ContentType string `json:"contentType,omitempty"`
ArchiveExtractionStatus string `json:"archiveExtractionStatus,omitempty"`
Type string `json:"type,omitempty"`
Asset
}

func (a PublicAsset) IsExtractionDone() bool {
return a.ArchiveExtractionStatus == "done"
}

func PublicAssetFrom(a any) *PublicAsset {
if a == nil {
return nil
}
if a, ok := a.(PublicAsset); ok {
return &a
}
if a, ok := a.(*PublicAsset); ok {
return a
}

m, ok := a.(map[string]any)
if !ok {
return nil
}

aa := PublicAsset{}
if v, ok := m["type"].(string); ok {
aa.Type = v
}

if v, ok := m["id"].(string); ok {
aa.ID = v
}

if v, ok := m["url"].(string); ok {
aa.URL = v
}

if v, ok := m["contentType"].(string); ok {
aa.ContentType = v
}

if v, ok := m["archiveExtractionStatus"].(string); ok {
aa.ArchiveExtractionStatus = v
}

return &aa
}
15 changes: 7 additions & 8 deletions go/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ func TestPublicAPIClient_GetAsset(t *testing.T) {
defer httpmock.Deactivate()

httpmock.RegisterResponder("GET", "https://example.com/api/p/ppp/assets/aaa", lo.Must(httpmock.NewJsonResponder(http.StatusOK, map[string]any{
"id": "aaa",
"url": "https://example.com",
"files": []string{"https://example.com/a.txt", "https://example.com/b.txt"},
"type": "asset",
"id": "aaa",
"url": "https://example.com",
})))
httpmock.RegisterResponder("GET", "https://example.com/api/p/ppp/assets/aaa2", lo.Must(httpmock.NewJsonResponder(http.StatusNotFound, nil)))

Expand All @@ -147,11 +147,10 @@ func TestPublicAPIClient_GetAsset(t *testing.T) {
res, err := c.GetAsset(ctx, "ppp", "aaa")
assert.NoError(t, err)
assert.Equal(t, &PublicAsset{
ID: "aaa",
URL: "https://example.com",
Files: []string{
"https://example.com/a.txt",
"https://example.com/b.txt",
Type: "asset",
Asset: Asset{
ID: "aaa",
URL: "https://example.com",
},
}, res)

Expand Down
14 changes: 14 additions & 0 deletions go/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,17 @@ func getValues[T any](v *Value) []T {

return nil
}

func PublicAssetFrom(a any) *PublicAsset {
j, err := json.Marshal(a)
if err != nil {
return nil
}

pa := PublicAsset{}
if err := json.Unmarshal(j, &pa); err != nil {
return nil
}

return &pa
}

0 comments on commit 741a322

Please sign in to comment.