Skip to main content
Tweeted twitter.com/StackCodeReview/status/1365949907392208898
added 49 characters in body
Source Link

I have a simple task.

  1. Array of people object comes in, up to a thousand.
  2. Need to iterate over it, and create a custom payload for external service.

I have created a working solution, but I would like to see if I am totally off course, and what changes should I make.

I have reduced non-important details from the code (errors, and similar), so it might not work. But the point is to show how it works, and hopefully get some advice.

User struct {
   Name        string  `json:"name"`
   Lastname    string  `json:"lastname"`
 }

func HandleLambda(event events.SQSEvent) {
 var userRecords []map[string]interface{}

 for idx := range event.Records {
  record := event.Records[idx]
  ev, err := e.NewEvent(record.Body)
  
  if err != nil {
    continue
  }

  u, err := repository.NewUser(ev)

  if err != nil {
        continue
  }

  user := make(map[string]interface{})
  user["1"] = u.Name
  user["2"] = u.Lastname

  userRecords = append(userRecords, user)
  }

 uJSON, _ := json.Marshal(userRecords)
 // I send this to external service

 }

External service is using numbers as keys, that's why I need "1" and "2". I do not have control over it. That's the way it works.

I have a simple task.

  1. Array of people object comes in, up to a thousand.
  2. Need to iterate over it, and create a custom payload for external service.

I have created a working solution, but I would like to see if I am totally off course, and what changes should I make.

I have reduced non-important details from the code (errors, and similar), so it might not work. But the point is to show how it works, and hopefully get some advice.

User struct {
   Name        string  `json:"name"`
   Lastname    string  `json:"lastname"`
 }

func HandleLambda(event events.SQSEvent) {
 var userRecords []map[string]interface{}

 for idx := range event.Records {
  record := event.Records[idx]
  ev, err := e.NewEvent(record.Body)

  u, err := repository.NewUser(ev)
  user := make(map[string]interface{})
  user["1"] = u.Name
  user["2"] = u.Lastname

  userRecords = append(userRecords, user)
 }

 uJSON, _ := json.Marshal(userRecords)
 // I send this to external service

 }

External service is using numbers as keys, that's why I need "1" and "2". I do not have control over it. That's the way it works.

I have a simple task.

  1. Array of people object comes in, up to a thousand.
  2. Need to iterate over it, and create a custom payload for external service.

I have created a working solution, but I would like to see if I am totally off course, and what changes should I make.

I have reduced non-important details from the code (errors, and similar), so it might not work. But the point is to show how it works, and hopefully get some advice.

User struct {
   Name        string  `json:"name"`
   Lastname    string  `json:"lastname"`
 }

func HandleLambda(event events.SQSEvent) {
 var userRecords []map[string]interface{}

 for idx := range event.Records {
  record := event.Records[idx]
  ev, err := e.NewEvent(record.Body)
  
  if err != nil {
    continue
  }

  u, err := repository.NewUser(ev)

  if err != nil {
        continue
  }

  user := make(map[string]interface{})
  user["1"] = u.Name
  user["2"] = u.Lastname

  userRecords = append(userRecords, user)
  }

 uJSON, _ := json.Marshal(userRecords)
 // I send this to external service

 }

External service is using numbers as keys, that's why I need "1" and "2". I do not have control over it. That's the way it works.

added 1 character in body
Source Link
ferada
  • 11.1k
  • 25
  • 63

I have a simple task.

  1. Array of people object comes in, up to a thousand.
  2. Need to iterate over it, and create a custom payload for external service.

I have created a working solution, but I would like to see if I am totally off course, and what changes should I make.

I have reduced non-important details from the code (errors, and similar), so it might not work. But the point is to show how it works, and hopefully get some advice.

User struct {
   Name        string  `json:"name"`
   Lastname    string  `json:"lastname"`
 }

func HandleLambda(event events.SQSEvent) {
 var userRecords []map[string]interface{}

 for idx := range event.Records {
  record := event.Records[idx]
  ev, err := e.NewEvent(record.Body)

  u, err := repository.NewUser(ev)
  user := make(map[string]interface{})
  user["1"] = u.Name
  user["2"] = u.Lastname

  userRecords = append(userRecords, user)
 }

 uJSON, _ := json.Marshal(userRecords)
 // I send this to external service

 }

External service is using numbers as keys, thatsthat's why I need "1" and "2". I do not have control over it. That's the way it works.

I have a simple task.

  1. Array of people object comes in, up to a thousand.
  2. Need to iterate over it, and create a custom payload for external service.

I have created a working solution, but I would like to see if I am totally off course, and what changes should I make.

I have reduced non-important details from the code (errors, and similar), so it might not work. But the point is to show how it works, and hopefully get some advice.

User struct {
   Name        string  `json:"name"`
   Lastname    string  `json:"lastname"`
 }

func HandleLambda(event events.SQSEvent) {
 var userRecords []map[string]interface{}

 for idx := range event.Records {
  record := event.Records[idx]
  ev, err := e.NewEvent(record.Body)

  u, err := repository.NewUser(ev)
  user := make(map[string]interface{})
  user["1"] = u.Name
  user["2"] = u.Lastname

  userRecords = append(userRecords, user)
 }

 uJSON, _ := json.Marshal(userRecords)
 // I send this to external service

 }

External service is using numbers as keys, thats why I need "1" and "2". I do not have control over it. That's the way it works.

I have a simple task.

  1. Array of people object comes in, up to a thousand.
  2. Need to iterate over it, and create a custom payload for external service.

I have created a working solution, but I would like to see if I am totally off course, and what changes should I make.

I have reduced non-important details from the code (errors, and similar), so it might not work. But the point is to show how it works, and hopefully get some advice.

User struct {
   Name        string  `json:"name"`
   Lastname    string  `json:"lastname"`
 }

func HandleLambda(event events.SQSEvent) {
 var userRecords []map[string]interface{}

 for idx := range event.Records {
  record := event.Records[idx]
  ev, err := e.NewEvent(record.Body)

  u, err := repository.NewUser(ev)
  user := make(map[string]interface{})
  user["1"] = u.Name
  user["2"] = u.Lastname

  userRecords = append(userRecords, user)
 }

 uJSON, _ := json.Marshal(userRecords)
 // I send this to external service

 }

External service is using numbers as keys, that's why I need "1" and "2". I do not have control over it. That's the way it works.

Source Link

Proper way to map the object

I have a simple task.

  1. Array of people object comes in, up to a thousand.
  2. Need to iterate over it, and create a custom payload for external service.

I have created a working solution, but I would like to see if I am totally off course, and what changes should I make.

I have reduced non-important details from the code (errors, and similar), so it might not work. But the point is to show how it works, and hopefully get some advice.

User struct {
   Name        string  `json:"name"`
   Lastname    string  `json:"lastname"`
 }

func HandleLambda(event events.SQSEvent) {
 var userRecords []map[string]interface{}

 for idx := range event.Records {
  record := event.Records[idx]
  ev, err := e.NewEvent(record.Body)

  u, err := repository.NewUser(ev)
  user := make(map[string]interface{})
  user["1"] = u.Name
  user["2"] = u.Lastname

  userRecords = append(userRecords, user)
 }

 uJSON, _ := json.Marshal(userRecords)
 // I send this to external service

 }

External service is using numbers as keys, thats why I need "1" and "2". I do not have control over it. That's the way it works.