Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Emit events (#11)
Browse files Browse the repository at this point in the history
* Add event to protocol.

* Revert ids to strings until other stuff is merged into master.

* Add $event vertices for project and documents.

* Remove todo note.

* Collapse arg types.
  • Loading branch information
efritz committed Jul 31, 2019
1 parent 288a3b1 commit f256046
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
32 changes: 32 additions & 0 deletions export/exporter.go
Expand Up @@ -75,6 +75,11 @@ func (e *exporter) export(info protocol.ToolInfo) error {
return fmt.Errorf(`emit "project": %v`, err)
}

_, err = e.emitBeginEvent("project", proID)
if err != nil {
return fmt.Errorf(`emit "begin": %v`, err)
}

for _, p := range e.pkgs {
if err := e.exportPkg(p, proID); err != nil {
return fmt.Errorf("export package %q: %v", p.Name, err)
Expand Down Expand Up @@ -114,6 +119,11 @@ func (e *exporter) export(info protocol.ToolInfo) error {
}
}

_, err = e.emitEndEvent("project", proID)
if err != nil {
return fmt.Errorf(`emit "end": %v`, err)
}

return nil
}

Expand All @@ -133,6 +143,11 @@ func (e *exporter) exportPkg(p *packages.Package, proID string) (err error) {
return fmt.Errorf(`emit "document": %v`, err)
}

_, err = e.emitBeginEvent("document", docID)
if err != nil {
return fmt.Errorf(`emit "begin": %v`, err)
}

_, err = e.emitContains(proID, []string{docID})
if err != nil {
return fmt.Errorf(`emit "contains": %v`, err)
Expand All @@ -151,6 +166,13 @@ func (e *exporter) exportPkg(p *packages.Package, proID string) (err error) {
}
}

for _, info := range e.files {
_, err = e.emitEndEvent("document", info.docID)
if err != nil {
return fmt.Errorf(`emit "end": %v`, err)
}
}

return nil
}

Expand Down Expand Up @@ -431,6 +453,16 @@ func (e *exporter) emitMetaData(root string, info protocol.ToolInfo) (string, er
return id, e.emit(protocol.NewMetaData(id, root, info))
}

func (e *exporter) emitBeginEvent(scope string, data string) (string, error) {
id := e.nextID()
return id, e.emit(protocol.NewEvent(id, "begin", scope, data))
}

func (e *exporter) emitEndEvent(scope string, data string) (string, error) {
id := e.nextID()
return id, e.emit(protocol.NewEvent(id, "end", scope, data))
}

func (e *exporter) emitProject() (string, error) {
id := e.nextID()
return id, e.emit(protocol.NewProject(id))
Expand Down
29 changes: 29 additions & 0 deletions protocol/protocol.go
Expand Up @@ -110,6 +110,35 @@ func NewMetaData(id, root string, info ToolInfo) *MetaData {
}
}

// Event is optional metadata emitted to give hints to consumers about
// the beginning and ending of new "socpes" (e.g. a project or document).
type Event struct {
Vertex
// The kind of event (begin or end).
Kind string `json:"kind"`
// The type of element this event describes (project or document).
Scope string `json:"scope"`
// The identifier of the data beginning or ending.
Data string `json:"data"`
}

// NewEvent returns a new Event object with the given ID, kind, scope,
// and data information.
func NewEvent(id, kind, scope, data string) *Event {
return &Event{
Vertex: Vertex{
Element: Element{
ID: id,
Type: ElementVertex,
},
Label: VertexEvent,
},
Kind: kind,
Scope: scope,
Data: data,
}
}

// Project declares the language of the dump.
type Project struct {
Vertex
Expand Down

0 comments on commit f256046

Please sign in to comment.