Skip to content

Commit

Permalink
Refactor more function return values
Browse files Browse the repository at this point in the history
  • Loading branch information
tsavola committed Nov 11, 2022
1 parent e6295a0 commit 657cf63
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
6 changes: 3 additions & 3 deletions gate/image/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,11 @@ func putString(dest []byte, s string) (tail []byte) {
return dest[len(s):]
}

func putVaruint32Before(dest []byte, offset int, x uint32) (n int) {
func putVaruint32Before(dest []byte, offset int, x uint32) int {
var temp [binary.MaxVarintLen32]byte
n = binary.PutUvarint(temp[:], uint64(x))
n := binary.PutUvarint(temp[:], uint64(x))
copy(dest[offset-n:], temp[:n])
return
return n
}

func mapOldSection(offset int64, dest, src []*manifest.ByteRange, i section.ID) int64 {
Expand Down
13 changes: 7 additions & 6 deletions gate/packet/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (code Code) String() string {
}
}

func (b Buf) String() (s string) {
func (b Buf) String() string {
var (
size string
index string
Expand All @@ -46,7 +46,7 @@ func (b Buf) String() (s string) {
index = fmt.Sprintf(" index=%d", i)
}

s = fmt.Sprintf("size=%s code=%s domain=%s%s", size, b.Code(), b.Domain(), index)
s := fmt.Sprintf("size=%s code=%s domain=%s%s", size, b.Code(), b.Domain(), index)

switch b.Domain() {
case DomainFlow:
Expand All @@ -55,7 +55,8 @@ func (b Buf) String() (s string) {
case DomainData:
s += DataBuf(b).string()
}
return

return s
}

func (b FlowBuf) String() string {
Expand All @@ -74,13 +75,13 @@ func (b DataBuf) String() string {
return Buf(b).String() + b.string()
}

func (b DataBuf) string() (s string) {
s = fmt.Sprintf(" id=%d", b.ID())
func (b DataBuf) string() string {
s := fmt.Sprintf(" id=%d", b.ID())
if n := b.DataLen(); n > 0 {
s += fmt.Sprintf(" datalen=%d", n)
}
if x := b.Note(); x != 0 {
s += fmt.Sprintf(" note=%d", x)
}
return
return s
}
24 changes: 12 additions & 12 deletions gate/runtime/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ import (
"gate.computer/internal/file"
)

func popServiceBuffers(frozen *snapshot.Buffers) (services []snapshot.Service) {
func popServiceBuffers(frozen *snapshot.Buffers) []snapshot.Service {
if frozen == nil {
return
return nil
}

services = frozen.Services
ss := frozen.Services
frozen.Services = nil
return
return ss
}

func popInputBuffer(frozen *snapshot.Buffers) (input []byte) {
func popInputBuffer(frozen *snapshot.Buffers) []byte {
if frozen == nil {
return
return nil
}

input = frozen.Input
b := frozen.Input
frozen.Input = nil
return
return b
}

func popOutputBuffer(frozen *snapshot.Buffers) (output []byte) {
func popOutputBuffer(frozen *snapshot.Buffers) []byte {
if frozen == nil {
return
return nil
}

output = frozen.Output
b := frozen.Output
frozen.Output = nil
return
return b
}

type read struct {
Expand Down
6 changes: 3 additions & 3 deletions gate/runtime/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ func (discoverer *serviceDiscoverer) checkPacket(p packet.Buf) (packet.Buf, erro
return p, nil
}

func makeServicesPacket(domain packet.Domain, services []ServiceState) (resp packet.Buf) {
resp = packet.Make(packet.CodeServices, domain, packet.ServicesHeaderSize+len(services))
func makeServicesPacket(domain packet.Domain, services []ServiceState) packet.Buf {
resp := packet.Make(packet.CodeServices, domain, packet.ServicesHeaderSize+len(services))
resp.SetSize()
binary.LittleEndian.PutUint16(resp[packet.OffsetServicesCount:], uint16(len(services)))

for i, s := range services {
resp[packet.ServicesHeaderSize+i] = s.flags
}

return
return resp
}
6 changes: 3 additions & 3 deletions gate/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ func newAccount(pri *principal.ID) *account {
}
}

func (acc *account) shutdown(lock serverLock) (is map[string]accountInstance) {
func (acc *account) shutdown(lock serverLock) map[string]accountInstance {
ps := acc.programs
acc.programs = nil

for prog := range ps {
prog.unref(lock)
}

is = acc.instances
is := acc.instances
acc.instances = nil
return
return is
}

// ensureProgramRef adds program reference unless already found. It must not
Expand Down
6 changes: 3 additions & 3 deletions gate/server/web/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ func popOptionalActionParam(w http.ResponseWriter, r *http.Request, s *webserver
return false
}

func mustPopOptionalLastFunctionParam(w http.ResponseWriter, r *http.Request, s *webserver, query url.Values) (value string) {
value = popOptionalLastParam(w, r, s, query, api.ParamFunction)
func mustPopOptionalLastFunctionParam(w http.ResponseWriter, r *http.Request, s *webserver, query url.Values) string {
value := popOptionalLastParam(w, r, s, query, api.ParamFunction)
if value != "" && !api.FunctionRegexp.MatchString(value) {
respondInvalidFunction(w, r, s, value)
panic(responded)
}
return
return value
}

func mustNotHaveParams(w http.ResponseWriter, r *http.Request, s *webserver, query url.Values) {
Expand Down
17 changes: 8 additions & 9 deletions gate/service/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,19 @@ func (r *Registry) Clone() *Registry {

// Catalog of service metadata. Only the services which are discoverable in
// this context are included.
func (r *Registry) Catalog(ctx context.Context) (services []Service) {
func (r *Registry) Catalog(ctx context.Context) []Service {
m := make(map[string]string)
r.catalog(ctx, m)

services := make([]Service, 0, len(m))

for name, revision := range m {
if revision != "" {
services = append(services, Service{name, revision})
}
}

return
return services
}

func (r *Registry) catalog(ctx context.Context, m map[string]string) {
Expand All @@ -168,18 +170,15 @@ func (r *Registry) catalog(ctx context.Context, m map[string]string) {
}
}

func (r *Registry) lookup(name string) (result Factory) {
func (r *Registry) lookup(name string) Factory {
for {
var found bool

result, found = r.factories[name]
if found {
return
if f, found := r.factories[name]; found {
return f
}

r = r.parent
if r == nil {
return
return nil
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions grpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,17 @@ func receiveForward(ctx context.Context, code packet.Code, out chan<- packet.Thu
}
}

func receiveBuffer(initial packet.Buf, stream api.Instance_ReceiveClient, abort func(error)) (buf []byte) {
func receiveBuffer(initial packet.Buf, stream api.Instance_ReceiveClient, abort func(error)) []byte {
initial.SetSize()
buf = initial
buf := initial

for {
r, err := stream.Recv()
if err != nil {
if err != io.EOF {
abort(err)
}
return
return buf
}

p := mustBePacket(r.Value, abort)
Expand Down

0 comments on commit 657cf63

Please sign in to comment.