zk/internal/core/util.go

29 lines
583 B
Go
Raw Normal View History

2021-04-18 14:37:54 +00:00
package core
// lazyStringer implements Stringer and wait for String() to be called the first
// time before computing its value.
type lazyStringer struct {
value *string
render func() string
}
func newLazyStringer(render func() string) *lazyStringer {
return &lazyStringer{render: render}
}
// String implements Stringer.
func (s *lazyStringer) String() string {
if s == nil {
return ""
}
if s.value == nil {
str := s.render()
s.value = &str
}
return *s.value
}
func (s *lazyStringer) MarshalJSON() ([]byte, error) {
return []byte(`"` + s.String() + `"`), nil
}