Skip to content

Instantly share code, notes, and snippets.

@randallmlough
randallmlough / struct_to_map_with_tags.go
Last active Nov 5, 2019
Struct with tags to map to SQL statement and values with tests
View struct_to_map_with_tags.go
// struct tags
// db:"something" - first tag will always be the name
// db:"-" - will skip the field and move on
// db:",follow" - will inspect the struct without using it's current fieldname as a column name. Useful when you want to embed a type/struct without using it's fieldname as a column
// db:",required" – if a fieldvalue is of type pointer and it is empty, it will return an error
// db:",asIs" – doesn't do anything further mapping or reflection. Takes it's fieldname and value and adds to list. Useful for json columns or []string, etc.
// db:",omitempty" - if the pointer value is nil it will not include it in the final map
// example: `db:"column_name,follow,required"`
// example: `db:"column_name,asIs,omitempty"`
// note: if a "follow" or "asIs" is present, an embeded struct will be dot notated. Ie. "something"."level_one_field"."level_two_field" etc.
@randallmlough
randallmlough / translate.go
Last active Sep 5, 2022 — forked from hvoecking/translate.go
Golang reflection: traversing arbitrary structures – []map[string]interface{} support
View translate.go
// Traverses an arbitrary struct and translates all stings it encounters
//
// I haven't seen an example for reflection traversing an arbitrary struct, so
// I want to share this with you. If you encounter any bugs or want to see
// another example please comment.
//
// The MIT License (MIT)
//
// Copyright (c) 2014 Heye Vöcking
//
@randallmlough
randallmlough / hugo-img-partial.html
Last active Jun 21, 2021
Complete hugo img partial using page resources
View hugo-img-partial.html
{{ $image := .Params.image}}
{{ $media := (.Site.GetPage "page" "media").Resources }}
{{ $original := index ($media.Match (printf "%s" $image)) 0 }}
{{ $width := $original.Width}}
{{ $intWidth := int $width }}
{{ $sizes := .Params.sizes}}
{{ $options := .Params.options}}
{{ if le (len $sizes) 1 }}
{{ $oneSize := index $sizes 0}}
@randallmlough
randallmlough / hugo-img-srcset.html
Last active May 8, 2021
A hugo img srcset partial that auto resizes images
View hugo-img-srcset.html
<style>
img {
max-width: 100%;
}
</style>
<h1>Creating a srcset loop based on Resources</h1>
<!--
NOTE: This requires Hugo 0.32