gpt4 book ai didi

angularjs - 在 GoLang 中解析嵌入式结构形式的值

转载 作者:IT王子 更新时间:2023-10-29 02:11:52 25 4
gpt4 key购买 nike

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

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

我正在使用 gorilla 模式将我的 Form 值解码为 Struct 1。嵌入式结构 Struct 2 的值未通过。

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

任何帮助将不胜感激

编辑表单结构示例,

使用 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) {

});

最佳答案

您的 HTML 表单可能没有正确的结构设计。您可能想要发布您的输入和/或您的 HTML 表单设计。

gorilla/schemadecode 期望输入的值作为 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 支持的所有类型,例如您问题中的类型:结构 slice 。但是 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" 引用的。

使用这个和测试文件中的其他示例作为您的指南。

关于angularjs - 在 GoLang 中解析嵌入式结构形式的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44157149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com