From a27988d8c7f7dbab95edf65da5482aec5d8067e4 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Mon, 24 Apr 2023 12:16:29 +0200 Subject: Server: Support unmarshalling server as a string This is for V1 configs --- types/server/server.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'types/server/server.go') diff --git a/types/server/server.go b/types/server/server.go index aea496d..3bdebe1 100644 --- a/types/server/server.go +++ b/types/server/server.go @@ -2,6 +2,9 @@ package server import ( + "encoding/json" + "fmt" + "github.com/eduvpn/eduvpn-common/types/cookie" "github.com/eduvpn/eduvpn-common/types/protocol" ) @@ -20,6 +23,36 @@ const ( TypeCustom ) +// This is here to support V1 configs which had the server type as a string +func (t *Type) UnmarshalJSON(data []byte) error { + // First try to just unmarshal the type + var num int8 + if err := json.Unmarshal(data, &num); err == nil { + if num < int8(TypeUnknown) || num > int8(TypeCustom) { + return fmt.Errorf("invalid server type: %d", num) + } + *t = Type(num) + return nil + } + + // unmarshal the old way, as a string + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + switch str { + case "secure_internet": + *t = TypeSecureInternet + case "institute_access": + *t = TypeInstituteAccess + case "custom_server": + *t = TypeCustom + default: + return fmt.Errorf("invalid server type: %s", str) + } + return nil +} + // RequiredAskTransition represents the data that is sent when a transition is required to be handled and the go library needs data from the client // This data can be a profile selection or secure internet location selection type RequiredAskTransition struct { -- cgit v1.2.3