只对批准的领域进行非军事化处理

问题描述 投票:0回答:1

我试图给用户提供编辑结构中某些字段的权限,这些字段将根据动作、上下文和当前用户的角色而变化。

目前,我正在用一种强制的方式来做这件事,但它是相当繁琐的,而且不可扩展。

我认为有一个 approved_fields 可能是一个很好的、可扩展性更强的解决方案,但我不知道如何去完成这个任务。我觉得反思是最好的方式,但我还不知道。

任何灵感都会有帮助。

type Foo struct {
  Bar1 int
  Bar2 int
  Bar3 int
}

foo := Foo{}
approved_fields := []string{"Bar1", "Bar2"}
decode(json_data, &foo, approved_fields)

foo2 := Foo{}
approved_fields := []string{"Bar1", "Bar3"}
decode(json_data, &foo2, approved_fields)
json go reflection unmarshalling
1个回答
0
投票

这是我想解决的方法。可能会有一些更有效的方法,因为我在选择字段之前,先对整个项目进行解集。

import (
    "encoding/json"
    "log"
    "reflect"

    "github.com/pkg/errors"
)

func fieldByName(s string, v interface{}) (reflect.Value, error) {
    el := reflect.ValueOf(v).Elem()
    fbn := el.FieldByName(s)
    if !fbn.IsValid() {
        return fbn, errors.Errorf("does not have field named %s", s)
    }
    return fbn, nil
}

func decode(data []byte, v interface{}, approvedFields []string) error {
    typeOf := reflect.TypeOf(v)
    if typeOf.Kind() == reflect.Ptr {
        typeOf = typeOf.Elem()
    }
    if typeOf.Kind() == reflect.Slice {
        return errors.New("does not support slices")
    }

    newItem := reflect.New(typeOf)
    newItemInterface := newItem.Interface()
    if err := json.Unmarshal(data, &newItemInterface); err != nil {
        return errors.Wrap(err, "json unmarshall")
    }
    for _, approvedField := range approvedFields {
        fbn, err := fieldByName(approvedField, v)
        if err != nil {
            return errors.Wrap(err, "field by name")
        }
        val, _ := fieldByName(approvedField, newItemInterface)
        fbn.Set(val)
    }
    return nil
}

一个测试。

func TestBar1Bar2(t *testing.T) {
    var json_data []byte
    {
        f := &Foo{
            Bar1: 1,
            Bar2: 2,
            Bar3: 3,
        }
        json_data, _ = json.Marshal(f)
    }
    approved_fields := []string{"Bar1", "Bar2"}
    f := &Foo{}
    err := decode(json_data, f, approved_fields)
    if err != nil {
        t.Fatal(err)
    }
    assert.Equal(t, f.Bar1, 1)
    assert.Equal(t, f.Bar2, 2)
    assert.Equal(t, f.Bar3, 0)
}
© www.soinside.com 2019 - 2024. All rights reserved.