Henry-Sarabia / igdb

Go client for the Internet Game Database API
https://api.igdb.com/
MIT License
81 stars 17 forks source link

Expanding fields breaks JSON decoding #32

Closed Henry-Sarabia closed 5 years ago

Henry-Sarabia commented 5 years ago

The IGDB API now supports field expansion. Field expansion allows a user to denote subfields to retrieve in addition to the regular fields of a request. Because Go is a statically typed language, when additional subfields are sent back in the JSON object, the object will no longer match the defined struct and the JSON decoder will return an error.

Here are three options for dealing with this problem.

  1. Ignore the issue and simply let the package return a JSON decoder error when expansion is used.
  2. Take a half-measure and fail early. Document the lack of expansion support and check for any expanded fields before sending the request. If an expanded field is found, return an error explaining the lack of expansion support.
  3. Take a half-measure and return an untyped object. Add an extra function for each endpoint that allows expansion but will return an untyped map of the JSON object instead of a proper populated Go struct type.
Henry-Sarabia commented 5 years ago

As of now, I am taking option 1 off the table - there needs to be some sort of sane documented behavior. That leaves options 2 and 3.

Option 2 is the simplest, but the package will be less valuable to users who need some sort of expansion support.

Option 3 does give the aforementioned users some sort of recourse, but requires vastly increasing the surface area of the package's public API. There is, however, a compromise that could be made. A new, single Client function can be introduced that will take an endpoint and return the untyped map of the JSON object. This function, perhaps named RawGet or UnsafeGet, provides a sort of escape hatch but gives no guarantees about the returned objects' contents.

Henry-Sarabia commented 5 years ago

For now, I will implement option 2 until I can figure out exactly how I want to handle the unsafe get function.