由于响应结构的微小变化,重复调用JSON解码器。

huangapple go评论116阅读模式
英文:

Calling JSON decoder repetitvely because of small change in response structure

问题

我已经将Github和Google身份验证系统添加到我的Web应用程序中。我希望在这两种情况下都能获取用户的电子邮件地址。我尝试创建一个函数,该函数将进行API请求并获取电子邮件地址。我遇到了一个问题,即Google返回一个JSON对象,而Github返回一个JSON数组作为响应。我无法找到一种方法来避免调用JSON解码器两次,因为我不能为它们都使用相同类型的变量。

  1. // 发送一个请求到API,并通过将HTTP头“Authorization”设置为authHeader值来授权
  2. func getUserEmail(endpoint, authHeader, provider string) (string, error) {
  3. var email string // 在这里存储用户电子邮件
  4. var client http.Client // 创建客户端以便我们可以修改请求头
  5. // 创建一个GET请求到端点
  6. req, err := http.NewRequest("GET", endpoint, nil)
  7. if err != nil {
  8. return "", err
  9. }
  10. // 通过使用HTTP头来授权请求
  11. req.Header.Add("Authorization", authHeader)
  12. // 将数据返回为JSON
  13. req.Header.Add("accept", "application/json")
  14. // 发送请求
  15. resp, err := client.Do(req)
  16. if err != nil {
  17. fmt.Println("Internet connection or client policy error")
  18. return "", err
  19. }
  20. defer resp.Body.Close()
  21. if provider == "google" {
  22. var response map[string]interface{}
  23. err = json.NewDecoder(resp.Body).Decode(&response)
  24. if err != nil {
  25. fmt.Println("Error occured during decoding access token response")
  26. return "", err
  27. }
  28. email = response["email"].(string)
  29. } else if provider == "github" {
  30. var response []map[string]interface{}
  31. err = json.NewDecoder(resp.Body).Decode(&response)
  32. if err != nil {
  33. fmt.Println("Error occured during decoding access token response")
  34. return "", err
  35. }
  36. email = response[0]["email"].(string)
  37. } else {
  38. return "", errors.New("invalid provider")
  39. }
  40. return email, nil
  41. }
英文:

I have added Github and Google authentication system to my web application. I am looking to get user email in both of these cases. I tried to make a one function which would make the API request and get the email.<br>
I ran into an issue when Google returned a JSON object and Github a JSON array as a response. <br>
I cant figure out a way how I could avoid calling the JSON decoder twice as I cant have a same type variable for both of them.

  1. // Sends a request to the API and
  2. // authorizes it by setting HTTP header &quot;Authorization&quot; to authHeader value
  3. func getUserEmail(endpoint, authHeader, provider string) (string, error) {
  4. var email string // Store user email here
  5. var client http.Client // Create client so we can modify request headers
  6. // Create a GET request to the endpoint
  7. req, err := http.NewRequest(&quot;GET&quot;, endpoint, nil)
  8. if err != nil {
  9. return &quot;&quot;, err
  10. }
  11. // Authorize the request by using the HTTP header
  12. req.Header.Add(&quot;Authorization&quot;, authHeader)
  13. // Give the data back as JSON
  14. req.Header.Add(&quot;accept&quot;, &quot;application/json&quot;)
  15. // Send the request
  16. resp, err := client.Do(req)
  17. if err != nil {
  18. fmt.Println(&quot;Internet connection or client policy error&quot;)
  19. return &quot;&quot;, err
  20. }
  21. defer resp.Body.Close()
  22. if provider == &quot;google&quot; {
  23. var response map[string]interface{}
  24. err = json.NewDecoder(resp.Body).Decode(&amp;response)
  25. if err != nil {
  26. fmt.Println(&quot;Error occured during decoding access token response&quot;)
  27. return &quot;&quot;, err
  28. }
  29. email = response[&quot;email&quot;].(string)
  30. } else if provider == &quot;github&quot; {
  31. var response []map[string]interface{}
  32. err = json.NewDecoder(resp.Body).Decode(&amp;response)
  33. if err != nil {
  34. fmt.Println(&quot;Error occured during decoding access token response&quot;)
  35. return &quot;&quot;, err
  36. }
  37. email = response[0][&quot;email&quot;].(string)
  38. } else {
  39. return &quot;&quot;, errors.New(&quot;invalid provider&quot;)
  40. }
  41. return email, nil
  42. }

答案1

得分: 2

你可以将其解组为var response interface{}。一旦JSON解组完成,你可以对response进行类型断言,以检查它是[]interface{}还是map[string]interface{},然后根据情况进行处理。

  1. var email string
  2. var response interface{}
  3. if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
  4. return err
  5. }
  6. // 如果你确定响应内容的结构,在给定其类型的情况下,总是符合你的预期,你可以使用快速且简单的类型切换/断言。
  7. switch v := response.(type) {
  8. case []interface{}:
  9. email = v[0].(map[string]interface{})["email"].(string)
  10. case map[string]interface{}:
  11. email = v["email"].(string)
  12. }
  13. // 但是!如果你不确定,如果API没有提供保证,那么你应该使用逗号-ok惯用法来防止恐慌。
  14. if s, ok := response.([]interface{}); ok && len(s) > 0 {
  15. if m, ok := s[0].(map[string]interface{}); ok && len(m) > 0 {
  16. email, _ = m["email"].(string)
  17. }
  18. } else if m, ok := response.(map[string]interface{}); ok && len(m) > 0 {
  19. email, _ = m["email"].(string)
  20. }

你还可以根据提供者的值预先分配一个指针到预期类型,并将请求体解组到其中,这将减少所需的类型断言的数量,但需要进行指针解引用。

  1. var email string
  2. var response interface{}
  3. if provider == "google" {
  4. response = new(map[string]interface{})
  5. } else if provider == "github" {
  6. response = new([]map[string]interface{})
  7. }
  8. if err := json.NewDecoder(r.Body).Decode(response); err != nil {
  9. return err
  10. }
  11. switch v := response.(type) {
  12. case *[]map[string]interface{}:
  13. email = (*v)[0]["email"].(string) // 不需要断言切片元素的类型
  14. case *map[string]interface{}:
  15. email = (*v)["email"].(string)
  16. }
英文:

You can unmarshal into var response interface{}. Once the json is unmarshaled you can do a type assertion on the response to check if it's []interface{} or map[string]interface{} and go from there.

  1. var email string
  2. var response interface{}
  3. if err := json.NewDecoder(r.Body).Decode(&amp;response); err != nil {
  4. return err
  5. }
  6. // If you are sure that the structure of the content of the response,
  7. // given its type, is always what you expect it to be, you can use a
  8. // quick-and-dirty type switch/assertion.
  9. switch v := response.(type) {
  10. case []interface{}:
  11. email = v[0].(map[string]interface{})[&quot;email&quot;].(string)
  12. case map[string]interface{}:
  13. email = v[&quot;email&quot;].(string)
  14. }
  15. // But! If you&#39;re not sure, if the APIs don&#39;t provide a guarantee,
  16. // then you should guard against panics using the comma-ok idiom
  17. // at every step.
  18. if s, ok := response.([]interface{}); ok &amp;&amp; len(s) &gt; 0 {
  19. if m, ok := s[0].(map[string]interface{}); ok &amp;&amp; len(m) &gt; 0 {
  20. email, _ = m[&quot;email&quot;].(string)
  21. }
  22. } else if m, ok := response.(map[string]interface{}); ok &amp;&amp; len(m) &gt; 0 {
  23. email, _ = m[&quot;email&quot;].(string)
  24. }

You can also pre-allocate a pointer to the expected type based on the provider value and unmarshal the request body into that, this will reduce the number of type assertions necessary but will require pointer-dereferencing.

  1. var email string
  2. var response interface{}
  3. if provider == &quot;google&quot; {
  4. response = new(map[string]interface{})
  5. } else if provider == &quot;github&quot; {
  6. response = new([]map[string]interface{})
  7. }
  8. if err := json.NewDecoder(r.Body).Decode(response); err != nil {
  9. return err
  10. }
  11. switch v := response.(type) {
  12. case *[]map[string]interface{}:
  13. email = (*v)[0][&quot;email&quot;].(string) // no need to assert the slice element&#39;s type
  14. case *map[string]interface{}:
  15. email = (*v)[&quot;email&quot;].(string)
  16. }

huangapple
  • 本文由 发表于 2022年1月27日 15:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70874903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定