Code
Hub
Workspaces
Following
Trending
Connect
MCP
copy
Create free account
hub
/
github.com/creasty/defaults
/ functions
Functions
250 in github.com/creasty/defaults
⨍
Functions
250
◇
Types & classes
149
↓ 136 callers
Function
Set
Set fills the fields of the struct ptr points to from their `default` tags, and descends into structs, pointers, slices and maps to do the same below.
set.go:56
↓ 11 callers
Function
CanUpdate
CanUpdate returns true when the given value is an initial value of its type
canupdate.go:15
↓ 8 callers
Method
add
add adds an entry for k below the deepest x holds, and reports true, or reports false and adds nothing if x already holds k.
path.go:154
↓ 6 callers
Function
MustSet
MustSet function is a wrapper of Set function It will call Set and panic if err not equals nil.
mustset.go:5
↓ 5 callers
Method
hash
hash mixes k into an int whose low bits pick a bucket.
path.go:123
↓ 5 callers
Method
key
key is w as the path's index holds it. A reflect.Type points to the one descriptor of its type, so the descriptor's address names the type, as == on T
path.go:109
↓ 5 callers
Function
pathEntries
This test is in package defaults rather than defaults_test: it takes walking and pathIndex on their own, which Set reaches only through a walk deeper
path_test.go:21
↓ 5 callers
Function
pathKeys
pathKeys returns n keys, no two alike, and keeps the values they name alive for the test.
path_test.go:57
↓ 5 callers
Function
pathSmallIndex
pathSmallIndex returns an index with room for size entries, which grow doubles. The walk makes a larger one; a small one reaches grow in a handful of
path_test.go:52
↓ 5 callers
Method
pop
pop takes the deepest entry out of x.
path.go:171
↓ 5 callers
Method
repeatsFar
repeatsFar is repeatsNear for a w more than scanDepth down.
path.go:68
↓ 5 callers
Function
unmarshalTag
unmarshalTag offers value to target's own unmarshalers, UnmarshalText first and UnmarshalJSON second, and reports whether one took it. An empty value
unmarshal.go:22
↓ 4 callers
Function
pathBelow
pathBelow returns an entry naming a value of its own, below outer.
path_test.go:38
↓ 4 callers
Method
repeatsNear
repeatsNear reports whether the value w names is already being walked above it, on w.outer's path, if w is no more than scanDepth down. For a deeper w
path.go:55
↓ 3 callers
Method
leave
leave takes w out of the path's index, if the index holds it, as w's walk returns. It is called on every return, a failed walk's too: a failure ends t
path.go:100
↓ 2 callers
Method
SetDefaults
()
setter.go:14
↓ 2 callers
Function
benchFillValue
benchFillValue is benchFill for a type built at run time.
benchmark_test.go:556
↓ 2 callers
Function
benchStruct
benchStruct builds a struct of n fields of typ, F0 to Fn-1, each tagged tag: a field count or a tag chosen at run time, where a literal struct would t
benchmark_test.go:621
↓ 2 callers
Function
isInitialValue
(field reflect.Value)
canupdate.go:5
↓ 2 callers
Function
isPromotedMethod
isPromotedMethod reports whether the method called name is promoted from an embedded field rather than declared by t itself. reflect lists the two al
method.go:23
↓ 2 callers
Function
pathKeyInBucket
pathKeyInBucket returns a key other than k that an index of that many buckets puts in k's bucket, to test a chain of them.
path_test.go:70
↓ 2 callers
Function
set
set is Set, for a struct that may be reached by recursion and so carries the tags still being applied above it and the values being walked on the path
set.go:62
↓ 2 callers
Function
setField
setField fills field from its tag and descends into it. It reports whether an UnmarshalText or UnmarshalJSON took the tag, the field's own or, behind
set.go:118
↓ 1 callers
Method
Ping
()
method_test.go:48
↓ 1 callers
Method
UnmarshalJSON
([]byte)
set_unmarshaler_test.go:108
↓ 1 callers
Method
UnmarshalJSON
(data []byte)
benchmark_test.go:666
↓ 1 callers
Method
UnmarshalText
([]byte)
set_unmarshaler_test.go:103
↓ 1 callers
Method
UnmarshalText
(text []byte)
benchmark_test.go:658
↓ 1 callers
Method
addTo
addTo adds w to x, after every entry above it that x does not hold yet. Each entry below scanDepth adds itself as it is checked, so the entries x lack
path.go:90
↓ 1 callers
Function
benchFill
benchFill measures filling a zero T. The value is allocated once and reset in place before each Set, so allocs/op counts Set's allocations and not the
benchmark_test.go:533
↓ 1 callers
Function
benchNestedValues
benchNestedValues builds depth structs nested by value, each holding the next as its first field. Only the innermost has a default. A []string at ever
benchmark_test.go:632
↓ 1 callers
Function
benchRepeat
benchRepeat measures Set on a value it leaves as it is. The first Set fills ptr, and ready checks that the value is in the state the case measures.
benchmark_test.go:580
↓ 1 callers
Function
benchTaggedChain
benchTaggedChain builds depth structs linked by pointers tagged `default:"{}"`, ending in a struct whose int has a default. Every level is a type of i
benchmark_test.go:647
↓ 1 callers
Function
callSetter
(v interface{})
setter.go:17
↓ 1 callers
Function
canHoldDefaults
canHoldDefaults reports whether a slice element or map value of type t is of a kind the walk descends into. Such a value has no tag of its own, so des
set.go:426
↓ 1 callers
Function
fillField
fillField is setField for a field it does not leave alone: it applies the tag if isInitial, which reports whether the field is zero, and descends into
set.go:143
↓ 1 callers
Method
grow
grow doubles x's room for entries and its buckets, as many of each and a power of two, and chains every entry again from the top down, so that each ch
path.go:179
↓ 1 callers
Function
hasPromotedSetter
hasPromotedSetter reports whether the SetDefaults of t, a struct type whose pointer implements Setter, is promoted from an embedded field rather than
setter.go:26
↓ 1 callers
Method
repeatsInIndex
repeatsInIndex is repeatsFar's check. It makes the path's index if the walk has none yet, adds to it every entry above w that it does not hold, and th
path.go:74
↓ 1 callers
Function
shouldInitializeField
shouldInitializeField reports whether the field's own state warrants visiting it, regardless of any tag: a struct is always descended into, as is a po
set.go:438
Function
BenchmarkCanUpdate
BenchmarkCanUpdate measures the zero check on its own, which is what Set spends per field.
benchmark_test.go:78
Function
BenchmarkFail
BenchmarkFail measures a Set that fails. The first error is checked, so each case measures the failure it names.
benchmark_test.go:492
Function
BenchmarkParse
BenchmarkParse applies one tag to one zero field. unparsed_kind is the cost every other row includes: a tagged zero field with nothing parsed and noth
benchmark_test.go:129
Function
BenchmarkSet
Every case calls b.ReportAllocs. A case that calls Set does its setup and checks the value it measures once before b.ResetTimer, and then does the sam
benchmark_test.go:25
Function
BenchmarkWalk
BenchmarkWalk measures going through a value with little or nothing left to fill. Unless a case says otherwise, it runs Set again on a value the first
benchmark_test.go:260
Function
Example
This example fills a tree of structs, pointers, slices and maps in one call. Every Endpoint the call reaches gets the same treatment wherever it sits:
example_test.go:42
Function
ExampleCanUpdate
()
example_test.go:142
Function
ExampleSet
()
example_test.go:75
Function
ExampleSet_nested
()
example_test.go:98
Function
ExampleSetter
()
example_test.go:124
Method
Ping
()
method_test.go:21
Method
Ping
()
method_test.go:26
Method
Ping
()
method_test.go:45
Method
SetDefaults
()
set_cycle_test.go:18
Method
SetDefaults
()
set_unexported_test.go:17
Method
SetDefaults
()
setter_test.go:20
Method
SetDefaults
()
setter_test.go:29
Method
SetDefaults
()
setter_test.go:41
Method
SetDefaults
()
setter_test.go:51
Method
SetDefaults
()
setter_test.go:64
Method
SetDefaults
()
setter_test.go:76
Method
SetDefaults
()
setter_test.go:86
Method
SetDefaults
()
setter_test.go:96
Method
SetDefaults
()
set_unmarshaler_test.go:62
Method
SetDefaults
()
set_unmarshaler_test.go:76
Method
SetDefaults
SetDefaults derives a default that a tag cannot express, and CanUpdate keeps it from overriding what the caller already provided.
example_test.go:20
Method
SetDefaults
SetDefaults derives Addr from Host and Port, which the tags have filled by the time it is called.
example_test.go:34
Method
SetDefaults
()
set_shared_test.go:19
Method
SetDefaults
()
set_optout_test.go:20
Method
SetDefaults
()
set_optout_test.go:30
Method
SetDefaults
()
set_map_test.go:222
Method
SetDefaults
()
benchmark_test.go:692
Function
TestCanUpdate
TestCanUpdate covers what "can update" means: the value still holds the zero value of its own type. Note that an allocated-but-empty slice or map is t
canupdate_test.go:15
Function
TestCanUpdate_FloatEdges
TestCanUpdate_FloatEdges pins the two floats where a per-kind zero check and a comparison against the zero value could plausibly disagree: a negative
canupdate_test.go:62
Function
TestCanUpdate_Nil
TestCanUpdate_Nil covers a nil, which carries no type to compare against: there is nothing there to preserve, so it is updatable. It used to panic ins
canupdate_test.go:82
Function
TestIsPromotedMethod
TestIsPromotedMethod covers where the code behind a method comes from, which reflect reports alike for a method a type declares and one it takes from
method_test.go:54
Function
TestMustSet_PanicsForInvalidTag
(t *testing.T)
mustset_test.go:64
Function
TestMustSet_PanicsForNil
TestMustSet_PanicsForNil pins that a nil panics with Set's error. It used to panic too, but from inside Set, before there was an error to panic with.
mustset_test.go:46
Function
TestMustSet_PanicsForNonPointer
(t *testing.T)
mustset_test.go:25
Function
TestMustSet_PanicsForPointerToNonStruct
(t *testing.T)
mustset_test.go:35
Function
TestMustSet_SetsDefaults
(t *testing.T)
mustset_test.go:12
Function
TestPathIndex_AddHoldsEachKeyOnce
TestPathIndex_AddHoldsEachKeyOnce covers add's answer, which is the repeat: a key the index holds is not added again.
path_test.go:183
Function
TestPathIndex_GrowingKeepsEachBucketDeepestFirst
TestPathIndex_GrowingKeepsEachBucketDeepestFirst covers the chains grow builds again. The deepest key of a bucket heads its chain, so taking it out mo
path_test.go:262
Function
TestPathIndex_GrowsAndKeepsEveryKey
TestPathIndex_GrowsAndKeepsEveryKey covers growing past the room the index was made with, which a walk does a few hundred values down: every key is st
path_test.go:221
Function
TestPathIndex_HoldsKeysThatShareABucket
TestPathIndex_HoldsKeysThatShareABucket covers the chain a bucket holds: keys that hash together are told apart, and taking one out leaves the others
path_test.go:244
Function
TestPathIndex_PopTakesTheDeepestKeyOut
TestPathIndex_PopTakesTheDeepestKeyOut covers pop, which leave calls: the keys join and leave the index as their values do the path, so the one taken
path_test.go:200
Function
TestSet_ArraysAreLeftAlone
TestSet_ArraysAreLeftAlone pins that reflect.Array has no case in setField, which makes an array behave unlike the slice of the same element type: its
set_slice_test.go:148
Function
TestSet_Bool
(t *testing.T)
set_scalar_test.go:58
Function
TestSet_ByteSlice
TestSet_ByteSlice covers []byte, where encoding/json accepts two spellings and rejects bare text.
set_slice_test.go:167
Function
TestSet_CallerPointerToContainerIsSkipped
TestSet_CallerPointerToContainerIsSkipped covers a pointer the caller allocated to a slice, a map or a pointer, as a field, a slice element and a map
set_pointer_test.go:152
Function
TestSet_CannotPreserveFalseBool
TestSet_CannotPreserveFalseBool pins the one type whose caller-provided value is indistinguishable from "unset": a bool. An explicit false is overwrit
set_preserve_test.go:98
Function
TestSet_CyclesEnd
TestSet_CyclesEnd covers data the caller built with a way back to itself. Where the walk comes back to a value it is still walking, Set goes no furthe
set_cycle_test.go:29
Function
TestSet_CyclesEndAtAnyDepth
TestSet_CyclesEndAtAnyDepth covers cycles that close far down a long path. Set scans the path for a value near its top, but looks up a value further d
set_cycle_test.go:152
Function
TestSet_Duration
(t *testing.T)
set_duration_test.go:14
Function
TestSet_DurationAndIntegerShareOneParser
TestSet_DurationAndIntegerShareOneParser pins the outcomes of int64-kinded fields being handed to both time.ParseDuration and strconv.ParseInt: a bare
set_duration_test.go:44
Function
TestSet_DurationEmptyTagReportsNothing
TestSet_DurationEmptyTagReportsNothing covers `default:""` on an int64-kinded field. Both parsers reject an empty tag, as they reject "1d", but it ask
set_duration_test.go:80
Function
TestSet_DurationErrorNamesBothParsers
TestSet_DurationErrorNamesBothParsers covers an int64-kinded tag that neither parser takes. The error carries both rejections, the duration parser's f
set_duration_test.go:63
Function
TestSet_DurationStringOnANarrowerIntegerIsRejected
TestSet_DurationStringOnANarrowerIntegerIsRejected pins that the duration fallback is int64-only. A narrower width rejects a duration string outright,
set_duration_test.go:95
Function
TestSet_DurationTagIsTrimmed
TestSet_DurationTagIsTrimmed covers a duration tag carrying surrounding whitespace, which used to leave the field at zero. The trim happens where Pars
set_duration_test.go:117
Function
TestSet_EmbeddedPointerStruct
(t *testing.T)
set_struct_test.go:94
next →
1–100 of 250, ranked by callers