All Projects → creasty → Defaults

creasty / Defaults

Licence: mit
Initialize structs with default values

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to Defaults

Mapper
A simple and easy go tools for auto mapper map to struct, struct to map, struct to struct, slice to slice, map to slice, map to json.
Stars: ✭ 175 (-39.66%)
Mutual labels:  struct, map
Arcgis Runtime Samples Ios
Swift samples demonstrating various capabilities of ArcGIS Runtime SDK for iOS
Stars: ✭ 275 (-5.17%)
Mutual labels:  map
react-vector-maps
🗺 A React component for interactive vector maps of the world and 100+ countries
Stars: ✭ 112 (-61.38%)
Mutual labels:  map
js-collections-map-set
Repository to have example code to demonstrate JavaScript Map and Set data structures.
Stars: ✭ 21 (-92.76%)
Mutual labels:  map
TileMapGenerator
Create your own 2D Maps with layer-by-layer system using Noise-Sample and BufferedImage pattern
Stars: ✭ 19 (-93.45%)
Mutual labels:  map
pmap
Process Map Visualization of event analysis in R
Stars: ✭ 19 (-93.45%)
Mutual labels:  map
Ant-Design-Pro-V5
Ant Design Pro V5 详细配置,包括分模块打包,ahooks的使用,L7 地图组件的封装,合理的初始化数据,更有动态表单、动态表格、OSS图片上传等优秀组件(项目会逐渐迭代)~
Stars: ✭ 28 (-90.34%)
Mutual labels:  map
Gonorth
GoNorth is a story and content planning tool for RPGs and other open world games.
Stars: ✭ 289 (-0.34%)
Mutual labels:  map
Kind Of
Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
Stars: ✭ 268 (-7.59%)
Mutual labels:  map
angular-mapboxgl-directive
AngularJS directive for Mapbox GL
Stars: ✭ 43 (-85.17%)
Mutual labels:  map
iOS-MapKit-Tutorial
iOS MapKit Getting Started
Stars: ✭ 24 (-91.72%)
Mutual labels:  map
Leaflet-active-area
A Leaflet plugin to center the map not in the center of the map but inside a DIV. Useful for responsive design.
Stars: ✭ 99 (-65.86%)
Mutual labels:  map
Promise Pool
Map-like, concurrent promise processing
Stars: ✭ 258 (-11.03%)
Mutual labels:  map
django-leaflet-admin-list
The Django Leaflet Admin List package provides an admin list view featured by the map and bounding box filter for the geo-based data of the GeoDjango.
Stars: ✭ 28 (-90.34%)
Mutual labels:  map
Vue Yandex Map
Yandex Maps Component for VueJS
Stars: ✭ 285 (-1.72%)
Mutual labels:  map
map download
地图地形下载工具
Stars: ✭ 21 (-92.76%)
Mutual labels:  map
map-machine
Python renderer for OpenStreetMap with custom icons intended to display as many map features as possible
Stars: ✭ 82 (-71.72%)
Mutual labels:  map
goverter
Generate type-safe Go converters by simply defining an interface
Stars: ✭ 100 (-65.52%)
Mutual labels:  struct
Election Geodata
Precinct shapes (and vote results) for US elections past, present, and future
Stars: ✭ 289 (-0.34%)
Mutual labels:  map
Redisson
Redisson - Redis Java client with features of In-Memory Data Grid. Over 50 Redis based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Publish / Subscribe, Bloom filter, Spring Cache, Tomcat, Scheduler, JCache API, Hibernate, MyBatis, RPC, local cache ...
Stars: ✭ 17,972 (+6097.24%)
Mutual labels:  map

defaults

Build Status codecov GitHub release License

Initialize structs with default values

  • Supports almost all kind of types
    • Scalar types
      • int/8/16/32/64, uint/8/16/32/64, float32/64
      • uintptr, bool, string
    • Complex types
      • map, slice, struct
    • Aliased types
      • time.Duration
      • e.g., type Enum string
    • Pointer types
      • e.g., *SampleStruct, *int
  • Recursively initializes fields in a struct
  • Dynamically sets default values by defaults.Setter interface
  • Preserves non-initial values from being reset with a default value

Usage

type Gender string

type Sample struct {
	Name   string `default:"John Smith"`
	Age    int    `default:"27"`
	Gender Gender `default:"m"`

	Slice       []string       `default:"[]"`
	SliceByJSON []int          `default:"[1, 2, 3]"` // Supports JSON
	Map         map[string]int `default:"{}"`
	MapByJSON   map[string]int `default:"{\"foo\": 123}"`

	Struct    OtherStruct  `default:"{}"`
	StructPtr *OtherStruct `default:"{\"Foo\": 123}"`

	NoTag  OtherStruct               // Recurses into a nested struct by default
	OptOut OtherStruct `default:"-"` // Opt-out
}

type OtherStruct struct {
	Hello  string `default:"world"` // Tags in a nested struct also work
	Foo    int    `default:"-"`
	Random int    `default:"-"`
}

// SetDefaults implements defaults.Setter interface
func (s *OtherStruct) SetDefaults() {
	if defaults.CanUpdate(s.Random) { // Check if it's a zero value (recommended)
		s.Random = rand.Int() // Set a dynamic value
	}
}
obj := &Sample{}
if err := defaults.Set(obj); err != nil {
	panic(err)
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].