File: stream_visitor_lazy.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (30 lines) | stat: -rw-r--r-- 588 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package grpctool

import (
	"sync"

	"google.golang.org/protobuf/proto"
)

type LazyStreamVisitor struct {
	streamMessage proto.Message
	once          sync.Once
	sv            *StreamVisitor
}

func NewLazyStreamVisitor(streamMessage proto.Message) *LazyStreamVisitor {
	return &LazyStreamVisitor{
		streamMessage: streamMessage,
	}
}

func (v *LazyStreamVisitor) Get() *StreamVisitor {
	v.once.Do(func() {
		var err error
		v.sv, err = NewStreamVisitor(v.streamMessage)
		if err != nil {
			panic(err) // this will never panic as long as the proto file is correct
		}
	})
	return v.sv
}