Skip to content

Commit

Permalink
Intermediate commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bdragon300 committed Dec 10, 2024
1 parent a1f744b commit 22640f3
Show file tree
Hide file tree
Showing 90 changed files with 918 additions and 748 deletions.
18 changes: 17 additions & 1 deletion assets/default_config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
render:
selections: []
selections:
- objectKindRe: schema
file: schemas/{{.Name | toSnakeCase}}.go
- objectKindRe: parameter
file: parameters/{{.Name | toSnakeCase}}.go
- objectKindRe: channel
file: channels/{{.Name | toSnakeCase}}.go
- objectKindRe: protoChannel
file: channels/{{.Name | toSnakeCase}}.go
- objectKindRe: server
file: servers/{{.Name | toSnakeCase}}.go
- objectKindRe: protoServer
file: servers/{{.Name | toSnakeCase}}.go
- objectKindRe: message
file: messages/{{.Name | toSnakeCase}}.go
- objectKindRe: protoMessage
file: messages/{{.Name | toSnakeCase}}.go
2 changes: 1 addition & 1 deletion assets/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import (
)

//go:embed *
var AssetsFS embed.FS
var AssetFS embed.FS
21 changes: 13 additions & 8 deletions cmd/go-asyncapi/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ import (
"golang.org/x/mod/modfile"
)

const defaultConfigFileName = "default_config.yaml"
const (
defaultConfigFileName = "default_config.yaml"
defaultPackage = "main"
defaultTemplate = "main.tmpl"
)

type GenerateCmd struct {
Pub *generatePubSubArgs `arg:"subcommand:pub" help:"Generate only the publisher code"`
Expand Down Expand Up @@ -155,10 +159,11 @@ func generate(cmd *GenerateCmd) error {
return fmt.Errorf("schema render: %w", err)
}

// TODO: uncomment
// Formatting
if err = writer.FormatFiles(files); err != nil {
return fmt.Errorf("formatting code: %w", err)
}
//if err = writer.FormatFiles(files); err != nil {
// return fmt.Errorf("formatting code: %w", err)
//}

// Writing
if err = writer.WriteToFiles(files, cmd.TargetDir); err != nil {
Expand Down Expand Up @@ -432,8 +437,8 @@ func getRenderOpts(opts generatePubSubArgs, targetDir string) (common.RenderOpts
return res, err
}
for _, item := range conf.Render.Selections {
pkg, _ := lo.Coalesce(item.Package, lo.Ternary(targetDir != "", path.Base(targetDir), "main"))
templateName, _ := lo.Coalesce(item.Template, "main")
pkg, _ := lo.Coalesce(item.Package, lo.Ternary(targetDir != "", path.Base(targetDir), defaultPackage))
templateName, _ := lo.Coalesce(item.Template,defaultTemplate)
sel := common.RenderSelectionConfig{
Template: templateName,
File: item.File,
Expand Down Expand Up @@ -468,7 +473,7 @@ func loadConfig(fileName string) (toolConfig, error) {
var f io.ReadCloser
var err error
if fileName == "" {
f, err = assets.AssetsFS.Open(defaultConfigFileName)
f, err = assets.AssetFS.Open(defaultConfigFileName)
if err != nil {
return conf, fmt.Errorf("cannot open default config file in assets, this is a programming error: %w", err)
}
Expand Down Expand Up @@ -506,7 +511,7 @@ func protocolBuilders() map[string]asyncapi.ProtocolBuilder {
}

func getImplementationsManifest() (implementations.ImplManifest, error) {
f, err := implementations.ImplementationsFS.Open("manifest.json")
f, err := implementations.ImplementationFS.Open("manifest.json")
if err != nil {
return nil, fmt.Errorf("cannot open manifest.json: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion implementations/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

//go:embed *
var ImplementationsFS embed.FS
var ImplementationFS embed.FS

type ImplManifestItem struct {
URL string `json:"url"`
Expand Down
4 changes: 2 additions & 2 deletions internal/asyncapi/asyncapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (a AsyncAPI) Compile(ctx *common.CompileContext) error {
}

func (a AsyncAPI) build(ctx *common.CompileContext) *render.AsyncAPI {
allMessagesPrm := lang.NewListCbPromise[*render.Message](func(item common.Renderable, _ []string) bool {
_, ok := item.(*render.Message)
allMessagesPrm := lang.NewListCbPromise[*render.Message](func(item common.CompileObject, _ []string) bool {
_, ok := item.Renderable.(*render.Message)
return ok
})
ctx.PutListPromise(allMessagesPrm)
Expand Down
16 changes: 12 additions & 4 deletions internal/asyncapi/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (c Channel) Compile(ctx *common.CompileContext) error {
return err
}
ctx.PutObject(obj)
if v, ok := obj.(*render.ProtoChannel); ok {
ctx.Logger.Trace("ProtoObjects", "object", obj)
for _, protoObj := range v.ProtoChannels {
ctx.PutObject(protoObj)
}
}
return nil
}

Expand Down Expand Up @@ -89,21 +95,23 @@ func (c Channel) buildChannels(ctx *common.CompileContext, channelKey string) (c
if c.Servers != nil {
ctx.Logger.Trace("Channel servers", "names", *c.Servers)
res.SpecServerNames = *c.Servers
prm := lang.NewListCbPromise[*render.Server](func(item common.Renderable, path []string) bool {
srv, ok := item.(*render.Server)
prm := lang.NewListCbPromise[*render.Server](func(item common.CompileObject, path []string) bool {
srv, ok := item.Renderable.(*render.Server)
if !ok {
return false
}
return lo.Contains(*c.Servers, srv.Name)
})
res.ServersPromise = prm
ctx.PutListPromise(prm)
} else {
ctx.Logger.Trace("Channel for all servers")
prm := lang.NewListCbPromise[*render.Server](func(item common.Renderable, path []string) bool {
_, ok := item.(*render.Server)
prm := lang.NewListCbPromise[*render.Server](func(item common.CompileObject, path []string) bool {
_, ok := item.Renderable.(*render.Server)
return ok
})
res.ServersPromise = prm
ctx.PutListPromise(prm)
}

// Channel/operation bindings
Expand Down
2 changes: 1 addition & 1 deletion internal/asyncapi/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ type ComponentsItem struct {
ChannelBindings types.OrderedMap[string, ChannelBindings] `json:"channelBindings" yaml:"channelBindings" cgen:"components"`
OperationBindings types.OrderedMap[string, OperationBinding] `json:"operationBindings" yaml:"operationBindings" cgen:"components"`
MessageBindings types.OrderedMap[string, MessageBindings] `json:"messageBindings" yaml:"messageBindings" cgen:"components"`
}
}
24 changes: 19 additions & 5 deletions internal/asyncapi/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ func (m Message) Compile(ctx *common.CompileContext) error {
return err
}
ctx.PutObject(obj)
if v, ok := obj.(*render.ProtoMessage); ok {
ctx.Logger.Trace("ProtoObjects", "object", obj)
for _, protoObj := range v.ProtoMessages {
ctx.PutObject(protoObj)
}
}
return nil
}

func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Renderable, error) {
_, isComponent := ctx.Stack.Top().Flags[common.SchemaTagComponent]
ignore := m.XIgnore || isComponent //&& !ctx.CompileOpts.MessageOpts.IsAllowedName(messageKey))
//_, isComponent := ctx.Stack.Top().Flags[common.SchemaTagComponent]
ignore := m.XIgnore //|| (isComponent && !ctx.CompileOpts.MessageOpts.IsAllowedName(messageKey))
if ignore {
ctx.Logger.Debug("Message denoted to be ignored")
return &render.Message{Dummy: true}, nil
Expand Down Expand Up @@ -95,11 +101,19 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
ctx.Logger.Trace(fmt.Sprintf("Message content type is %q", res.ContentType))

// Lookup servers after linking to figure out all protocols the message is used in
prm := lang.NewListCbPromise[*render.Server](func(item common.Renderable, path []string) bool {
_, ok := item.(*render.Server)
prm := lang.NewListCbPromise[*render.Server](func(item common.CompileObject, path []string) bool {
_, ok := item.Renderable.(*render.Server)
return ok
})
res.AllServersPromise = prm
ctx.PutListPromise(prm)

prm2 := lang.NewCbPromise[*render.AsyncAPI](func(item common.CompileObject, path []string) bool {
_, ok := item.Renderable.(*render.AsyncAPI)
return ok
})
res.AsyncAPIPromise = prm2
ctx.PutPromise(prm2)

// Link to Headers struct if any
if m.Headers != nil {
Expand Down Expand Up @@ -147,7 +161,7 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
}
res.ProtoMessages = protoMessages

return res, nil
return &res, nil
}

func (m Message) setStructFields(ctx *common.CompileContext, langMessage *render.Message) {
Expand Down
12 changes: 6 additions & 6 deletions internal/asyncapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (o Object) Compile(ctx *common.CompileContext) error {
}

func (o Object) build(ctx *common.CompileContext, flags map[common.SchemaTag]string, objectKey string) (common.GolangType, error) {
_, isComponent := flags[common.SchemaTagComponent]
ignore := o.XIgnore || isComponent //&& !ctx.CompileOpts.ModelOpts.IsAllowedName(objectKey))
//_, isComponent := flags[common.SchemaTagComponent]
ignore := o.XIgnore //|| (isComponent && !ctx.CompileOpts.ModelOpts.IsAllowedName(objectKey))
if ignore {
ctx.Logger.Debug("Object denoted to be ignored")
return &lang.GoSimple{Name: "any", IsInterface: true}, nil
Expand Down Expand Up @@ -252,8 +252,8 @@ func (o Object) buildLangStruct(ctx *common.CompileContext, flags map[common.Sch
var contentTypesFunc func() []string
_, isMarshal := flags[common.SchemaTagMarshal]
if isMarshal {
messagesPrm := lang.NewListCbPromise[*render.Message](func(item common.Renderable, _ []string) bool {
_, ok := item.(*render.Message)
messagesPrm := lang.NewListCbPromise[*render.Message](func(item common.CompileObject, _ []string) bool {
_, ok := item.Renderable.(*render.Message)
return ok
})
ctx.PutListPromise(messagesPrm)
Expand Down Expand Up @@ -419,8 +419,8 @@ func (o Object) buildUnionStruct(ctx *common.CompileContext, flags map[common.Sc
}

// Collect all messages to retrieve struct field tags
messagesPrm := lang.NewListCbPromise[*render.Message](func(item common.Renderable, _ []string) bool {
_, ok := item.(*render.Message)
messagesPrm := lang.NewListCbPromise[*render.Message](func(item common.CompileObject, _ []string) bool {
_, ok := item.Renderable.(*render.Message)
return ok
})
ctx.PutListPromise(messagesPrm)
Expand Down
2 changes: 1 addition & 1 deletion internal/asyncapi/protobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func BuildProtoServerStruct(
HasDefinition: true,
},
}
// TODO: handle when protoName is empty
// TODO: handle when protoName is empty (it appears when we build ProtoServer for unsupported protocol)
// Producer/consumer
ctx.Logger.Trace("Server producer", "proto", protoName)
fld := lang.GoStructField{
Expand Down
13 changes: 8 additions & 5 deletions internal/asyncapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func (s Server) Compile(ctx *common.CompileContext) error {
return err
}
ctx.PutObject(obj)

if v, ok := obj.(*render.ProtoServer); ok {
ctx.PutObject(v.ProtoServer)
}
return nil
}

Expand Down Expand Up @@ -64,8 +66,8 @@ func (s Server) build(ctx *common.CompileContext, serverKey string) (common.Rend
}

// Channels which are connected to this server
prm := lang.NewListCbPromise[*render.Channel](func(item common.Renderable, path []string) bool {
_, ok := item.(*render.Channel)
prm := lang.NewListCbPromise[*render.Channel](func(item common.CompileObject, path []string) bool {
_, ok := item.Renderable.(*render.Channel)
if !ok {
return false
}
Expand Down Expand Up @@ -105,7 +107,8 @@ func (s Server) build(ctx *common.CompileContext, serverKey string) (common.Rend
if err != nil {
return nil, err
}
return &render.ProtoServer{Server: &res, Type: protoStruct}, nil
res.ProtoServer = &render.ProtoServer{Server: &res, Type: protoStruct}
return &res, nil
}

ctx.Logger.Trace("Server", "proto", protoBuilder.ProtocolName())
Expand All @@ -121,5 +124,5 @@ func (s Server) build(ctx *common.CompileContext, serverKey string) (common.Rend
ctx.Storage.RegisterProtocol(s.Protocol)
}

return res, nil
return &res, nil
}
8 changes: 4 additions & 4 deletions internal/common/compile_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import (

const nameWordSep = "_"

// ErrObjectDefinitionUnknownYet is returned when some template tries to get a package in the generated code for an
// object, but the definition of this object has not been rendered, therefore the package is unknown yet. When this
// error is returned, a template caused this error goes to the end of the rendering queue.
var ErrObjectDefinitionUnknownYet = errors.New("object definition is unknown yet")
// ErrDefinitionNotRendered is returned when we try to get a package in the generated code for an object, but the
// definition of this object has not been rendered, therefore its location is unknown yet.
var ErrDefinitionNotRendered = errors.New("definition is not rendered")

type CompileObject struct {
Renderable
Expand All @@ -36,6 +35,7 @@ type GolangType interface {
U() string
IsPointer() bool
DefinitionInfo() (*GolangTypeDefinitionInfo, error)
SetDefinitionInfo(info *GolangTypeDefinitionInfo)
}

type CompilationStorage interface {
Expand Down
3 changes: 2 additions & 1 deletion internal/common/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ type ObjectPromise interface {
Assigned() bool
Ref() string
Origin() PromiseOrigin
FindCallback() func(item CompileObject, path []string) bool
}

type ObjectListPromise interface {
AssignList(objs []any)
Assigned() bool
FindCallback() func(item Renderable, path []string) bool
FindCallback() func(item CompileObject, path []string) bool
Ref() string
}
3 changes: 3 additions & 0 deletions internal/common/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ const (
ObjectKindOther ObjectKind = ""// Utility language object, not intended for selection (type, value, interface, etc.)
ObjectKindSchema = "schema"
ObjectKindServer = "server"
ObjectKindProtoServer = "protoServer"
ObjectKindServerVariable = "serverVariable"
ObjectKindChannel = "channel"
ObjectKindProtoChannel = "protoChannel"
ObjectKindMessage = "message"
ObjectKindProtoMessage = "protoMessage"
ObjectKindParameter = "parameter"
ObjectKindCorrelationID = "correlationID"
ObjectKindAsyncAPI = "asyncapi" // Utility object represents the entire AsyncAPI document
Expand Down
7 changes: 5 additions & 2 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ func resolvePromise(p common.ObjectPromise, srcSpecID string, sources map[string
}

srcObjects := sources[tgtSpecID].AllObjects()
cb := func(_ common.Renderable, path []string) bool { return ref.MatchPointer(path) }
cb := func(_ common.CompileObject, path []string) bool { return ref.MatchPointer(path) }
if qcb := p.FindCallback(); qcb != nil {
cb = qcb
}
found := lo.Filter(srcObjects, func(obj common.CompileObject, _ int) bool { return cb(obj, obj.ObjectURL.Pointer) })
if len(found) != 1 {
panic(fmt.Sprintf("Ref %q must point to one object, but %d objects found", p.Ref(), len(found)))
Expand All @@ -154,7 +157,7 @@ func resolvePromise(p common.ObjectPromise, srcSpecID string, sources map[string
// TODO: detect ref loops to avoid infinite recursion
func resolveListPromise(p common.ObjectListPromise, srcSpecID string, sources map[string]ObjectSource) ([]common.Renderable, bool) {
// Exclude links from selection in order to avoid duplicates in list
cb := func(obj common.Renderable, _ []string) bool { return !isPromise(obj) }
cb := func(obj common.CompileObject, _ []string) bool { return !isPromise(obj) }
if qcb := p.FindCallback(); qcb != nil {
cb = qcb
}
Expand Down
8 changes: 4 additions & 4 deletions internal/render/asyncapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ type AsyncAPI struct {
DefaultContentType string
}

func (a AsyncAPI) Kind() common.ObjectKind {
func (a *AsyncAPI) Kind() common.ObjectKind {
return common.ObjectKindAsyncAPI
}

func (a AsyncAPI) Selectable() bool {
func (a *AsyncAPI) Selectable() bool {
return true
}

func (a AsyncAPI) EffectiveDefaultContentType() string {
func (a *AsyncAPI) EffectiveDefaultContentType() string {
res, _ := lo.Coalesce(a.DefaultContentType, fallbackContentType)
return res
}

func (a AsyncAPI) String() string {
func (a *AsyncAPI) String() string {
return "AsyncAPI"
}

Expand Down
Loading

0 comments on commit 22640f3

Please sign in to comment.