Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON tags added to types #42

Open
moserke opened this issue Feb 13, 2023 · 3 comments
Open

JSON tags added to types #42

moserke opened this issue Feb 13, 2023 · 3 comments

Comments

@moserke
Copy link
Contributor

moserke commented Feb 13, 2023

When consuming the API there are times one might want to marshal the go types. For example when using the go sdk to get the request feed, marshal, then log the marshaled payloads. When doing this however you end up with different marshaled json to a call directly to the API with "accept/json". This is due to the go types not having json tags.

A slimmed down example. The go type:

type Request struct {
	ID                string
	ServerHostname    string
	RemoteIP          string
	RemoteHostname    string
}

Would generate marshaled json as

{
  "ID": "1234567",
  "ServerHostname": "host.name",
  "RemoteIP": "127.0.0.1",
  "RemoteHostname": "private network host"
}

However hitting the API directly would give you a json encoded response of

{
  "id": "1234567",
  "serverHostname": "host.name",
  "remoteIP": "127.0.0.1",
  "remoteHostname": "private network host"
}

Tags should be added to the go struct to match the API encoding. Like this

type Request struct {
	ID                string `json:"id"`
	ServerHostname    string `json:"serverHostname"`
	RemoteIP          string `json:"remoteIP"`
	RemoteHostname    string `json:"remoteHostname"
}
@moserke
Copy link
Contributor Author

moserke commented Feb 13, 2023

One can go through and "alias" your struct with tags and then type cast onto the alias but this is fragile and does require a consistent check of https://docs.fastly.com/signalsciences/api/#_corps__corpName__sites__siteName__feed_requests_get to make sure there are no changes.

@shawnps
Copy link
Contributor

shawnps commented Feb 14, 2023

Thanks for the issue @moserke -- I created a PR here which adds the missing JSON struct tags:

#43

@moserke
Copy link
Contributor Author

moserke commented Feb 14, 2023

Looks great, thanks so much for the fast turn around!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants