Пакетное сообщение Golang через Mux

# #json #api #go #post

Вопрос:

Здравствуйте, я новичок в Golang, и я пытаюсь сделать пакетную публикацию с помощью Mux. Я хочу иметь возможность публиковать несколько «производимых» элементов вместо одного.

Здесь я определяю, что такое элемент продукта

 // Define the produce structure
type Produce struct {
    Name string `json:"name"`
    Code string `json:"code"`
    Unit_Price float64 `json:"unit_price"`
}

// Init produce var as a Produce slice
var produce []Produce
 

Вот мой текущий почтовый индекс

 func addProduce(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var newProduceItem Produce
    _ = json.NewDecoder(r.Body).Decode(amp;newProduceItem)
    re := regexp.MustCompile("^[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}$")
    if re.MatchString(newProduceItem.Code) == true amp;amp; len(newProduceItem.Name) > 0 {
        newProduceItem.Unit_Price = math.Round(newProduceItem.Unit_Price*100) / 100 //rounds to the nearest cent
        produce = append(produce, newProduceItem)
        json.NewEncoder(w).Encode(newProduceItem)
    } else {
        http.Error(w, fmt.Sprintf("Incorrect produce code sequence or product name. Example code sequence: A12T-4GH7-QPL9-3N4M"), http.StatusBadRequest)
    }
}
 

Она вызывается в функции main(), как показано здесь.

 func main() {
    router := mux.NewRouter()
    router.HandleFunc("/produce", addProduce).Methods("POST")
    log.Fatal(http.ListenAndServe(":8000", router))
}
 

Вот пример данных JSON, которые работают, когда я ОТПРАВЛЯЮ их в Postman

 {
    "name":"Peach",
    "code": "TTTT-44D4-A12T-1224",
    "unit_price": 5.3334
}
 

Я хочу иметь возможность одновременно публиковать несколько элементов продукта, таких как….

 [
    {
        "name": "Green Pepper",
        "code": "YRT6-72AS-K736-L4AR",
        "unit_price": 0.79
    },
    {
        "name": "Gala Apple",
        "code": "TQ4C-VV6T-75ZX-1RMR",
        "unit_price": 3.59
    },
]
 

Спасибо

Комментарии:

1. Похоже, вы спрашиваете, как проанализировать массив JSON? (смотрите этот пример ). Изменение var newProduceItem Produce на var newProduceItems []Produce поможет вам пройти большую часть пути.

2. Вам нужно поддерживать оба []Produce и Produce или вы можете просто обновить API, чтобы принять []Produce ?

Ответ №1:

Очевидно, что есть много способов сделать это, вот один

 package main

import (
    "encoding/json"
    "fmt"
    "log"
    "math"
    "net/http"
    "regexp"

    "github.com/gorilla/mux"
)

type Produce struct {
    Name       string  `json:"name"`
    Code       string  `json:"code"`
    Unit_Price float64 `json:"unit_price"`
}

type ProduceList []Produce

// global var where all produce is kept,
// not persistent
var produce ProduceList

func addProduce(w http.ResponseWriter, r *http.Request) {

    // we accept a json and decode it into a slice of structs
    var newProduceItems ProduceList
    err := json.NewDecoder(r.Body).Decode(amp;newProduceItems)
    if err != nil {
        log.Panic(err)
    }

    var tempItems ProduceList
    re := regexp.MustCompile("^[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}$")

    // iterate over each element in the posted json and validate
    // when validated, add to the temporary accumulator
    // if not validated, error out and stop
    for idx, produceItem := range newProduceItems {
        if !re.MatchString(produceItem.Code) || len(produceItem.Name) <= 0 {
            errMsg := fmt.Sprintf("Item %d: Incorrect produce code sequence or product name. Example code sequence: A12T-4GH7-QPL9-3N4M", idx)
            http.Error(w, errMsg, http.StatusBadRequest)
            return
        }

        produceItem.Unit_Price = math.Round(produceItem.Unit_Price*100) / 100 //rounds to the nearest cent
        tempItems = append(tempItems, produceItem)
    }

    // after validation, append new items to the global accumulator and respond back with added items
    produce = append(produce, tempItems...)
    w.Header().Set("Content-Type", "application/json")
    if err = json.NewEncoder(w).Encode(newProduceItems); err != nil {
        log.Panic(err)
    }
}

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/produce", addProduce).Methods("POST")
    log.Fatal(http.ListenAndServe(":8000", router))
}