Skip to content

Commit

Permalink
style: channel use struct
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Nov 15, 2024
1 parent c757033 commit 748d66d
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 94 deletions.
6 changes: 3 additions & 3 deletions datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func NewSchema(schema *base.Schema) *Schema {
// any polymorphic properties need to be handled in their own threads
// any properties each need to be processed in their own thread.
// we go as fast as we can.
polyCompletedChan := make(chan bool)
polyCompletedChan := make(chan struct{})
errChan := make(chan error)

type buildResult struct {
Expand All @@ -340,7 +340,7 @@ func NewSchema(schema *base.Schema) *Schema {

// schema async
buildOutSchemas := func(schemas []lowmodel.ValueReference[*base.SchemaProxy], items *[]*SchemaProxy,
doneChan chan bool, e chan error,
doneChan chan struct{}, e chan error,
) {
bChan := make(chan buildResult)
totalSchemas := len(schemas)
Expand All @@ -353,7 +353,7 @@ func NewSchema(schema *base.Schema) *Schema {
j++
(*items)[r.idx] = r.s
}
doneChan <- true
doneChan <- struct{}{}
}

// props async
Expand Down
4 changes: 2 additions & 2 deletions datamodel/low/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1876,14 +1876,14 @@ func TestBuildSchema_BadNodeTypes(t *testing.T) {
}

eChan := make(chan error, 1)
doneChan := make(chan bool, 1)
doneChan := make(chan struct{}, 1)
bChan := make(chan schemaProxyBuildResult, 1)
var err error
go func() {
for {
e := <-eChan
err = e
doneChan <- true
doneChan <- struct{}{}
}
}()

Expand Down
6 changes: 3 additions & 3 deletions datamodel/low/v2/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ func (p *PathItem) Build(ctx context.Context, _, root *yaml.Node, idx *index.Spe

// all operations have been superficially built,
// now we need to build out the operation, we will do this asynchronously for speed.
opBuildChan := make(chan bool)
opBuildChan := make(chan struct{})
opErrorChan := make(chan error)

var buildOpFunc = func(op low.NodeReference[*Operation], ch chan<- bool, errCh chan<- error) {
var buildOpFunc = func(op low.NodeReference[*Operation], ch chan<- struct{}, errCh chan<- error) {
er := op.Value.Build(ctx, op.KeyNode, op.ValueNode, idx)
if er != nil {
errCh <- er
}
ch <- true
ch <- struct{}{}
}

if len(ops) <= 0 {
Expand Down
36 changes: 18 additions & 18 deletions datamodel/low/v2/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// processes a property of a Swagger document asynchronously using bool and error channels for signals.
type documentFunction func(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error)
type documentFunction func(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error)

// Swagger represents a high-level Swagger / OpenAPI 2 document. An instance of Swagger is the root of the specification.
type Swagger struct {
Expand Down Expand Up @@ -234,7 +234,7 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
extractTags,
extractSecurity,
}
doneChan := make(chan bool)
doneChan := make(chan struct{})
errChan := make(chan error)
for i := range extractionFuncs {
go extractionFuncs[i](ctx, info.RootNode.Content[0], &doc, rolodex.GetRootIndex(), doneChan, errChan)
Expand All @@ -261,67 +261,67 @@ func (s *Swagger) GetExternalDocs() *low.NodeReference[any] {
}
}

func extractInfo(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractInfo(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
info, err := low.ExtractObject[*base.Info](ctx, base.InfoLabel, root, idx)
if err != nil {
e <- err
return
}
doc.Info = info
c <- true
c <- struct{}{}
}

func extractPaths(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractPaths(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
paths, err := low.ExtractObject[*Paths](ctx, PathsLabel, root, idx)
if err != nil {
e <- err
return
}
doc.Paths = paths
c <- true
c <- struct{}{}
}

func extractDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
def, err := low.ExtractObject[*Definitions](ctx, DefinitionsLabel, root, idx)
if err != nil {
e <- err
return
}
doc.Definitions = def
c <- true
c <- struct{}{}
}

func extractParamDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractParamDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
param, err := low.ExtractObject[*ParameterDefinitions](ctx, ParametersLabel, root, idx)
if err != nil {
e <- err
return
}
doc.Parameters = param
c <- true
c <- struct{}{}
}

func extractResponsesDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractResponsesDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
resp, err := low.ExtractObject[*ResponsesDefinitions](ctx, ResponsesLabel, root, idx)
if err != nil {
e <- err
return
}
doc.Responses = resp
c <- true
c <- struct{}{}
}

func extractSecurityDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractSecurityDefinitions(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
sec, err := low.ExtractObject[*SecurityDefinitions](ctx, SecurityDefinitionsLabel, root, idx)
if err != nil {
e <- err
return
}
doc.SecurityDefinitions = sec
c <- true
c <- struct{}{}
}

func extractTags(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractTags(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
tags, ln, vn, err := low.ExtractArray[*base.Tag](ctx, base.TagsLabel, root, idx)
if err != nil {
e <- err
Expand All @@ -332,10 +332,10 @@ func extractTags(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.
KeyNode: ln,
ValueNode: vn,
}
c <- true
c <- struct{}{}
}

func extractSecurity(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- bool, e chan<- error) {
func extractSecurity(ctx context.Context, root *yaml.Node, doc *Swagger, idx *index.SpecIndex, c chan<- struct{}, e chan<- error) {
sec, ln, vn, err := low.ExtractArray[*base.SecurityRequirement](ctx, SecurityLabel, root, idx)
if err != nil {
e <- err
Expand All @@ -346,5 +346,5 @@ func extractSecurity(ctx context.Context, root *yaml.Node, doc *Swagger, idx *in
KeyNode: ln,
ValueNode: vn,
}
c <- true
c <- struct{}{}
}
6 changes: 3 additions & 3 deletions datamodel/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,9 @@ func TestTranslatePipeline(t *testing.T) {

ctx, c := context.WithTimeout(context.Background(), 5*time.Second)
defer c()
doneChan := make(chan bool)
doneChan := make(chan struct{})

go func(completedChan chan bool) {
go func(completedChan chan struct{}) {

const concurrency = 2
in := make(chan int)
Expand Down Expand Up @@ -532,7 +532,7 @@ func TestTranslatePipeline(t *testing.T) {
close(done)
wg.Wait()
require.Error(t, err)
doneChan <- true
doneChan <- struct{}{}
}(doneChan)

select {
Expand Down
8 changes: 4 additions & 4 deletions index/extract_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,9 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
var found []*Reference

// run this async because when things get recursive, it can take a while
var c chan bool
var c chan struct{}
if !index.config.ExtractRefsSequentially {
c = make(chan bool)
c = make(chan struct{})
}

locate := func(ref *Reference, refIndex int, sequence []*ReferenceMapped) {
Expand All @@ -638,7 +638,7 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
}
sequence[refIndex] = rm
if !index.config.ExtractRefsSequentially {
c <- true
c <- struct{}{}
}
index.refLock.Unlock()
} else {
Expand Down Expand Up @@ -684,7 +684,7 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
index.errorLock.Unlock()
}
if !index.config.ExtractRefsSequentially {
c <- true
c <- struct{}{}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions index/index_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ type SpecIndex struct {
arrayCircularReferences []*CircularReferenceResult // only available when the resolver has been used.
allowCircularReferences bool // decide if you want to error out, or allow circular references, default is false.
config *SpecIndexConfig // configuration for the index
componentIndexChan chan bool
polyComponentIndexChan chan bool
componentIndexChan chan struct{}
polyComponentIndexChan chan struct{}
resolver *Resolver
cache *sync.Map
built bool
uri []string
logger *slog.Logger
nodeMap map[int]map[int]*yaml.Node
nodeMapCompleted chan bool
nodeMapCompleted chan struct{}
pendingResolve []refMap
highModelCache Cache
}
Expand Down
4 changes: 2 additions & 2 deletions index/index_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ func boostrapIndexCollections(index *SpecIndex) {
index.polymorphicRefs = make(map[string]*Reference)
index.refsWithSiblings = make(map[string]Reference)
index.opServersRefs = make(map[string]map[string][]*Reference)
index.componentIndexChan = make(chan bool)
index.polyComponentIndexChan = make(chan bool)
index.componentIndexChan = make(chan struct{})
index.polyComponentIndexChan = make(chan struct{})
}
6 changes: 3 additions & 3 deletions index/map_index_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func (index *SpecIndex) GetNode(line int, column int) (*yaml.Node, bool) {

// MapNodes maps all nodes in the document to a map of line/column to node.
func (index *SpecIndex) MapNodes(rootNode *yaml.Node) {
cruising := make(chan bool)
cruising := make(chan struct{})
nodeChan := make(chan *nodeMap)
go func(nodeChan chan *nodeMap) {
done := false
for !done {
node, ok := <-nodeChan
if !ok {
done = true
cruising <- true
cruising <- struct{}{}
return
}
if index.nodeMap[node.line] == nil {
Expand All @@ -66,7 +66,7 @@ func (index *SpecIndex) MapNodes(rootNode *yaml.Node) {
go enjoyALuxuryCruise(rootNode, nodeChan, true)
<-cruising
close(cruising)
index.nodeMapCompleted <- true
index.nodeMapCompleted <- struct{}{}
close(index.nodeMapCompleted)
}

Expand Down
Loading

0 comments on commit 748d66d

Please sign in to comment.