1

I have an app that makes a lot of requests to different urls. Each of them have different request & response structure.

Configurations of the requests types located in Mysql databse.

It contains next data:

  • URL
  • Method
  • Query params
  • Body params
  • Headers
  • Response structure

So while before I was using Node.js for this things it was easy to make solution for this.

But with Go I see only way in using reflect package for this. But reflect harms performance of the app.

Is there a simple way to generate code for this?

Example of request config:

{
  id: 1,
  name: "Req1",
  url: "http://example.com",
  query: [
    {name: "user_id", source: "user", value: "id" },
    {name: "user_ip", source: "user", value: "ip" },
    {name: "token", source: "const", value: "xxx" },
  ],
  headers: [
   {name: "Accept-Language", source: "user", value: "language"}
  ],
  body: [
    {"name": "user.ua", "user", "ua"}
  ]
}

User example:

{
  ip: "127.0.0.1",
  id: "123",
  ua: "User Agent...",
  "language": "en"
}

And in the output should be made next request:

URL: http://example.com?user_id=123&user_ip=127.0.0.1&token=xxx
Headers:
{
  Accept-Language: en
}
Body:
{
  user: {
    ua: "User Agent..."
  }
}

In body might be used path of the param.

Is there a tool for automatically generating this type of code?

Bohdan Srdi
  • 144
  • 1
  • 6
  • You can probably translate your JS code to Go. I don't see anything in the question that requires code generation or reflect. – Charlie Tumahai Jan 24 '22 at 17:49
  • What is your input? If it is json files, there are several packages (like https://github.com/mholt/json-to-go) that can help you. Otherwise you'll have to type them. – Benjamin Barrois Jan 24 '22 at 17:52
  • @CeriseLimón there is different request / response structures. And there is like 50 different, I don't want to write manually code for each type of request. Without reflect I don't see a way to set some "object" value by path. And I know that for better performance it's better to use defined types, so I need to generate those types dynamically. And also the code that will choose the proper type dynamically. – Bohdan Srdi Jan 24 '22 at 19:54
  • Don't create a type for each request. Use map[string]interface{}, []interface{} and so on. – Charlie Tumahai Jan 24 '22 at 19:57
  • @BenjaminBarrois my input is the id of request type, and the user data. Based on this need to build request & parse it's response. I have like 50 types of different requests. Also I know that for better performance it's better to use defined types. So I want to generate those types dinamically & I need to dynamically generate code that will take proper types for given request. – Bohdan Srdi Jan 24 '22 at 19:57
  • @CeriseLimón yes, but it will degrade the performance as far as I know. And there will be billions of requests. Performance must be top notch. That why I switching from Node.js to Go. – Bohdan Srdi Jan 24 '22 at 19:58
  • Unless you already have the evidence, you should not assume that slinging data in and out of request specific types is faster than using a small set of common types. – Charlie Tumahai Jan 24 '22 at 20:09
  • I meant what input do you have for the generation, not for the program. Is it only a JS code? If so you should take your structures and extract them to json and use the project I sent the link of before. Otherwise you can generate models and even http server/client from a Swagger with https://github.com/go-swagger/go-swagger. Anyways you need a well-formated input for your generation, otherwise you'll have to do it with your fingers. – Benjamin Barrois Jan 25 '22 at 13:33

0 Answers0