cmd/viewer: add support for map of structs without pointers
This adds support for fields like `map[string]netaddr.IPPrefix`. Signed-off-by: Maisem Ali <maisem@tailscale.com>pull/5289/head
parent
adc5ffea99
commit
26f103473c
|
@ -20,13 +20,14 @@ type StructWithoutPtrs struct {
|
||||||
type Map struct {
|
type Map struct {
|
||||||
Int map[string]int
|
Int map[string]int
|
||||||
SliceInt map[string][]int
|
SliceInt map[string][]int
|
||||||
StructWithPtr map[string]*StructWithPtrs
|
StructPtrWithPtr map[string]*StructWithPtrs
|
||||||
StructWithoutPtr map[string]*StructWithoutPtrs
|
StructPtrWithoutPtr map[string]*StructWithoutPtrs
|
||||||
|
StructWithoutPtr map[string]StructWithoutPtrs
|
||||||
SlicesWithPtrs map[string][]*StructWithPtrs
|
SlicesWithPtrs map[string][]*StructWithPtrs
|
||||||
SlicesWithoutPtrs map[string][]*StructWithoutPtrs
|
SlicesWithoutPtrs map[string][]*StructWithoutPtrs
|
||||||
StructWithoutPtrKey map[StructWithoutPtrs]int `json:"-"`
|
StructWithoutPtrKey map[StructWithoutPtrs]int `json:"-"`
|
||||||
|
|
||||||
// Unsupported.
|
// Unsupported views.
|
||||||
SliceIntPtr map[string][]*int
|
SliceIntPtr map[string][]*int
|
||||||
PointerKey map[*string]int `json:"-"`
|
PointerKey map[*string]int `json:"-"`
|
||||||
StructWithPtrKey map[StructWithPtrs]int `json:"-"`
|
StructWithPtrKey map[StructWithPtrs]int `json:"-"`
|
||||||
|
|
|
@ -73,16 +73,22 @@ func (src *Map) Clone() *Map {
|
||||||
dst.SliceInt[k] = append([]int{}, src.SliceInt[k]...)
|
dst.SliceInt[k] = append([]int{}, src.SliceInt[k]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if dst.StructWithPtr != nil {
|
if dst.StructPtrWithPtr != nil {
|
||||||
dst.StructWithPtr = map[string]*StructWithPtrs{}
|
dst.StructPtrWithPtr = map[string]*StructWithPtrs{}
|
||||||
for k, v := range src.StructWithPtr {
|
for k, v := range src.StructPtrWithPtr {
|
||||||
dst.StructWithPtr[k] = v.Clone()
|
dst.StructPtrWithPtr[k] = v.Clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if dst.StructPtrWithoutPtr != nil {
|
||||||
|
dst.StructPtrWithoutPtr = map[string]*StructWithoutPtrs{}
|
||||||
|
for k, v := range src.StructPtrWithoutPtr {
|
||||||
|
dst.StructPtrWithoutPtr[k] = v.Clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if dst.StructWithoutPtr != nil {
|
if dst.StructWithoutPtr != nil {
|
||||||
dst.StructWithoutPtr = map[string]*StructWithoutPtrs{}
|
dst.StructWithoutPtr = map[string]StructWithoutPtrs{}
|
||||||
for k, v := range src.StructWithoutPtr {
|
for k, v := range src.StructWithoutPtr {
|
||||||
dst.StructWithoutPtr[k] = v.Clone()
|
dst.StructWithoutPtr[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if dst.SlicesWithPtrs != nil {
|
if dst.SlicesWithPtrs != nil {
|
||||||
|
@ -128,8 +134,9 @@ func (src *Map) Clone() *Map {
|
||||||
var _MapCloneNeedsRegeneration = Map(struct {
|
var _MapCloneNeedsRegeneration = Map(struct {
|
||||||
Int map[string]int
|
Int map[string]int
|
||||||
SliceInt map[string][]int
|
SliceInt map[string][]int
|
||||||
StructWithPtr map[string]*StructWithPtrs
|
StructPtrWithPtr map[string]*StructWithPtrs
|
||||||
StructWithoutPtr map[string]*StructWithoutPtrs
|
StructPtrWithoutPtr map[string]*StructWithoutPtrs
|
||||||
|
StructWithoutPtr map[string]StructWithoutPtrs
|
||||||
SlicesWithPtrs map[string][]*StructWithPtrs
|
SlicesWithPtrs map[string][]*StructWithPtrs
|
||||||
SlicesWithoutPtrs map[string][]*StructWithoutPtrs
|
SlicesWithoutPtrs map[string][]*StructWithoutPtrs
|
||||||
StructWithoutPtrKey map[StructWithoutPtrs]int
|
StructWithoutPtrKey map[StructWithoutPtrs]int
|
||||||
|
|
|
@ -196,18 +196,22 @@ func (v MapView) SliceInt() views.MapFn[string, []int, views.Slice[int]] {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v MapView) StructWithPtr() views.MapFn[string, *StructWithPtrs, StructWithPtrsView] {
|
func (v MapView) StructPtrWithPtr() views.MapFn[string, *StructWithPtrs, StructWithPtrsView] {
|
||||||
return views.MapFnOf(v.ж.StructWithPtr, func(t *StructWithPtrs) StructWithPtrsView {
|
return views.MapFnOf(v.ж.StructPtrWithPtr, func(t *StructWithPtrs) StructWithPtrsView {
|
||||||
return t.View()
|
return t.View()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v MapView) StructWithoutPtr() views.MapFn[string, *StructWithoutPtrs, StructWithoutPtrsView] {
|
func (v MapView) StructPtrWithoutPtr() views.MapFn[string, *StructWithoutPtrs, StructWithoutPtrsView] {
|
||||||
return views.MapFnOf(v.ж.StructWithoutPtr, func(t *StructWithoutPtrs) StructWithoutPtrsView {
|
return views.MapFnOf(v.ж.StructPtrWithoutPtr, func(t *StructWithoutPtrs) StructWithoutPtrsView {
|
||||||
return t.View()
|
return t.View()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v MapView) StructWithoutPtr() views.Map[string, StructWithoutPtrs] {
|
||||||
|
return views.MapOf(v.ж.StructWithoutPtr)
|
||||||
|
}
|
||||||
|
|
||||||
func (v MapView) SlicesWithPtrs() views.MapFn[string, []*StructWithPtrs, views.SliceView[*StructWithPtrs, StructWithPtrsView]] {
|
func (v MapView) SlicesWithPtrs() views.MapFn[string, []*StructWithPtrs, views.SliceView[*StructWithPtrs, StructWithPtrsView]] {
|
||||||
return views.MapFnOf(v.ж.SlicesWithPtrs, func(t []*StructWithPtrs) views.SliceView[*StructWithPtrs, StructWithPtrsView] {
|
return views.MapFnOf(v.ж.SlicesWithPtrs, func(t []*StructWithPtrs) views.SliceView[*StructWithPtrs, StructWithPtrsView] {
|
||||||
return views.SliceOfViews[*StructWithPtrs, StructWithPtrsView](t)
|
return views.SliceOfViews[*StructWithPtrs, StructWithPtrsView](t)
|
||||||
|
@ -231,8 +235,9 @@ func (v MapView) StructWithPtrKey() map[StructWithPtrs]int { panic("unsupported"
|
||||||
var _MapViewNeedsRegeneration = Map(struct {
|
var _MapViewNeedsRegeneration = Map(struct {
|
||||||
Int map[string]int
|
Int map[string]int
|
||||||
SliceInt map[string][]int
|
SliceInt map[string][]int
|
||||||
StructWithPtr map[string]*StructWithPtrs
|
StructPtrWithPtr map[string]*StructWithPtrs
|
||||||
StructWithoutPtr map[string]*StructWithoutPtrs
|
StructPtrWithoutPtr map[string]*StructWithoutPtrs
|
||||||
|
StructWithoutPtr map[string]StructWithoutPtrs
|
||||||
SlicesWithPtrs map[string][]*StructWithPtrs
|
SlicesWithPtrs map[string][]*StructWithPtrs
|
||||||
SlicesWithoutPtrs map[string][]*StructWithoutPtrs
|
SlicesWithoutPtrs map[string][]*StructWithoutPtrs
|
||||||
StructWithoutPtrKey map[StructWithoutPtrs]int
|
StructWithoutPtrKey map[StructWithoutPtrs]int
|
||||||
|
|
|
@ -224,6 +224,15 @@ func genView(buf *bytes.Buffer, it *codegen.ImportTracker, typ *types.Named, thi
|
||||||
mElem := m.Elem()
|
mElem := m.Elem()
|
||||||
var template string
|
var template string
|
||||||
switch u := mElem.(type) {
|
switch u := mElem.(type) {
|
||||||
|
case *types.Struct, *types.Named:
|
||||||
|
strucT := u
|
||||||
|
args.FieldType = it.QualifiedName(fieldType)
|
||||||
|
if codegen.ContainsPointers(strucT) {
|
||||||
|
writeTemplate("unsupportedField")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
template = "mapField"
|
||||||
|
args.MapValueType = it.QualifiedName(mElem)
|
||||||
case *types.Basic:
|
case *types.Basic:
|
||||||
template = "mapField"
|
template = "mapField"
|
||||||
args.MapValueType = it.QualifiedName(mElem)
|
args.MapValueType = it.QualifiedName(mElem)
|
||||||
|
|
Loading…
Reference in New Issue