Generating Random String of Numbers and Letters Using Go's "testing/quick" Package -
i've been breaking head on few days , can't seem able figure out. perhaps it's glaringly obvious, don't seem able spot it. i've read on basics of unicode, utf-8, utf-16, normalisation, etc, no avail. somebody's able me out here...
i'm using go's value
function testing/quick
package generate random values fields in data structs, in order implement generator
interface structs in question. specifically, given metadata
struct, i've defined implementation follows:
func (m *metadata) generate(r *rand.rand, size int) (value reflect.value) { value = reflect.valueof(m).elem() := 0; < value.numfield(); i++ { if t, ok := quick.value(value.field(i).type(), r); ok { value.field(i).set(t) } } return }
now, in doing so, i'll end both receiver , return value being set random generated values of appropriate type (strings, ints, etc. in receiver , reflect.value in returned reflect.value).
now, implementation value
function states return of type []rune
converted type string
. far know, should allow me use functions in runes
, unicode
, norm
packages define filter filters out not part of 'latin', 'letter' or 'number'. defined following filter uses transform filter out letters not in character rangetable
s (as defined in unicode
package):
func runefilter(in reflect.value) (out reflect.value) { out = in // make sure return if in.kind() == reflect.string { instr := in.string() t := transform.chain(norm.nfd, runes.remove(runes.notin(rangetable.merge(unicode.letter, unicode.latin, unicode.number))), norm.nfc) outstr, _, _ := transform.string(t, instr) out = reflect.valueof(outstr) } return }
now, think i've tried anything, keep ending series of strings far latin range, e.g.:
𥗉똿穊 𢷽嚶 秓䝏小𪖹䮋 𪿝ท솲 𡉪䂾 ʋ𥅮ᦸ 堮𡹯憨𥗼𧵕ꥆ 𢝌𐑮𧍛併怃𥊇 鯮 𣏲𝐒 ⓿ꐠ槹𬠂黟 𢼭踁퓺𪇖 俇𣄃𔘧 𢝶 𝖸쩈𤫐𢬿詢𬄙 𫱘𨆟𑊙 欓
so, can explain i'm overlooking here , how instead define transformer
removes/replaces non-letter/number/latin characters can use value
function intended (but smaller subset of 'random' characters)?
thanks!
confusingly generate interface needs function using type not pointer type. want type signature like
func (m metadata) generate(r *rand.rand, size int) (value reflect.value)
you can play here. note: important thing in playground switch type of generate function m metadata
m *metadata
, see hi mom! never prints.
in addition, think better served using own type , writing generate method type using list of of characters want use. example:
type latinstring string const latin = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz01233456789"
and use generator
func (l latinstring) generate(rand *rand.rand, size int) reflect.value { var buffer bytes.buffer := 0; < size; i++ { buffer.writestring(string(latin[rand.intn(len(latin))])) } s := latinstring(buffer.string()) return reflect.valueof(s) }
edit: library pretty cool, showing me
Comments
Post a Comment