Skip to content

Commit

Permalink
Refactor JSON unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
darylhjd committed Mar 12, 2021
1 parent b40d314 commit b41dfdf
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 168 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ func main() {
}
```

Refer to each DataType's corresponding file to see what helper functions are available.
As a general rule, most DataTypes support getting the full list of item names as well as
a particular DataEntry of that type.
Refer to each DataType's corresponding file to see what helper functions are available. As a general rule, most
DataTypes support getting the full list of item names as well as a particular DataEntry of that type.

This library provides a wildcard function, `GetCustomBody`, for your own requests. You can find
this function in `api.go`.
This library provides a wildcard function, `GetCustomBody`, for your own requests. You can find this function
in `api.go`.

```golang
package main
Expand Down
14 changes: 2 additions & 12 deletions artifacts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package genshinapi

import "encoding/json"

const (
// API DataType name
ArtifactsDType = "artifacts"
Expand Down Expand Up @@ -35,14 +33,6 @@ func GetArtifact(name string) (Artifact, error) {
}

var artifact Artifact
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return artifact, err
}

err = json.Unmarshal(bytes, &artifact)
if err != nil {
return artifact, err
}
return artifact, nil
err := getDataAndUnmarshal(reqBody, &artifact)
return artifact, err
}
14 changes: 2 additions & 12 deletions characters.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package genshinapi

import "encoding/json"

const (
// API DataType name
CharactersDType = "characters"
Expand Down Expand Up @@ -59,14 +57,6 @@ func GetCharacter(name string) (Character, error) {
}

var character Character
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return character, err
}

err = json.Unmarshal(bytes, &character)
if err != nil {
return character, err
}
return character, nil
err := getDataAndUnmarshal(reqBody, &character)
return character, err
}
14 changes: 2 additions & 12 deletions domains.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package genshinapi

import "encoding/json"

const (
// API DataType name
DomainsDType = "domains"
Expand Down Expand Up @@ -68,14 +66,6 @@ func GetDomain(name string) (Domain, error) {
}

var domain Domain
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return domain, err
}

err = json.Unmarshal(bytes, &domain)
if err != nil {
return domain, err
}
return domain, nil
err := getDataAndUnmarshal(reqBody, &domain)
return domain, err
}
14 changes: 2 additions & 12 deletions elements.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package genshinapi

import "encoding/json"

const (
// API DataType name
ElementsDType = "elements"
Expand Down Expand Up @@ -32,14 +30,6 @@ func GetElement(name string) (Element, error) {
}

var element Element
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return element, err
}

err = json.Unmarshal(bytes, &element)
if err != nil {
return element, err
}
return element, nil
err := getDataAndUnmarshal(reqBody, &element)
return element, err
}
109 changes: 18 additions & 91 deletions materials.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package genshinapi

import (
"encoding/json"
"errors"
)

Expand Down Expand Up @@ -601,16 +600,8 @@ func GetBossMaterial() (Material, error) {
}

var bm BossMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return bm, err
}

err = json.Unmarshal(bytes, &bm)
if err != nil {
return bm, err
}
return bm, nil
err := getDataAndUnmarshal(reqBody, &bm)
return bm, err
}

// GetCharacterAscensionMaterial : Return a CharacterAscensionMaterial struct.
Expand All @@ -621,16 +612,8 @@ func GetCharacterAscensionMaterial() (Material, error) {
}

var cam CharacterAscensionMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return cam, err
}

err = json.Unmarshal(bytes, &cam)
if err != nil {
return cam, err
}
return cam, nil
err := getDataAndUnmarshal(reqBody, &cam)
return cam, err
}

// GetCharacterExperienceMaterial : Return a CharacterExperienceMaterial struct.
Expand All @@ -641,16 +624,8 @@ func GetCharacterExperienceMaterial() (Material, error) {
}

var cem CharacterExperienceMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return cem, err
}

err = json.Unmarshal(bytes, &cem)
if err != nil {
return cem, err
}
return cem, nil
err := getDataAndUnmarshal(reqBody, &cem)
return cem, err
}

// GetCommonAscensionMaterial : Return a CommonAscensionMaterial struct
Expand All @@ -661,16 +636,8 @@ func GetCommonAscensionMaterial() (Material, error) {
}

var coam CommonAscensionMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return coam, err
}

err = json.Unmarshal(bytes, &coam)
if err != nil {
return coam, err
}
return coam, nil
err := getDataAndUnmarshal(reqBody, &coam)
return coam, err
}

// GetLocalSpecialtiesMaterial : Return a LocalSpecialtiesMaterial struct
Expand All @@ -681,16 +648,8 @@ func GetLocalSpecialtiesMaterial() (Material, error) {
}

var lsm LocalSpecialtiesMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return lsm, err
}

err = json.Unmarshal(bytes, &lsm)
if err != nil {
return lsm, err
}
return lsm, nil
err := getDataAndUnmarshal(reqBody, &lsm)
return lsm, err
}

// GetTalentBookMaterial : Return a TalentBookMaterial struct
Expand All @@ -701,16 +660,8 @@ func GetTalentBookMaterial() (Material, error) {
}

var tbookm TalentBookMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return tbookm, err
}

err = json.Unmarshal(bytes, &tbookm)
if err != nil {
return tbookm, err
}
return tbookm, nil
err := getDataAndUnmarshal(reqBody, &tbookm)
return tbookm, err
}

// GetTalentBossMaterial : Return a TalentBossMaterial struct
Expand All @@ -721,16 +672,8 @@ func GetTalentBossMaterial() (Material, error) {
}

var tbossm TalentBossMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return tbossm, err
}

err = json.Unmarshal(bytes, &tbossm)
if err != nil {
return tbossm, err
}
return tbossm, nil
err := getDataAndUnmarshal(reqBody, &tbossm)
return tbossm, err
}

// GetWeaponAscensionMaterial : Return a WeaponAscensionMaterial struct
Expand All @@ -741,16 +684,8 @@ func GetWeaponAscensionMaterial() (Material, error) {
}

var wam WeaponAscensionMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return wam, err
}

err = json.Unmarshal(bytes, &wam)
if err != nil {
return wam, err
}
return wam, nil
err := getDataAndUnmarshal(reqBody, &wam)
return wam, err
}

// GetWeaponExperienceMaterial : Return a WeaponExperienceMaterial struct
Expand All @@ -761,14 +696,6 @@ func GetWeaponExperienceMaterial() (Material, error) {
}

var wem WeaponExperienceMaterial
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return wem, err
}

err = json.Unmarshal(bytes, &wem)
if err != nil {
return wem, err
}
return wem, nil
err := getDataAndUnmarshal(reqBody, &wem)
return wem, err
}
14 changes: 2 additions & 12 deletions nations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package genshinapi

import "encoding/json"

const (
// API DataType name
NationsDType = "nations"
Expand Down Expand Up @@ -35,14 +33,6 @@ func GetNation(name string) (Nation, error) {
}

var nation Nation
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return nation, err
}

err = json.Unmarshal(bytes, &nation)
if err != nil {
return nation, err
}
return nation, nil
err := getDataAndUnmarshal(reqBody, &nation)
return nation, err
}
13 changes: 13 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ type DataEntry interface {
EntryName() string
}

// getDataAndUnmarshal : Get required data from API and unmarshal to required struct.
// The function accepts a pointer to a DataEntry struct (matptr).
// If not supported, function will return an error.
func getDataAndUnmarshal(req []string, matptr DataEntry) error {
bytes, err := GetCustomBody(req...)
if err != nil {
return err
}

err = json.Unmarshal(bytes, matptr)
return err
}

// DataTypes : Struct to hold list of data types provided by API.
// This is a struct because the API returns a JSON containing the list, rather than just a list.
type DataTypes struct {
Expand Down
14 changes: 2 additions & 12 deletions weapons.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package genshinapi

import "encoding/json"

const (
// API DataType name
WeaponsDType = "weapons"
Expand Down Expand Up @@ -39,14 +37,6 @@ func GetWeapon(name string) (Weapon, error) {
}

var weapon Weapon
bytes, err := GetCustomBody(reqBody...)
if err != nil {
return weapon, err
}

err = json.Unmarshal(bytes, &weapon)
if err != nil {
return weapon, err
}
return weapon, nil
err := getDataAndUnmarshal(reqBody, &weapon)
return weapon, err
}

0 comments on commit b41dfdf

Please sign in to comment.