gpt4 book ai didi

function - 为什么我的函数无法在一个URL上修剪空格,但在另一个URL上却能正常工作?

转载 作者:行者123 更新时间:2023-12-03 10:10:19 25 4
gpt4 key购买 nike

我有一个数据集结构,看起来像这样-

type Dataset struct {
Publications []GeneralDetails `bson:"publications,omitempty" json:"publications,omitempty"`
URI string `bson:"uri,omitempty" json:"uri,omitempty"`
}
还有一个GeneralDetails结构,看起来像这样-
type GeneralDetails struct {
Description string `bson:"description,omitempty" json:"description,omitempty"`
HRef string `bson:"href,omitempty" json:"href,omitempty"`
Title string `bson:"title,omitempty" json:"title,omitempty"`
}
我有一个ValidateDastaset函数,该函数可修剪空白并返回已解析的URL-
func ValidateDataset(ctx context.Context, dataset *Dataset) error {
var generalDetails = &GeneralDetails{}
var invalidFields []string
if dataset.URI != "" {
dataset.URI = strings.TrimSpace(dataset.URI)
_, err := url.Parse(dataset.URI)
if err != nil {
invalidFields = append(invalidFields, "URI")
log.Event(ctx, "error parsing URI", log.ERROR, log.Error(err))
}
}

if dataset.Publications != nil {
generalDetails.HRef = strings.TrimSpace(generalDetails.HRef)
_, err := url.Parse(generalDetails.HRef)
if err != nil {
invalidFields = append(invalidFields, "href")
log.Event(ctx, "error parsing URI", log.ERROR, log.Error(err))
}
}

if invalidFields != nil {
return fmt.Errorf("invalid fields: %v", invalidFields)
}
return nil

}
但是,仅在dataset.URI上修剪了空白,而在generalDetails.HRef上没有修剪,我真的不明白为什么。有人能帮忙吗?
编辑:这是我的测试包中的内容,这是我知道空白不被修剪的方式-
var testPublications = GeneralDetails{
Description: "some publication description",
HRef: "http://localhost:22000//datasets/publications",
Title: "some publication title",
}

func createDataset() Dataset {
return Dataset{
ID: "123",
URI: "http://localhost:22000/datasets/123",
Publications: []GeneralDetails{
{Description: "some publication description"},
{HRef: "http://localhost:22000//datasets/publications"},
{Title: "some publication title"},
},
}
}

}

func createGeneralDetails() GeneralDetails {
return testPublications

}
而实际的测试本身却失败了-
Convey("Successful validation (true) returned", t, func() {

Convey("when generalDetails.Href contains whitespace it should not return an error ", func() {
dataset := createDataset()
dataset.ID = "123"
generalDetails := createGeneralDetails()
generalDetails.HRef = " http://localhost:22000//datasets/publications "
validationErr := ValidateDataset(testContext, &dataset)
So(validationErr, ShouldBeNil)
So(generalDetails.HRef, ShouldEqual, "http://localhost:22000//datasets/publications")
})
})

最佳答案

ValidateDataset函数中,您创建了一个

var generalDetails = &GeneralDetails{}
但是您永远不会分配给此变量,也不会迭代传入的 Publications*Dataset字段
if dataset.Publications != nil {
generalDetails.HRef = strings.TrimSpace(generalDetails.HRef)
_, err := url.Parse(generalDetails.HRef)
if err != nil {
invalidFields = append(invalidFields, "href")
log.Event(ctx, "error parsing URI", log.ERROR, log.Error(err))
}
}
您只是在编辑nil generalDetails var,而完全忘记了 dataset.Publications
您需要做类似的事情
for i := range dataset.Publications {
generalDetails = &dataset.Publications[i]
// validate
}

关于function - 为什么我的函数无法在一个URL上修剪空格,但在另一个URL上却能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66281337/

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