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 all 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
2 changes: 1 addition & 1 deletion intern/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Event struct {
}

type Location struct {
ID uuid.UUID `json:"id" form:"-"`
ID uuid.UUID `json:"id" form:"id"`
CreatedAt *time.Time `json:"created_at" form:"-"`
UpdatedAt *time.Time `json:"updated_at,omitempty" form:"-"`
Name string `json:"name,omitempty" form:"name"`
Expand Down
11 changes: 11 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 @@ -63,7 +65,9 @@ func Unmarshal(input url.Values, target any) error {
return err
}
fieldVal.SetFloat(fValue)

case reflect.Slice:
// TODO: needs some work for implementation of prim types
sliceValue := reflect.ValueOf(value)
for i := 0; i < sliceValue.Len(); i++ {
if isPrimitiveType(sliceValue.Type().Elem().Kind()) {
Expand Down Expand Up @@ -93,6 +97,13 @@ func Unmarshal(input url.Values, target any) error {
if err := Unmarshal(newInput, fieldVal.Addr().Interface()); err != nil {
return err
}
case reflect.Array:
if fieldValRaw == "" {
continue
}
parsedUUID := uuid.MustParse(fieldValRaw)

fieldVal.Set(reflect.ValueOf(parsedUUID))
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)
}
})
}
}
28 changes: 0 additions & 28 deletions intern/server/templates/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,34 +845,6 @@ func (p *GuestHandler) UpdateEvent(c *gin.Context) {
return
}

for id, ldata := range raw {
ldata["id"] = []string{id}
l := model.Location{}
if err := form.Unmarshal(ldata, &l); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "could not parse other location")
p.logger.ErrorContext(ctx, "could not parse other location", "error", err)
continue
}
lID, err := uuid.Parse(id)
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 {
e.Airports[i] = &l
}
}
for i := 0; i < len(e.Hotels); i++ {
if lID == e.Hotels[i].ID {
e.Hotels[i] = &l
}
}
}

if err := p.eStore.UpdateEvent(ctx, e); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "could not update event")
Expand Down