在GoLang中解析嵌入的Struct表单值

时间:2017-05-24 11:31:47

标签: angularjs forms parsing go struct

我有一个包含另一个结构数组的结构,例如

type Struct1 struct {
  Value         string
  Items         []Struct2
}
type Struct2 struct {
  Value         string
}

我使用gorilla架构将我的Form值解码为Struct 1。 嵌入式结构体Struct2的值不会通过。

当我查看FormValue的日志(" Struct2")时,它返回' [对象对象],[对象对象]'

非常感谢任何帮助

修改 表单结构的一个例子,

使用AngularJS,

var data = $scope.struct1;
$http({
      method: 'POST',
        url:url, 
        data : data, 
        headers : {'Content-Type': 'application/x-www-form-urlencoded'},
        transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    }

    })
    .then(function successCallback(response) { 
        console.log(response);                       
    }, function errorCallback(response) {

    });

1 个答案:

答案 0 :(得分:1)

您可能没有为HTML表单设置正确的结构设计。您可能希望发布输入和/或HTML表单设计。

gorilla/schema' s decode期望输入的值作为map[string][]string类型的变量传递,您可以从the example in the documentation和测试包中的文件。这是一个简单的完整脚本,它只包含文档中的示例并打印结果:

package main

import(
    "fmt"
    "github.com/gorilla/schema"
)

type Person struct {
    Name  string
    Phone string
}

func main() {
    values := map[string][]string{
        "Name":  {"John"},
        "Phone": {"999-999-999"},
    }
    person := new(Person)
    decoder := schema.NewDecoder()
    decoder.Decode(person, values)
    fmt.Printf("Person: %v\n", person)
}

输出Person: &{John 999-999-999}

这是map[string][]string类型的地图文字的正确形式,您可以通过执行声明后跟分配并运行它而不会出错来演示:

var values map[string][]string
values = map[string][]string{
    "Name":  {"John"},
    "Phone": {"999-999-999"},
}

现在map[string][]string显然不支持gorilla / schema支持的所有类型,例如你问题中的类型:结构片段。但是处理HTML表单使得翻译有意义:它使用点分隔符附加索引和字段名称以创建所需的结构。因此,对于您在问题中发布的类型,我编写了此脚本以将值解码为结构:

package main

import(
    "fmt"
    "github.com/gorilla/schema"
)

type Struct1 struct {
  Value         string
  Items         []Struct2
}

type Struct2 struct {
  Value         string
}

func main() {
    values := map[string][]string{
        "Value": {"the thing with the items"},
        "Items.0.Value": {"a"},
        "Items.1.Value": {"b"},
        "Items.2.Value": {"c"},
    }
    s1 := new(Struct1)
    decoder := schema.NewDecoder()
    decoder.Decode(s1, values)
    fmt.Printf("S1: %v\n", s1)
}

运行输出:

S1: &{the thing with the items [{a} {b} {c}]}

这表明,如果输入符合该设计,解码可以在没有错误的情况下填充结构设计。

因此,您可能会尝试验证您的输入是否与该方案匹配 - 它具有类似于数组的索引和带有点分隔符的字段名称,其方式符合您的结构设计。如果没有,则表明您的结构设计需要更新以适合您的输入格式。

您可以在the decode_test.go file in the gorilla/schema package中看到解码此类结构的解码示例,例如以下几行:

type Foo struct {
    F01 int
    F02 Bar
    Bif []Baz
}

type Bar struct {
    F01 string
    F02 string
    F03 string
    F14 string
    S05 string
    Str string
}

type Baz struct {
    F99 []string
}

func TestSimpleExample(t *testing.T) {
    data := map[string][]string{
        "F01":       {"1"},
        "F02.F01":   {"S1"},
        "F02.F02":   {"S2"},
        "F02.F03":   {"S3"},
        "F02.F14":   {"S4"},
        "F02.S05":   {"S5"},
        "F02.Str":   {"Str"},
        "Bif.0.F99": {"A", "B", "C"},
}

Foo结构体有一个名为Bif的字段[]BazBaz是一个结构 - 所以我们有一些结构类型,就像你的问题一样。 Baz有一个名为F99的字段。您可以看到输入是使用字符串值"Bif.0.F99"引用的。

在测试文件中使用此示例和其他示例作为指南。