Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update (hotels&airports) #78

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions intern/parser/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"reflect"
"strconv"
"strings"

"github.com/google/uuid"
)

// Unmarshal parses the url.Values data and stores the result
Expand Down Expand Up @@ -93,6 +95,13 @@ func Unmarshal(input url.Values, target any) error {
if err := Unmarshal(newInput, fieldVal.Addr().Interface()); err != nil {
return err
}
case reflect.TypeOf(uuid.UUID{}).Kind():
if fieldValRaw == "" {
continue
}
parsedUUID := uuid.MustParse(fieldValRaw)

fieldVal.Set(reflect.ValueOf(parsedUUID))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff, but that would mean we have to implement any new type definition?

Since uuid.UUID is a [16]byte, can we maybe change the case to check for reflect.Array instead?


And in case that does not work, we could maybe provide a way to extend the Unmarshal capabilities by providing a custom HtmlFormUnmarshaler implementation.

case reflect.TypeOf((*HtmlFormUnmarshaler)(nil)).Elem().Kind():
    if fieldValRaw == "" {
        continue
    }
    fieldValue := reflect.New(field.Type).Interface().(HtmlFormUnmarshaler)
    if err := fieldValue.UnmarshalFormValue(fieldValRaw); err != nil {
        return err
    }
    fieldVal.Set(reflect.ValueOf(fieldValue).Elem())

In that case uuid.UUID has to implement this method:

type HtmlFormUnmarshaler interface {
    UnmarshalFormValue(value string) error
}

For this we could wrap the wrap the type:

type UUID struct {
    uuid.UUID
}
func (u *UUID) UnmarshalFormValue(value string) error {
    panic("not implemented")
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reflect.Array seems to work fine here

default:
panic(fmt.Sprintf("unsupported type: %s", field.Type.String()))
}
Expand Down
92 changes: 92 additions & 0 deletions intern/parser/form/form_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (C) 2024 the lets-party maintainers
// See root-dir/LICENSE for more information

package form

import (
"net/url"
"reflect"
"testing"

"github.com/google/uuid"
)

type TestStruct struct {
UUIDField uuid.UUID `form:"uuid_field"`
StringField string `form:"string_field"`
BoolField bool `form:"bool_field"`
IntField int `form:"int_field"`
FloatField float64 `form:"float_field"`
SliceField []string `form:"slice_field"`
StructField FieldStruct `form:"struct_field"`
}

type FieldStruct struct {
StringField string `form:"field_struct_strfield"`
BoolField bool `form:"field_struct_boolfield"`
}

func TestUnmarshal(t *testing.T) {
led0nk marked this conversation as resolved.
Show resolved Hide resolved
testCases := []struct {
name string
input url.Values
expected TestStruct
expectedErr bool
}{
{
name: "Valid input data",
input: url.Values{
"uuid_field": {"ca07d617-c87c-4ac3-affc-27a5e941b28f"},
"string_field": {"test_string"},
"bool_field": {"true"},
"int_field": {"42"},
"float_field": {"3.14"},
"slice_field": {"1", "2", "3"},
"struct_field.field_struct_strfield": {"stringfield"},
"struct_field.field_struct_boolfield": {"true"},
},
expected: TestStruct{
UUIDField: uuid.MustParse("ca07d617-c87c-4ac3-affc-27a5e941b28f"),
StringField: "test_string",
BoolField: true,
IntField: 42,
FloatField: 3.14,
SliceField: []string{"1", "2", "3"},
StructField: FieldStruct{
StringField: "stringfield",
BoolField: true,
},
},
expectedErr: false,
},
{
name: "Empty input",
input: url.Values{},
expected: TestStruct{},
expectedErr: false,
},
{
name: "Missing fields",
input: url.Values{
"string_field": {"test_string"},
},
expected: TestStruct{
StringField: "test_string",
},
expectedErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var target TestStruct
err := Unmarshal(tc.input, &target)
if (err != nil) != tc.expectedErr {
t.Errorf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(target, tc.expected) {
t.Errorf("Unmarshal did not produce expected result. got: %+v, expected: %+v", target, tc.expected)
}
})
}
}
8 changes: 5 additions & 3 deletions intern/server/templates/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,20 +854,22 @@ func (p *GuestHandler) UpdateEvent(c *gin.Context) {
p.logger.ErrorContext(ctx, "could not parse other location", "error", err)
continue
}
lID, err := uuid.Parse(id)
var err error
// INFO: workaround(l.ID) until Unmarshal method is fully implemented
l.ID, err = uuid.Parse(id)
led0nk marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "invalid uuid")
p.logger.ErrorContext(ctx, "invalid uuid", "error", err)
continue
}
for i := 0; i < len(e.Airports); i++ {
if lID == e.Airports[i].ID {
if l.ID == e.Airports[i].ID {
e.Airports[i] = &l
}
}
for i := 0; i < len(e.Hotels); i++ {
if lID == e.Hotels[i].ID {
if l.ID == e.Hotels[i].ID {
e.Hotels[i] = &l
}
}
Expand Down