Skip to content

Instantly share code, notes, and snippets.

@KarthikNayak
Created September 29, 2018 05:08
Show Gist options
  • Save KarthikNayak/2c3c02f25d2686eca8550d8d992ad517 to your computer and use it in GitHub Desktop.
Save KarthikNayak/2c3c02f25d2686eca8550d8d992ad517 to your computer and use it in GitHub Desktop.
Database server
package main
import (
"errors"
"fmt"
"net/http"
)
type getSetter interface {
set(key, value string) error
get(key string) (string, error)
}
type memory struct {
data map[string]string
}
func (m *memory) set(key, value string) error {
if key == "" {
return errors.New("set(): key not provided")
} else if value == "" {
return errors.New("set(): value not provided")
}
m.data[key] = value
return nil
}
func (m *memory) get(key string) (string, error) {
if key == "" {
return "", errors.New("get(): key not provided")
}
if value, ok := m.data[key]; ok {
return value, nil
}
return "", fmt.Errorf("get(): key: %v doesn't exist in data", key)
}
var dataStore getSetter = &memory{data: make(map[string]string)}
func setData(w http.ResponseWriter, r *http.Request) {
for key, value := range r.URL.Query() {
if err := dataStore.set(key, value[0]); err != nil {
_, err := w.Write([]byte(err.Error()))
if err != nil {
panic(err)
}
} else {
_, err := w.Write([]byte(fmt.Sprintf("Set key: %v with value: %v\n", key, value[0])))
if err != nil {
panic(err)
}
}
}
}
func getData(w http.ResponseWriter, r *http.Request) {
for arg, key := range r.URL.Query() {
if arg != "key" {
_, err := w.Write([]byte(fmt.Sprintf("Format is /get?key=<something>")))
if err != nil {
panic(err)
}
} else {
value, err := dataStore.get(key[0])
if err != nil {
_, err := w.Write([]byte(err.Error()))
if err != nil {
panic(err)
}
} else {
_, err := w.Write([]byte(fmt.Sprintf("Key: %v, Value: %v\n", key[0], value)))
if err != nil {
panic(err)
}
}
}
}
}
func main() {
http.HandleFunc("/set", setData)
http.HandleFunc("/get", getData)
if err := http.ListenAndServe(":4000", nil); err != nil {
panic(err)
}
}
package main
import (
"errors"
"testing"
)
var testMemorySetDB = []struct {
key string
value string
err error
}{
{
"foo",
"boo",
nil,
},
{
"foo",
"",
errors.New("some error"),
},
{
"",
"boo",
errors.New("some error"),
},
{
"",
"",
errors.New("some error"),
},
}
func TestMemorySet(t *testing.T) {
for _, tc := range testMemorySetDB {
m := memory{data: make(map[string]string)}
err := m.set(tc.key, tc.value)
if (tc.err == nil && err != nil) || (tc.err != nil && err == nil) {
t.Fatalf("expected error: %v got: %v", tc.err, err)
}
if tc.err == nil && m.data[tc.key] != tc.value {
t.Fatalf("value not being set in data")
}
}
}
var testMemoryGetDB = []struct {
key string
value string
err error
}{
{
"foo",
"boo",
nil,
},
{
"foo",
"",
errors.New("some error"),
},
{
"",
"",
errors.New("some error"),
},
}
func TestMemoryGet(t *testing.T) {
for _, tc := range testMemoryGetDB {
m := memory{data: make(map[string]string)}
_ = m.set(tc.key, tc.value)
value, err := m.get(tc.key)
if (tc.err == nil && err != nil) || (tc.err != nil && err == nil) {
t.Errorf("expected error: %v got: %v", tc.err, err)
}
if value != tc.value {
t.Errorf("expected value: %v got: %v", tc.value, value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment