Skip to content

Commit

Permalink
Intermediate commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bdragon300 committed Jan 5, 2025
1 parent dc6c5e9 commit ed71665
Show file tree
Hide file tree
Showing 36 changed files with 351 additions and 231 deletions.
6 changes: 3 additions & 3 deletions internal/asyncapi/asyncapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type AsyncAPI struct {
Asyncapi string `json:"asyncapi" yaml:"asyncapi"`
ID string `json:"id" yaml:"id"`
Info InfoItem `json:"info" yaml:"info"`
Servers types.OrderedMap[string, Server] `json:"servers" yaml:"servers"`
Servers types.OrderedMap[string, Server] `json:"servers" yaml:"servers" cgen:"selectable"`
DefaultContentType string `json:"defaultContentType" yaml:"defaultContentType"`
Channels types.OrderedMap[string, Channel] `json:"channels" yaml:"channels"`
Operations types.OrderedMap[string, Operation] `json:"operations" yaml:"operations"`
Channels types.OrderedMap[string, Channel] `json:"channels" yaml:"channels" cgen:"selectable"`
Operations types.OrderedMap[string, Operation] `json:"operations" yaml:"operations" cgen:"selectable"`
Components ComponentsItem `json:"components" yaml:"components"`
}

Expand Down
28 changes: 14 additions & 14 deletions internal/asyncapi/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
return &render.Channel{Dummy: true}, nil
}

_, isComponent := flags[common.SchemaTagComponent]
_, isSelectable := flags[common.SchemaTagSelectable]
if c.Ref != "" {
// Make a promise selectable if it defined in `channels` section
return registerRef(ctx, c.Ref, channelKey, lo.Ternary(isComponent, nil, lo.ToPtr(true))), nil
return registerRef(ctx, c.Ref, channelKey, lo.Ternary(isSelectable, lo.ToPtr(true), nil)), nil
}

chName, _ := lo.Coalesce(c.XGoName, channelKey)
res := &render.Channel{
OriginalName: chName,
Address: c.Address,
IsComponent: isComponent,
IsPublisher: ctx.CompileOpts.GeneratePublishers,
Address: c.Address,
IsSelectable: isSelectable,
IsPublisher: ctx.CompileOpts.GeneratePublishers,
IsSubscriber: ctx.CompileOpts.GenerateSubscribers,
}

// Servers which this channel is bound with
if len(c.Servers) > 0 {
ctx.Logger.Trace("Channel servers", "refs", c.Servers)
for _, srvRef := range c.Servers {
prm := lang.NewPromise[*render.Server](srvRef.Ref)
prm := lang.NewPromise[*render.Server](srvRef.Ref, nil)
res.ServersPromises = append(res.ServersPromises, prm)
ctx.PutPromise(prm)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
for _, paramName := range c.Parameters.Keys() {
ctx.Logger.Trace("Channel parameter", "name", paramName)
ref := ctx.PathStackRef("parameters", paramName)
prm := lang.NewGolangTypeAssignCbPromise(ref, nil, func(obj any) common.GolangType {
prm := lang.NewGolangTypePromise(ref, func(obj any) common.GolangType {
return obj.(*render.Parameter).Type
})
ctx.PutPromise(prm)
Expand All @@ -106,12 +106,12 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
ctx.Logger.PrevCallLevel()
}

for _, msgEntry := range c.Messages.Entries() {
msgName, ref := msgEntry.Key, msgEntry.Value
for _, msgName := range c.Messages.Keys() {
ctx.Logger.Trace("Channel message", "name", msgName)
refObj := lang.NewRef(ref.Ref, msgName, lo.ToPtr(false))
ctx.PutPromise(refObj)
res.MessagesPromises = append(res.MessagesPromises, refObj)
ref := ctx.PathStackRef("messages", msgName)
prm2 := lang.NewRef(ref, msgName, lo.ToPtr(true))
ctx.PutPromise(prm2)
res.MessagesRefs = append(res.MessagesRefs, prm2)
}

// All known Operations
Expand All @@ -129,7 +129,7 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
ctx.Logger.Trace("Found channel bindings")

ref := ctx.PathStackRef("bindings")
res.BindingsPromise = lang.NewPromise[*render.Bindings](ref)
res.BindingsPromise = lang.NewPromise[*render.Bindings](ref, nil)
ctx.PutPromise(res.BindingsPromise)

res.BindingsType = &lang.GoStruct{
Expand Down Expand Up @@ -161,5 +161,5 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[


type SecurityRequirement struct {
types.OrderedMap[string, []string] // FIXME: orderedmap must be in fields as utils.OrderedMap[SecurityRequirement, []SecurityRequirement]
types.OrderedMap[string, types.Union2[[]string, string]] // Possible values: `"name": []` or `"$ref": "url"`
}
2 changes: 1 addition & 1 deletion internal/asyncapi/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type ComponentsItem struct {
Schemas types.OrderedMap[string, Object] `json:"schemas" yaml:"schemas" cgen:"components,marshal,definition"`
Schemas types.OrderedMap[string, Object] `json:"schemas" yaml:"schemas" cgen:"components,marshal,definition,selectable"`

Servers types.OrderedMap[string, Server] `json:"servers" yaml:"servers" cgen:"components"`
Channels types.OrderedMap[string, Channel] `json:"channels" yaml:"channels" cgen:"components"`
Expand Down
22 changes: 9 additions & 13 deletions internal/asyncapi/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func (m Message) Compile(ctx *common.CompileContext) error {
}

func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Renderable, error) {
_, isComponent := ctx.Stack.Top().Flags[common.SchemaTagComponent]
if m.XIgnore {
ctx.Logger.Debug("Message denoted to be ignored")
return &render.Message{Dummy: true}, nil
Expand All @@ -58,19 +57,16 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re

// Message is the only type of objects, that has their own root key, the key in components and can be used
// as ref in other objects at the same time (at channel.[publish|subscribe].message).
// Therefore, a message object may get to selections more than once, it's needed to handle in templates.
// Therefore, a message object may get to selections more than once, it's needed to handle it in templates.
refName := messageKey
pathStack := ctx.Stack.Items()
makeSelectable := !isComponent
// Ignore the messageKey in definitions other than `messages`, since messageKey always be "message" there.
if messageKey == "message" && len(pathStack) > 3 {
refName = ""
// And force make the message selectable if it was defined in `components.messages` section.
makeSelectable = true
}

// Always draw the promises that are located in the `messages` section
return registerRef(ctx, m.Ref, refName, lo.Ternary(makeSelectable, lo.ToPtr(true), nil)), nil
return registerRef(ctx, m.Ref, refName, nil), nil
}

msgName, _ := lo.Coalesce(m.XGoName, messageKey)
Expand All @@ -92,8 +88,8 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
},
PayloadType: m.getPayloadType(ctx),
HeadersFallbackType: &lang.GoMap{KeyType: &lang.GoSimple{TypeName: "string"}, ValueType: &lang.GoSimple{TypeName: "any", IsInterface: true}},
ContentType: m.ContentType,
IsComponent: isComponent,
ContentType: m.ContentType,
IsSelectable: true,
}
ctx.Logger.Trace(fmt.Sprintf("Message content type is %q", res.ContentType))

Expand Down Expand Up @@ -127,7 +123,7 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
if m.Headers != nil {
ctx.Logger.Trace("Message headers")
ref := ctx.PathStackRef("headers")
res.HeadersTypePromise = lang.NewPromise[*lang.GoStruct](ref)
res.HeadersTypePromise = lang.NewPromise[*lang.GoStruct](ref, nil)
res.HeadersTypePromise.AssignErrorNote = "Probably the headers schema has type other than of 'object'?"
ctx.PutPromise(res.HeadersTypePromise)
}
Expand All @@ -144,15 +140,15 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
}

ref := ctx.PathStackRef("bindings")
res.BindingsPromise = lang.NewPromise[*render.Bindings](ref)
res.BindingsPromise = lang.NewPromise[*render.Bindings](ref,nil)
ctx.PutPromise(res.BindingsPromise)
}

// Link to CorrelationID if any
if m.CorrelationID != nil {
ctx.Logger.Trace("Message correlationId")
ref := ctx.PathStackRef("correlationId")
res.CorrelationIDPromise = lang.NewPromise[*render.CorrelationID](ref)
res.CorrelationIDPromise = lang.NewPromise[*render.CorrelationID](ref,nil)
ctx.PutPromise(res.CorrelationIDPromise)
}

Expand All @@ -178,7 +174,7 @@ func (m Message) setStructFields(ctx *common.CompileContext, langMessage *render
}
if langMessage.HeadersTypePromise != nil {
ctx.Logger.Trace("Message headers has a concrete type")
prm := lang.NewGolangTypePromise(langMessage.HeadersTypePromise.Ref())
prm := lang.NewGolangTypePromise(langMessage.HeadersTypePromise.Ref(), nil)
ctx.PutPromise(prm)
fields = append(fields, lang.GoStructField{Name: string(render.CorrelationIDStructFieldHeaders), Type: prm})
} else {
Expand All @@ -194,7 +190,7 @@ func (m Message) getPayloadType(ctx *common.CompileContext) common.GolangType {
if m.Payload != nil {
ctx.Logger.Trace("Message payload has a concrete type")
ref := ctx.PathStackRef("payload")
prm := lang.NewGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref, nil)
ctx.PutPromise(prm)
return prm
}
Expand Down
18 changes: 9 additions & 9 deletions internal/asyncapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (o Object) Compile(ctx *common.CompileContext) error {
}

func (o Object) build(ctx *common.CompileContext, flags map[common.SchemaTag]string, objectKey string) (common.Renderable, error) {
_, isComponent := flags[common.SchemaTagComponent]
_, isSelectable := flags[common.SchemaTagSelectable]
ignore := o.XIgnore
if ignore {
ctx.Logger.Debug("Object denoted to be ignored")
Expand All @@ -97,11 +97,11 @@ func (o Object) build(ctx *common.CompileContext, flags map[common.SchemaTag]str

refName := objectKey
// Ignore the objectKey in definitions other than `components.schemas`, generate a unique name instead
if !isComponent {
if !isSelectable {
refName = ctx.GenerateObjName("", "")
}

return registerRef(ctx, o.Ref, refName, lo.ToPtr(false)), nil
return registerRef(ctx, o.Ref, refName, &isSelectable), nil
}

if o.Type == nil {
Expand Down Expand Up @@ -290,7 +290,7 @@ func (o Object) buildLangStruct(ctx *common.CompileContext, flags map[common.Sch
for _, entry := range o.Properties.Entries() {
ctx.Logger.Trace("Object property", "name", entry.Key)
ref := ctx.PathStackRef("properties", entry.Key)
prm := lang.NewGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref, nil)
ctx.PutPromise(prm)

var langObj common.GolangType = prm
Expand Down Expand Up @@ -321,7 +321,7 @@ func (o Object) buildLangStruct(ctx *common.CompileContext, flags map[common.Sch
case 0: // "additionalProperties:" is an object
ctx.Logger.Trace("Object additional properties", "type", "object")
ref := ctx.PathStackRef("additionalProperties")
prm := lang.NewGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref, nil)
ctx.PutPromise(prm)
xTags, xTagNames, xTagVals := o.AdditionalProperties.V0.xGoTagsInfo(ctx)
f := lang.GoStructField{
Expand Down Expand Up @@ -391,7 +391,7 @@ func (o Object) buildLangArray(ctx *common.CompileContext, flags map[common.Sche
case o.Items != nil && o.Items.Selector == 0: // Only one "type:" of items
ctx.Logger.Trace("Object items", "typesCount", "single")
ref := ctx.PathStackRef("items")
prm := lang.NewGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref, nil)
ctx.PutPromise(prm)
res.ItemsType = prm
case o.Items == nil || o.Items.Selector == 1: // No items or Several types for each item sequentially
Expand Down Expand Up @@ -442,19 +442,19 @@ func (o Object) buildUnionStruct(ctx *common.CompileContext, flags map[common.Sc

res.Fields = lo.Times(len(o.OneOf), func(index int) lang.GoStructField {
ref := ctx.PathStackRef("oneOf", strconv.Itoa(index))
prm := lang.NewGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref, nil)
ctx.PutPromise(prm)
return lang.GoStructField{Type: &lang.GoPointer{Type: prm}}
})
res.Fields = append(res.Fields, lo.Times(len(o.AnyOf), func(index int) lang.GoStructField {
ref := ctx.PathStackRef("anyOf", strconv.Itoa(index))
prm := lang.NewGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref, nil)
ctx.PutPromise(prm)
return lang.GoStructField{Type: &lang.GoPointer{Type: prm}}
})...)
res.Fields = append(res.Fields, lo.Times(len(o.AllOf), func(index int) lang.GoStructField {
ref := ctx.PathStackRef("allOf", strconv.Itoa(index))
prm := lang.NewGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref, nil)
ctx.PutPromise(prm)
return lang.GoStructField{Type: prm}
})...)
Expand Down
33 changes: 23 additions & 10 deletions internal/asyncapi/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ func (o Operation) build(ctx *common.CompileContext, operationKey string, flags
return &render.Operation{Dummy: true}, nil
}

_, isComponent := flags[common.SchemaTagComponent]
_, isSelectable := flags[common.SchemaTagSelectable]
if o.Ref != "" {
// Make a promise selectable if it defined in `operations` section
return registerRef(ctx, o.Ref, operationKey, lo.Ternary(isComponent, nil, lo.ToPtr(true))), nil
return registerRef(ctx, o.Ref, operationKey, lo.Ternary(isSelectable, lo.ToPtr(true), nil)), nil
}

res := &render.Operation{
OriginalName: operationKey,
IsComponent: isComponent,
IsPublisher: o.Action == OperationActionSend,
IsSelectable: isSelectable,
IsPublisher: o.Action == OperationActionSend,
IsSubscriber: o.Action == OperationActionReceive,
}

Expand All @@ -73,15 +73,15 @@ func (o Operation) build(ctx *common.CompileContext, operationKey string, flags
}

ctx.Logger.Trace("Bound channel", "ref", o.Channel.Ref)
prm := lang.NewPromise[*render.Channel](o.Channel.Ref)
prm := lang.NewPromise[*render.Channel](o.Channel.Ref,nil)
ctx.PutPromise(prm)
res.ChannelPromise = prm

if o.Bindings != nil {
ctx.Logger.Trace("Found operation bindings")

ref := ctx.PathStackRef("bindings")
res.BindingsPromise = lang.NewPromise[*render.Bindings](ref)
res.BindingsPromise = lang.NewPromise[*render.Bindings](ref, nil)
ctx.PutPromise(res.BindingsPromise)

res.BindingsType = &lang.GoStruct{
Expand All @@ -95,7 +95,7 @@ func (o Operation) build(ctx *common.CompileContext, operationKey string, flags
for _, message := range o.Messages {
ctx.Logger.Trace("Operation message", "ref", message.Ref)

prm := lang.NewPromise[*render.Message](message.Ref)
prm := lang.NewPromise[*render.Message](message.Ref, nil)
ctx.PutPromise(prm)
res.MessagesPromises = append(res.MessagesPromises, prm)
}
Expand All @@ -107,7 +107,7 @@ func (o Operation) build(ctx *common.CompileContext, operationKey string, flags
ctx.Logger.Trace("Prebuild the operations for every supported protocol")
for proto := range ProtocolBuilders {
ctx.Logger.Trace("Operation", "proto", proto)
prm := lang.NewGolangTypeAssignCbPromise(o.Channel.Ref, nil, func(obj any) common.GolangType {
prmProtoChType := lang.NewGolangTypePromise(o.Channel.Ref, func(obj any) common.GolangType {
ch := obj.(*render.Channel)
protoCh, found := lo.Find(ch.ProtoChannels, func(p *render.ProtoChannel) bool {
return p.Protocol == proto
Expand All @@ -117,6 +117,18 @@ func (o Operation) build(ctx *common.CompileContext, operationKey string, flags
}
return protoCh.Type
})
ctx.PutPromise(prmProtoChType)
prmProtoCh := lang.NewPromise[*render.ProtoChannel](o.Channel.Ref, func(obj any) *render.ProtoChannel {
ch := obj.(*render.Channel)
protoCh, found := lo.Find(ch.ProtoChannels, func(p *render.ProtoChannel) bool {
return p.Protocol == proto
})
if !found {
panic(fmt.Sprintf("ProtoChannel[%s] not found in %s. This is a bug", proto, ch))
}
return protoCh
})
ctx.PutPromise(prmProtoCh)
res.ProtoOperations = append(res.ProtoOperations, &render.ProtoOperation{
Operation: res,
Type: &lang.GoStruct{
Expand All @@ -125,10 +137,11 @@ func (o Operation) build(ctx *common.CompileContext, operationKey string, flags
HasDefinition: true,
},
Fields: []lang.GoStructField{
{Type: prm},
{Name: "Channel", Type: &lang.GoPointer{Type: prmProtoChType}},
},
},
Protocol: proto,
ProtoChannelPromise: prmProtoCh,
Protocol: proto,
})
}

Expand Down
Loading

0 comments on commit ed71665

Please sign in to comment.