From 8286c2dc986535d23e3fad8d3e816b9dd1e5aea6 Mon Sep 17 00:00:00 2001 From: Ugorji Nwoke Date: Tue, 28 Nov 2023 03:15:29 -0500 Subject: [PATCH] codecgen: use base32 with unique letters for type names We previously used base64 with duplicate letters. This fails from go1.22 Fixes #407 --- codec/gen.go | 16 +++++++------- codec/values_codecgen_generated_test.go | 28 ++++++++++++------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/codec/gen.go b/codec/gen.go index de7ee72f..0026e3e1 100644 --- a/codec/gen.go +++ b/codec/gen.go @@ -8,7 +8,7 @@ package codec import ( "bytes" - "encoding/base64" + "encoding/base32" "errors" "fmt" "go/format" @@ -190,7 +190,11 @@ var ( errGenExpectArrayOrMap = errors.New("unexpected type - expecting array/map/slice") errGenUnexpectedTypeFastpath = errors.New("fast-path: unexpected type - requires map or slice") - genBase64enc = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__") + // don't use base64, only 63 characters allowed in valid go identifiers + // ie ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_ + // + // don't use numbers, as a valid go identifer must start with a letter. + genTypenameEnc = base32.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef") genQNameRegex = regexp.MustCompile(`[A-Za-z_.]+`) ) @@ -2303,8 +2307,6 @@ func genMethodNameT(t reflect.Type, tRef reflect.Type) (n string) { } } else { // best way to get the package name inclusive - // return ptrPfx + strings.Replace(tstr, ".", "_", 1000) - // return ptrPfx + genBase64enc.EncodeToString([]byte(tstr)) if t.Name() != "" && genQNameRegex.MatchString(tstr) { return ptrPfx + strings.Replace(tstr, ".", "_", 1000) } else { @@ -2315,12 +2317,12 @@ func genMethodNameT(t reflect.Type, tRef reflect.Type) (n string) { } } -// genCustomNameForType base64encodes the t.String() value in such a way +// genCustomNameForType base32 encodes the t.String() value in such a way // that it can be used within a function name. func genCustomTypeName(tstr string) string { - len2 := genBase64enc.EncodedLen(len(tstr)) + len2 := genTypenameEnc.EncodedLen(len(tstr)) bufx := make([]byte, len2) - genBase64enc.Encode(bufx, []byte(tstr)) + genTypenameEnc.Encode(bufx, []byte(tstr)) for i := len2 - 1; i >= 0; i-- { if bufx[i] == '=' { len2-- diff --git a/codec/values_codecgen_generated_test.go b/codec/values_codecgen_generated_test.go index f58dc8fe..27ecb209 100644 --- a/codec/values_codecgen_generated_test.go +++ b/codec/values_codecgen_generated_test.go @@ -11173,7 +11173,7 @@ func (x *TestStrucFlex) CodecEncodeSelf(e *Encoder) { if x.Mbu64 == nil { r.EncodeNil() } else { - h.encMapboolc3RydWN0IHt9((map[bool]struct{})(x.Mbu64), e) + h.encMapboolONaHEdLDOQQHWfI((map[bool]struct{})(x.Mbu64), e) } // end block: if x.Mbu64 map == nil } else { r.EncodeNil() @@ -11183,7 +11183,7 @@ func (x *TestStrucFlex) CodecEncodeSelf(e *Encoder) { if x.Mu8e == nil { r.EncodeNil() } else { - h.encMapuint8c3RydWN0IHt9((map[uint8]struct{})(x.Mu8e), e) + h.encMapuint8ONaHEdLDOQQHWfI((map[uint8]struct{})(x.Mu8e), e) } // end block: if x.Mu8e map == nil } else { r.EncodeNil() @@ -12152,7 +12152,7 @@ func (x *TestStrucFlex) CodecEncodeSelf(e *Encoder) { if x.Mbu64 == nil { r.EncodeNil() } else { - h.encMapboolc3RydWN0IHt9((map[bool]struct{})(x.Mbu64), e) + h.encMapboolONaHEdLDOQQHWfI((map[bool]struct{})(x.Mbu64), e) } // end block: if x.Mbu64 map == nil } if yyq2[66] { @@ -12394,7 +12394,7 @@ func (x *TestStrucFlex) CodecEncodeSelf(e *Encoder) { if x.Mu8e == nil { r.EncodeNil() } else { - h.encMapuint8c3RydWN0IHt9((map[uint8]struct{})(x.Mu8e), e) + h.encMapuint8ONaHEdLDOQQHWfI((map[uint8]struct{})(x.Mu8e), e) } // end block: if x.Mu8e map == nil } if yyq2[60] { @@ -13610,7 +13610,7 @@ func (x *TestStrucFlex) CodecEncodeSelf(e *Encoder) { if x.Mbu64 == nil { r.EncodeNil() } else { - h.encMapboolc3RydWN0IHt9((map[bool]struct{})(x.Mbu64), e) + h.encMapboolONaHEdLDOQQHWfI((map[bool]struct{})(x.Mbu64), e) } // end block: if x.Mbu64 map == nil } if yyq2[59] { @@ -13624,7 +13624,7 @@ func (x *TestStrucFlex) CodecEncodeSelf(e *Encoder) { if x.Mu8e == nil { r.EncodeNil() } else { - h.encMapuint8c3RydWN0IHt9((map[uint8]struct{})(x.Mu8e), e) + h.encMapuint8ONaHEdLDOQQHWfI((map[uint8]struct{})(x.Mu8e), e) } // end block: if x.Mu8e map == nil } if yyq2[60] { @@ -14361,9 +14361,9 @@ func (x *TestStrucFlex) codecDecodeSelfFromMap(l int, d *Decoder) { case "Mis": z.F.DecMapIntStringX(&x.Mis, d) case "Mbu64": - h.decMapboolc3RydWN0IHt9((*map[bool]struct{})(&x.Mbu64), d) + h.decMapboolONaHEdLDOQQHWfI((*map[bool]struct{})(&x.Mbu64), d) case "Mu8e": - h.decMapuint8c3RydWN0IHt9((*map[uint8]struct{})(&x.Mu8e), d) + h.decMapuint8ONaHEdLDOQQHWfI((*map[uint8]struct{})(&x.Mu8e), d) case "Mu8u64": h.decMapuint8stringUint64T((*map[uint8]stringUint64T)(&x.Mu8u64), d) case "Msp2ss": @@ -15097,7 +15097,7 @@ func (x *TestStrucFlex) codecDecodeSelfFromArray(l int, d *Decoder) { return } z.DecReadArrayElem() - h.decMapboolc3RydWN0IHt9((*map[bool]struct{})(&x.Mbu64), d) + h.decMapboolONaHEdLDOQQHWfI((*map[bool]struct{})(&x.Mbu64), d) yyj171++ yyb171 = !z.DecContainerNext(yyj171, l, yyhl171) if yyb171 { @@ -15105,7 +15105,7 @@ func (x *TestStrucFlex) codecDecodeSelfFromArray(l int, d *Decoder) { return } z.DecReadArrayElem() - h.decMapuint8c3RydWN0IHt9((*map[uint8]struct{})(&x.Mu8e), d) + h.decMapuint8ONaHEdLDOQQHWfI((*map[uint8]struct{})(&x.Mu8e), d) yyj171++ yyb171 = !z.DecContainerNext(yyj171, l, yyhl171) if yyb171 { @@ -16545,7 +16545,7 @@ func (x codecSelfer19780) decChanstring(v *chan string, d *Decoder) { } } -func (x codecSelfer19780) encMapboolc3RydWN0IHt9(v map[bool]struct{}, e *Encoder) { +func (x codecSelfer19780) encMapboolONaHEdLDOQQHWfI(v map[bool]struct{}, e *Encoder) { var h codecSelfer19780 z, r := GenHelper().Encoder(e) _, _, _ = h, z, r @@ -16566,7 +16566,7 @@ func (x codecSelfer19780) encMapboolc3RydWN0IHt9(v map[bool]struct{}, e *Encoder } } -func (x codecSelfer19780) decMapboolc3RydWN0IHt9(v *map[bool]struct{}, d *Decoder) { +func (x codecSelfer19780) decMapboolONaHEdLDOQQHWfI(v *map[bool]struct{}, d *Decoder) { var h codecSelfer19780 z, r := GenHelper().Decoder(d) _, _, _ = h, z, r @@ -16611,7 +16611,7 @@ func (x codecSelfer19780) decMapboolc3RydWN0IHt9(v *map[bool]struct{}, d *Decode } } -func (x codecSelfer19780) encMapuint8c3RydWN0IHt9(v map[uint8]struct{}, e *Encoder) { +func (x codecSelfer19780) encMapuint8ONaHEdLDOQQHWfI(v map[uint8]struct{}, e *Encoder) { var h codecSelfer19780 z, r := GenHelper().Encoder(e) _, _, _ = h, z, r @@ -16646,7 +16646,7 @@ func (x codecSelfer19780) encMapuint8c3RydWN0IHt9(v map[uint8]struct{}, e *Encod } } -func (x codecSelfer19780) decMapuint8c3RydWN0IHt9(v *map[uint8]struct{}, d *Decoder) { +func (x codecSelfer19780) decMapuint8ONaHEdLDOQQHWfI(v *map[uint8]struct{}, d *Decoder) { var h codecSelfer19780 z, r := GenHelper().Decoder(d) _, _, _ = h, z, r