Skip to content

Instantly share code, notes, and snippets.

@jackhftang
Last active April 9, 2019 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackhftang/0658bd5d9cd04077c419e82bdaff0865 to your computer and use it in GitHub Desktop.
Save jackhftang/0658bd5d9cd04077c419e82bdaff0865 to your computer and use it in GitHub Desktop.
Generate a string map in goalng. [WTFPL](http://www.wtfpl.net/txt/copying/)
package util
import (
"time"
)
<?php
$className = "StringMap";
$arr = [
"Bool" => "bool",
"String" => "string",
"Int" => "int",
"Int8" => "int8",
"Int16" => "int16",
"Int32" => "int32",
"Int64" => "int64",
"Uint" => "uint",
"Uint8" => "uint8",
"Uint16" => "uint16",
"Uint32" => "uint32",
"Uint64" => "uint64",
"Time" => "time.Time"
];
?>
type <?= $className ?> map[string]interface{}
func (m <?= $className ?>) has(key string) bool {
_, ok := m[key]
return ok
}
func (m <?= $className ?>) GetInterface(key string) interface{} {
return m[key]
}
<? foreach($arr as $name => $type){ ?>
func (m <?= $className ?>) Get<?= $name ?>(key string) <?= $type ?> {
return m[key].(<?= $type ?>)
}
func (m <?= $className ?>) Get<?= $name ?>Or(key string, n <?= $type ?>) <?= $type ?> {
if val, ok := m[key]; ok {
if x, ok := val.(<?= $type ?>); ok {
return x
}
}
return n
}
<? } ?>
package util
import (
"time"
)
type StringMap map[string]interface{}
func (m StringMap) has(key string) bool {
_, ok := m[key]
return ok
}
func (m StringMap) GetInterface(key string) interface{} {
return m[key]
}
func (m StringMap) GetBool(key string) bool {
return m[key].(bool)
}
func (m StringMap) GetBoolOr(key string, n bool) bool {
if val, ok := m[key]; ok {
if x, ok := val.(bool); ok {
return x
}
}
return n
}
func (m StringMap) GetString(key string) string {
return m[key].(string)
}
func (m StringMap) GetStringOr(key string, n string) string {
if val, ok := m[key]; ok {
if x, ok := val.(string); ok {
return x
}
}
return n
}
func (m StringMap) GetInt(key string) int {
return m[key].(int)
}
func (m StringMap) GetIntOr(key string, n int) int {
if val, ok := m[key]; ok {
if x, ok := val.(int); ok {
return x
}
}
return n
}
func (m StringMap) GetInt8(key string) int8 {
return m[key].(int8)
}
func (m StringMap) GetInt8Or(key string, n int8) int8 {
if val, ok := m[key]; ok {
if x, ok := val.(int8); ok {
return x
}
}
return n
}
func (m StringMap) GetInt16(key string) int16 {
return m[key].(int16)
}
func (m StringMap) GetInt16Or(key string, n int16) int16 {
if val, ok := m[key]; ok {
if x, ok := val.(int16); ok {
return x
}
}
return n
}
func (m StringMap) GetInt32(key string) int32 {
return m[key].(int32)
}
func (m StringMap) GetInt32Or(key string, n int32) int32 {
if val, ok := m[key]; ok {
if x, ok := val.(int32); ok {
return x
}
}
return n
}
func (m StringMap) GetInt64(key string) int64 {
return m[key].(int64)
}
func (m StringMap) GetInt64Or(key string, n int64) int64 {
if val, ok := m[key]; ok {
if x, ok := val.(int64); ok {
return x
}
}
return n
}
func (m StringMap) GetUint(key string) uint {
return m[key].(uint)
}
func (m StringMap) GetUintOr(key string, n uint) uint {
if val, ok := m[key]; ok {
if x, ok := val.(uint); ok {
return x
}
}
return n
}
func (m StringMap) GetUint8(key string) uint8 {
return m[key].(uint8)
}
func (m StringMap) GetUint8Or(key string, n uint8) uint8 {
if val, ok := m[key]; ok {
if x, ok := val.(uint8); ok {
return x
}
}
return n
}
func (m StringMap) GetUint16(key string) uint16 {
return m[key].(uint16)
}
func (m StringMap) GetUint16Or(key string, n uint16) uint16 {
if val, ok := m[key]; ok {
if x, ok := val.(uint16); ok {
return x
}
}
return n
}
func (m StringMap) GetUint32(key string) uint32 {
return m[key].(uint32)
}
func (m StringMap) GetUint32Or(key string, n uint32) uint32 {
if val, ok := m[key]; ok {
if x, ok := val.(uint32); ok {
return x
}
}
return n
}
func (m StringMap) GetUint64(key string) uint64 {
return m[key].(uint64)
}
func (m StringMap) GetUint64Or(key string, n uint64) uint64 {
if val, ok := m[key]; ok {
if x, ok := val.(uint64); ok {
return x
}
}
return n
}
func (m StringMap) GetTime(key string) time.Time {
return m[key].(time.Time)
}
func (m StringMap) GetTimeOr(key string, n time.Time) time.Time {
if val, ok := m[key]; ok {
if x, ok := val.(time.Time); ok {
return x
}
}
return n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment