Skip to content

Commit

Permalink
Use gabs package to improve parsing of json data
Browse files Browse the repository at this point in the history
  • Loading branch information
zeddo123 committed Nov 19, 2021
1 parent dd6b5ea commit a217e34
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 26 deletions.
51 changes: 37 additions & 14 deletions editions.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package gol

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/Jeffail/gabs/v2"
)

type Book map[string]interface{}
type Book struct {
Container
Authors []string
}

// GetEdition returns a book from its open library id
func GetEdition(olid string) (b Book, err error) {
Expand All @@ -21,11 +25,16 @@ func GetEdition(olid string) (b Book, err error) {

defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(bodyBytes, &b)
b.Container, err = gabs.ParseJSON(bodyBytes)
if err != nil {
return b, err
}

if error, ok := b["error"]; ok {
return b, fmt.Errorf("GetEdition: Error fetching book; %s", error)
// verify if an error field is present in the returned data
if err := HasError(b.Container); err != nil {
return b, err
}

return
}

Expand All @@ -50,23 +59,37 @@ func GetEditionISBN(isbnid string) (b Book, err error) {

defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(bodyBytes, &b)
if error, ok := b["error"]; ok {
return b, fmt.Errorf("Book/Edition not found; %s", error)
b.Container, err = gabs.ParseJSON(bodyBytes)
if err != nil {
return b, err
}
if err != nil {
return b, err
}

// verify if an error field is present in the returned data
if err := HasError(b.Container); err != nil {
return b, err
}

return
}

/*
// KeyAuthors returns array of all authors keys
func (b Book) KeyAuthors() []string {
a := make([]string, len(b.AuthorsKey))
for i, AuthorKey := range b.AuthorsKey {
a[i] = AuthorKey.Key[9:]
func (b *Book) KeyAuthors() (err error) {
for _, child := range b.S("authors").Children() {
for _, v := range child.ChildrenMap() {
b.Authors = append(b.Authors, v.Data().(string))
}
}

if len(b.Authors) == 0 {
return fmt.Errorf("Could not find any authors")
}
return a
return
}

/*
// Authors returns all the information related to the book's authors
func (b Book) Authors() (a []Author, err error) {
return Authors(b)
Expand Down
31 changes: 25 additions & 6 deletions editions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,32 @@ func TestGetEditionISBN(t *testing.T) {
})
}
}
func TestEditionKeyAuthors(t *testing.T) {
t.Parallel()
t.Run("Existing Author", func(t *testing.T) {
t.Parallel()
b, _ := gol.GetEdition("OL4554174M")
ok := b.KeyAuthors()
if ok != nil {
t.Fatalf("KeyAuthors() did not return authors for an existing book")
}
})

//func TestEditionKeyAuthors(t *testing.T) {
//a := b.KeyAuthors()
//if !cmp.Equal(a, []string{"OL236174A"}) {
//t.Errorf("Unexpected returned array. Expecting [OL18295A] got %v", a)
//}
//}
t.Run("Non-Existing Author", func(t *testing.T) {
t.Parallel()
b, _ := gol.GetEdition("OL0000000M")
ok := b.KeyAuthors()
if ok == nil {
t.Fatalf("KeyAuthors() did not return an error for non existing book")
}
})

//if !cmp.Equal(a, []string{"OL236174A"}) {
// t.Errorf("Unexpected returned array. Expecting [OL18295A] got %v", a)
//}
}

/*
//func TestEditionAuthors(t *testing.T) {
//tr, err := editions[0].Authors()
//if err != nil {
Expand All @@ -53,3 +71,4 @@ func TestGetEditionISBN(t *testing.T) {
//t.Errorf("Expected set of authors incorrect")
//}
//}
*/
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/Open-pi/gol

go 1.16

require github.com/google/go-cmp v0.5.5
require github.com/Jeffail/gabs/v2 v2.6.1
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/Jeffail/gabs/v2 v2.6.1 h1:wwbE6nTQTwIMsMxzi6XFQQYRZ6wDc1mSdxoAN+9U4Gk=
github.com/Jeffail/gabs/v2 v2.6.1/go.mod h1:xCn81vdHKxFUuWWAaD5jCTQDNPBMh5pPs9IJ+NcziBI=
17 changes: 16 additions & 1 deletion gol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@ gol uses the WorkAPI, the EditionAPI, and the CoverAPI
*/
package gol

type JsonData map[string]interface{}
import (
"fmt"

"github.com/Jeffail/gabs/v2"
)

type Container = *gabs.Container

func HasError(data Container) error {

// verify if an error field is present in the returned data
if err, ok := data.Path("error").Data().(string); ok {
return fmt.Errorf("Error fetching data; %s", err)
}
return nil
}

0 comments on commit a217e34

Please sign in to comment.