nsmithuk / local-kms

A mock version of AWS' Key Management Service, for local development and testing.
MIT License
198 stars 34 forks source link

Refactoring switch case #3

Closed GuilhermeFirmiano closed 5 years ago

GuilhermeFirmiano commented 5 years ago

You could use reflection

GuilhermeFirmiano commented 5 years ago
             w.Header().Set("Content-Type", "application/x-amz-json-1.1")

        h := handler.NewRequestHandler(r, logger, database)

        split := strings.Split(r.Header.Get("X-Amz-Target"), ".")

        method := reflect.ValueOf(h).MethodByName(split[1])

        if method.IsValid() {
            result := method.Call([]reflect.Value{})

            response := result[0].Interface().(handler.Response)

            respond(w, response)
        }

        error501(w)
nsmithuk commented 5 years ago

Thank you for the feedback @GuilhermeFirmiano. You're right, that switch statement is a little unwieldy. I'll take a look over at what you're suggesting.

nsmithuk commented 5 years ago

Hi @GuilhermeFirmiano. Thank you again for the feedback. I've just pushed the change.