Skip to content

can I centralize the response before commit? #2263

Answered by aldas
Knorway asked this question in Q&A
Discussion options

You must be logged in to vote

It depends mostly what behavior this response "container" should have. Does that size field must be size of bytes of data field? Is extraInfo some arbitrary map of something?

What you are looking for is JSON marshalling and struct tags + some helper function. You can make that responseDTO.Data field to be of type interface{} and for each handler set your handler specific struct there (having tags for json fields)

import (
	"github.com/labstack/echo/v4"
	"net/http"
)

func main() {
	e := echo.New()

	e.GET("/", func(ctx echo.Context) error {
		type dataDTO struct {
			Go string `json:"go"`
		}

		type responseDTO = struct {
			Size      int64             `json:"size"`
			ExtraInfo map[string]

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Knorway
Comment options

@aldas
Comment options

aldas Sep 6, 2022
Maintainer

@Knorway
Comment options

Answer selected by Knorway
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants