source

구조에서 필드 제거 또는 JSON 응답에서 필드 숨기기

lovecheck 2023. 3. 13. 20:35
반응형

구조에서 필드 제거 또는 JSON 응답에서 필드 숨기기

Go에서 API를 만들었습니다.이 API는 호출을 받으면 쿼리를 실행하고 구조체의 인스턴스를 작성한 후 해당 구조를 JSON으로 인코딩한 후 호출자에게 반송합니다.이제 발신자가 "fields" GET 파라미터를 전달함으로써 반환할 특정 필드를 선택할 수 있도록 하겠습니다.

즉, 필드 값에 따라 구조가 변경됩니다.구조에서 필드를 제거하는 방법이 있습니까?또는 적어도 동적으로 그것들을 JSON 응답에 숨길 수 있습니까?(주의: 빈 값이 있을 수 있기 때문에 JSON impt Empty 태그가 작동하지 않을 수 있습니다) 둘 다 가능하지 않은 경우, 이를 처리하는 더 나은 방법이 있습니까?

사용하고 있는 구조의 작은 버전은 다음과 같습니다.

type SearchResult struct {
    Date        string      `json:"date"`
    IdCompany   int         `json:"idCompany"`
    Company     string      `json:"company"`
    IdIndustry  interface{} `json:"idIndustry"`
    Industry    string      `json:"industry"`
    IdContinent interface{} `json:"idContinent"`
    Continent   string      `json:"continent"`
    IdCountry   interface{} `json:"idCountry"`
    Country     string      `json:"country"`
    IdState     interface{} `json:"idState"`
    State       string      `json:"state"`
    IdCity      interface{} `json:"idCity"`
    City        string      `json:"city"`
} //SearchResult

type SearchResults struct {
    NumberResults int            `json:"numberResults"`
    Results       []SearchResult `json:"results"`
} //type SearchResults

그런 다음 다음과 같이 응답을 인코딩 및 출력합니다.

err := json.NewEncoder(c.ResponseWriter).Encode(&msg)

이 질문은 발신자가 제공한 필드 목록을 기반으로 동적으로 필드를 선택하도록 요구하는 것입니다.이것은 정적으로 정의된 json 구조 태그에서는 수행할 수 없습니다.

필드를 항상 json-encode로 건너뛰는 경우는, 물론,json:"-"필드를 무시합니다.(또한 필드가 내보내기되지 않은 경우에는 필수가 아닙니다.이 필드들은 항상 json 인코더에 의해 무시됩니다.)이건 질문이 아니에요.

「 」의 합니다.json:"-"★★★★

]json:"-"[답변]은 검색에서 여기서 끝나는 대부분의 사용자가 원하는 답변이지만 질문에 대한 답변은 아닙니다.

★★★★★★★★★★★★★★★★★★.map[string]interface{}이 경우에는 구조물 대신.를 쉽게 할 수 .delete삭제할 필드를 맵에 내장합니다.

즉, 요청된 필드에 대해서만 쿼리할 수 없는 경우입니다.

json:"-"를 사용합니다.

// Field is ignored by this package.
Field int `json:"-"`

// Field appears in JSON as key "myName".
Field int `json:"myName"`

// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`

// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"`

문서 : http://golang.org/pkg/encoding/json/ #Marshal

이를 위한 또 다른 방법은 다음과 같은 포인터 구조를 갖는 것입니다.,omitempty태그. 포인터가 0이면 필드가 마샬링되지 않습니다.

이 방법에서는 추가 반영이나 맵의 비효율적인 사용이 필요하지 않습니다.

다음 방법을 사용하는 jorelli와 동일한 예:http://play.golang.org/p/JJNa0m2_nw

.reflect하여 필드 를 반영하여 할 수 .json값을 태그합니다.하여 SearchResults로 를 SearchResults 합니다.map[string]interface{}그 후 SearchResults 구조체 대신 정렬합니다.다음은 이 방법을 정의하는 방법의 예입니다.

func fieldSet(fields ...string) map[string]bool {
    set := make(map[string]bool, len(fields))
    for _, s := range fields {
        set[s] = true
    }
    return set
}

func (s *SearchResult) SelectFields(fields ...string) map[string]interface{} {
    fs := fieldSet(fields...)
    rt, rv := reflect.TypeOf(*s), reflect.ValueOf(*s)
    out := make(map[string]interface{}, rt.NumField())
    for i := 0; i < rt.NumField(); i++ {
        field := rt.Field(i)
        jsonKey := field.Tag.Get("json")
        if fs[jsonKey] {
            out[jsonKey] = rv.Field(i).Interface()
        }
    }
    return out
}

이 메서드를 어떻게 호출하고 선택 항목을 정리할지를 보여주는 실행 가능한 솔루션을 소개합니다.http://play.golang.org/p/1K9xjQRnO8

방금 보안관을 발표했는데 구조 필드에 주석이 달린 태그를 기반으로 구조물을 지도로 변환하는 거야생성된 맵을 정렬(JSON 또는 기타)할 수 있습니다.발신자가 요청한 필드 집합만 직렬화할 수는 없지만, 그룹 집합을 사용하면 대부분의 사례를 다룰 수 있을 것입니다.필드 대신 그룹을 직접 사용하면 캐시 기능도 향상될 수 있습니다.

예:

package main

import (
    "encoding/json"
    "fmt"
    "log"

    "github.com/hashicorp/go-version"
    "github.com/liip/sheriff"
)

type User struct {
    Username string   `json:"username" groups:"api"`
    Email    string   `json:"email" groups:"personal"`
    Name     string   `json:"name" groups:"api"`
    Roles    []string `json:"roles" groups:"api" since:"2"`
}

func main() {
    user := User{
        Username: "alice",
        Email:    "alice@example.org",
        Name:     "Alice",
        Roles:    []string{"user", "admin"},
    }

    v2, err := version.NewVersion("2.0.0")
    if err != nil {
        log.Panic(err)
    }

    o := &sheriff.Options{
        Groups:     []string{"api"},
        ApiVersion: v2,
    }

    data, err := sheriff.Marshal(o, user)
    if err != nil {
        log.Panic(err)
    }

    output, err := json.MarshalIndent(data, "", "  ")
    if err != nil {
        log.Panic(err)
    }
    fmt.Printf("%s", output)
}

다음 세 가지 성분을 섭취하십시오.

  1. reflect패키지는 구조체의 모든 필드를 루프합니다.

  2. if to pick up the (필드를 픽업하는 )Marshal , 그리고.

  3. encoding/json로로로 Marshal당신이 좋아하는 분야.

준비:

  1. 잘 섞어주세요.reflect.TypeOf(your_struct).Field(i).Name()i'1'의 your_struct.

  2. reflect.ValueOf(your_struct).Field(i)Value의 표현i'1'의 your_struct.

  3. fieldValue.Interface()type interface {})의 실제 값( interface 을합니다.fieldValue입입 of Value(브래킷 사용:Interface() 메서드에 의해 생성됩니다).interface{}

이 과정에서 다행히 트랜지스터나 회로 차단기를 태우지 않으면 다음과 같은 결과를 얻을 수 있습니다.

func MarshalOnlyFields(structa interface{},
    includeFields map[string]bool) (jsona []byte, status error) {
    value := reflect.ValueOf(structa)
    typa := reflect.TypeOf(structa)
    size := value.NumField()
    jsona = append(jsona, '{')
    for i := 0; i < size; i++ {
        structValue := value.Field(i)
        var fieldName string = typa.Field(i).Name
        if marshalledField, marshalStatus := json.Marshal((structValue).Interface()); marshalStatus != nil {
            return []byte{}, marshalStatus
        } else {
            if includeFields[fieldName] {
                jsona = append(jsona, '"')
                jsona = append(jsona, []byte(fieldName)...)
                jsona = append(jsona, '"')
                jsona = append(jsona, ':')
                jsona = append(jsona, (marshalledField)...)
                if i+1 != len(includeFields) {
                    jsona = append(jsona, ',')
                }
            }
        }
    }
    jsona = append(jsona, '}')
    return
}

서비스:

map[string]bool,

type magic struct {
    Magic1 int
    Magic2 string
    Magic3 [2]int
}

func main() {
    var magic = magic{0, "tusia", [2]int{0, 1}}
    if json, status := MarshalOnlyFields(magic, map[string]bool{"Magic1": true}); status != nil {
        println("error")
    } else {
        fmt.Println(string(json))
    }

}

맛있게 드세요!

일부 필드를 무시하여 구조를 JSON 문자열로 변환하기 위해 이 함수를 만들었습니다.도움이 되길 바랍니다.

func GetJSONString(obj interface{}, ignoreFields ...string) (string, error) {
    toJson, err := json.Marshal(obj)
    if err != nil {
        return "", err
    }

    if len(ignoreFields) == 0 {
        return string(toJson), nil
    }

    toMap := map[string]interface{}{}
    json.Unmarshal([]byte(string(toJson)), &toMap)

    for _, field := range ignoreFields {
        delete(toMap, field)
    }

    toJson, err = json.Marshal(toMap)
    if err != nil {
        return "", err
    }
    return string(toJson), nil
}

예: https://play.golang.org/p/nmq7MFF47Gp

태그 지정 속성 "omitifempty"를 사용하거나 선택적 필드 포인터를 만들고 건너뛸 필드를 초기화하지 않은 상태로 둘 수 있습니다.

같은 문제가 아니라 비슷한 문제가 있었어요.물론 성능 문제를 신경 쓰지 않는다면 아래 코드를 통해 문제를 해결할 수 있습니다.이러한 솔루션을 시스템에 구현하기 전에 가능하면 구조를 재설계할 것을 권장합니다.가변 구조 응답을 보내는 것은 과도한 엔지니어링입니다.응답 구조는 요청과 리소스 간의 계약을 나타내며 종속 요청이 되어서는 안 된다고 생각합니다.(원하지 않는 필드는 무효로 할 수 있습니다).경우에 따라서는, 이 설계를 실장할 필요가 있는 경우도 있습니다.이 경우, 사용하고 있는 플레이 링크와 코드를 나타냅니다.

type User2 struct {
    ID       int    `groups:"id" json:"id,omitempty"`
    Username string `groups:"username" json:"username,omitempty"`
    Nickname string `groups:"nickname" json:"nickname,omitempty"`
}

type User struct {
    ID       int    `groups:"private,public" json:"id,omitempty"`
    Username string `groups:"private" json:"username,omitempty"`
    Nickname string `groups:"public" json:"nickname,omitempty"`
}

var (
    tagName = "groups"
)

//OmitFields sets fields nil by checking their tag group value and access control tags(acTags)
func OmitFields(obj interface{}, acTags []string) {
    //nilV := reflect.Value{}
    sv := reflect.ValueOf(obj).Elem()
    st := sv.Type()
    if sv.Kind() == reflect.Struct {
        for i := 0; i < st.NumField(); i++ {
            fieldVal := sv.Field(i)
            if fieldVal.CanSet() {
                tagStr := st.Field(i).Tag.Get(tagName)
                if len(tagStr) == 0 {
                    continue
                }
                tagList := strings.Split(strings.Replace(tagStr, " ", "", -1), ",")
                //fmt.Println(tagList)
                // ContainsCommonItem checks whether there is at least one common item in arrays
                if !ContainsCommonItem(tagList, acTags) {
                    fieldVal.Set(reflect.Zero(fieldVal.Type()))
                }
            }
        }
    }
}

//ContainsCommonItem checks if arrays have at least one equal item
func ContainsCommonItem(arr1 []string, arr2 []string) bool {
    for i := 0; i < len(arr1); i++ {
        for j := 0; j < len(arr2); j++ {
            if arr1[i] == arr2[j] {
                return true
            }
        }
    }
    return false
}
func main() {
    u := User{ID: 1, Username: "very secret", Nickname: "hinzir"}
    //assume authenticated user doesn't has permission to access private fields
    OmitFields(&u, []string{"public"}) 
    bytes, _ := json.Marshal(&u)
    fmt.Println(string(bytes))


    u2 := User2{ID: 1, Username: "very secret", Nickname: "hinzir"}
    //you want to filter fields by field names
    OmitFields(&u2, []string{"id", "nickname"}) 
    bytes, _ = json.Marshal(&u2)
    fmt.Println(string(bytes))

}

제 구조를 이렇게 정의했습니다.

type User struct {
    Username string  `json:"username" bson:"username"`
    Email    string  `json:"email" bson:"email"`
    Password *string `json:"password,omitempty" bson:"password"`
    FullName string  `json:"fullname" bson:"fullname"`
}

그리고 내 함수 세트 안에user.Password = nil마샬이 되지 않기 위해서요

저도 이 문제에 직면했습니다.처음에는 http 핸들러의 응답을 특화하려고 했습니다.저의 첫 번째 접근법은 구조체의 정보를 다른 구조체에 복사하는 패키지를 만들고 두 번째 구조물을 분류하는 것이었습니다.저는 리플렉션을 사용하여 패키지를 작성했습니다.그래서 그 접근법이 마음에 들지 않았고 역동적이지도 않았습니다.

그래서 encoding/json 패키지를 수정하기로 했습니다.기능Marshal,MarshalIndent그리고.(Encoder) Encode추가 수신

type F map[string]F

맵에 있는 필드만 매핑할 수 있도록 정리하는 데 필요한 필드의 JSON을 시뮬레이트하려고 했습니다.

https://github.com/jtorz/jsont

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/jtorz/jsont/v2"
)

type SearchResult struct {
    Date        string      `json:"date"`
    IdCompany   int         `json:"idCompany"`
    Company     string      `json:"company"`
    IdIndustry  interface{} `json:"idIndustry"`
    Industry    string      `json:"industry"`
    IdContinent interface{} `json:"idContinent"`
    Continent   string      `json:"continent"`
    IdCountry   interface{} `json:"idCountry"`
    Country     string      `json:"country"`
    IdState     interface{} `json:"idState"`
    State       string      `json:"state"`
    IdCity      interface{} `json:"idCity"`
    City        string      `json:"city"`
} //SearchResult

type SearchResults struct {
    NumberResults int            `json:"numberResults"`
    Results       []SearchResult `json:"results"`
} //type SearchResults
func main() {
    msg := SearchResults{
        NumberResults: 2,
        Results: []SearchResult{
            {
                Date:        "12-12-12",
                IdCompany:   1,
                Company:     "alfa",
                IdIndustry:  1,
                Industry:    "IT",
                IdContinent: 1,
                Continent:   "america",
                IdCountry:   1,
                Country:     "México",
                IdState:     1,
                State:       "CDMX",
                IdCity:      1,
                City:        "Atz",
            },
            {
                Date:        "12-12-12",
                IdCompany:   2,
                Company:     "beta",
                IdIndustry:  1,
                Industry:    "IT",
                IdContinent: 1,
                Continent:   "america",
                IdCountry:   2,
                Country:     "USA",
                IdState:     2,
                State:       "TX",
                IdCity:      2,
                City:        "XYZ",
            },
        },
    }
    fmt.Println(msg)
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

        //{"numberResults":2,"results":[{"date":"12-12-12","idCompany":1,"idIndustry":1,"country":"México"},{"date":"12-12-12","idCompany":2,"idIndustry":1,"country":"USA"}]}
        err := jsont.NewEncoder(w).Encode(msg, jsont.F{
            "numberResults": nil,
            "results": jsont.F{
                "date":       nil,
                "idCompany":  nil,
                "idIndustry": nil,
                "country":    nil,
            },
        })
        if err != nil {
            log.Fatal(err)
        }
    })

    http.ListenAndServe(":3009", nil)
}

지금은 조금 오래된 질문이지만, 조금 전에 같은 문제에 부딪혔고, 쉽게 할 수 있는 방법이 없어 이 목적을 충족시키는 도서관을 만들었습니다.이 기능을 통해 쉽게 데이터를 생성할 수 있습니다.map[string]interface{}정적인 구조에서요.

https://github.com/tuvistavie/structomap

Chhaileng 응답을 확장하기 위해 재귀가 있는 필드의 모든 항목을 제거하는 버전이 있습니다.

// GetJSONWithOutFields - Description: return a string representation of an interface with specified fields removed
func GetJSONWithOutFields(obj interface{}, ignoreFields ...string) (string, error) {
    toJson, err := json.Marshal(obj)
    if err != nil {
        return "", err
    }

    if len(ignoreFields) == 0 {
        return string(toJson), nil
    }

    toMap := map[string]interface{}{}
    err = json.Unmarshal(toJson, &toMap)
    if err != nil {
        return "", err
    }

    for _, field := range ignoreFields {
        DeleteField(toMap, field)
    }

    toJson, err = json.Marshal(toMap)
    if err != nil {
        return "", err
    }
    return string(toJson), nil
}

// DeleteField - Description: recursively delete field
func DeleteField(toMap map[string]interface{}, field string) {
    delete(toMap, field)
    for _, v := range toMap {
        if m, isMap := v.(map[string]interface{}); isMap {
            DeleteField(m, field)
        }
    }
}

언급URL : https://stackoverflow.com/questions/17306358/removing-fields-from-struct-or-hiding-them-in-json-response

반응형