#!/usr/bin/env fish

set name (basename (status -f))

echo "package ctxdata"
echo ""
echo "import ("
echo "	\"time\""
echo ")"
echo ""
echo "// Code generated by $name; DO NOT EDIT."

set template '
// Get{{NAME}} returns the value of key as type {{GOTYPE}}.
// If key is not set, or its value is not of type {{GOTYPE}},
// then Get{{NAME}} returns a zero value of type {{GOTYPE}}.
func (d *Data) Get{{NAME}}(key string) (val {{GOTYPE}}) {
	return d.Get{{NAME}}Default(key, val)
}

// Get{{NAME}}Default returns the value of key as type {{GOTYPE}}.
// If key is not set, or its value is not of type {{GOTYPE}},
// then Get{{NAME}}Default returns defaultValue.
func (d *Data) Get{{NAME}}Default(key string, defaultValue {{GOTYPE}}) {{GOTYPE}} {
	val, err := d.Get(key)
	if err != nil {
		return defaultValue
	}

	res, ok := val.({{GOTYPE}})
	if !ok {
		return defaultValue
	}

	return res
}'

for pair in String/string Int/int Int64/int64 Uint64/uint64 Float64/float64 Bool/bool Duration/time.Duration Time/time.Time Error/error
    set name   (echo $pair | cut -d'/' -f1)
    set gotype (echo $pair | cut -d'/' -f2)
    echo $template | sed -e "s/{{NAME}}/$name/g" | sed -e "s/{{GOTYPE}}/$gotype/g"
end
