Skip to content

Commit

Permalink
Intermediate commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bdragon300 committed Dec 22, 2024
1 parent dab333c commit b696a02
Show file tree
Hide file tree
Showing 85 changed files with 478 additions and 484 deletions.
2 changes: 1 addition & 1 deletion docs/go-asyncapi/content/code-structure/channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (m MyChannelKafka) Publish(ctx context.Context, envelopes ...kafka.Envelope
func (m MyChannelKafka) SealEnvelope(envelope kafka.EnvelopeWriter, message *MyChannelMessageOut) error {
envelope.ResetPayload()

if err := message.MarshalKafkaEnvelope(envelope); err != nil {
if err := message.MarshalEnvelopeKafka(envelope); err != nil {
return err
}
envelope.SetTopic(m.topic)
Expand Down
8 changes: 4 additions & 4 deletions docs/go-asyncapi/content/code-structure/message.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type MyMessageOut struct {
Headers map[string]any
}

func (m *MyMessageOut) MarshalKafkaEnvelope(envelope kafka.EnvelopeWriter) error {
func (m *MyMessageOut) MarshalEnvelopeKafka(envelope kafka.EnvelopeWriter) error {
enc := encoding.NewEncoder("application/json", envelope)

if err := enc.Encode(m.Payload); err != nil {
Expand Down Expand Up @@ -100,7 +100,7 @@ type MyMessageIn struct {
Headers map[string]any
}

func (m *MyMessageIn) UnmarshalKafkaEnvelope(envelope kafka.EnvelopeReader) error {
func (m *MyMessageIn) UnmarshalEnvelopeKafka(envelope kafka.EnvelopeReader) error {
dec := encoding.NewDecoder("application/json", envelope)

if err := dec.Decode(&m.Payload); err != nil {
Expand Down Expand Up @@ -317,7 +317,7 @@ func NewDecoder(contentType string, r io.Reader) Decoder {
{{< tab "Generated message code" >}}
```go
// ...
func (m *MyMessageOut) MarshalKafkaEnvelope(envelope kafka.EnvelopeWriter) error {
func (m *MyMessageOut) MarshalEnvelopeKafka(envelope kafka.EnvelopeWriter) error {
enc := encoding.NewEncoder("application/json", envelope)

if err := enc.Encode(m.Payload); err != nil {
Expand All @@ -330,7 +330,7 @@ func (m *MyMessageOut) MarshalKafkaEnvelope(envelope kafka.EnvelopeWriter) error

// ...

func (m *MyMessageIn) UnmarshalKafkaEnvelope(envelope kafka.EnvelopeReader) error {
func (m *MyMessageIn) UnmarshalEnvelopeKafka(envelope kafka.EnvelopeReader) error {
dec := encoding.NewDecoder("application/json", envelope)

if err := dec.Decode(&m.Payload); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/asyncapi/amqp/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func (pb ProtoBuilder) BuildChannel(ctx *common.CompileContext, channel *asyncap
)

return &render.ProtoChannel{
Channel: parent,
Type: chanStruct,
ProtoName: pb.ProtoName,
Channel: parent,
Type: chanStruct,
Protocol: pb.ProtoName,
}, nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/asyncapi/amqp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func (pb ProtoBuilder) BuildServer(ctx *common.CompileContext, server *asyncapi.
return &render.ProtoServer{
Server: parent,
Type: baseServer,
ProtoName: pb.ProtoName,
}, nil
}

Expand Down
8 changes: 0 additions & 8 deletions internal/asyncapi/asyncapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package asyncapi
import (
"github.com/bdragon300/go-asyncapi/internal/common"
"github.com/bdragon300/go-asyncapi/internal/render"
"github.com/bdragon300/go-asyncapi/internal/render/lang"
"github.com/bdragon300/go-asyncapi/internal/types"
)

Expand All @@ -28,14 +27,7 @@ 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.CompileObject, _ []string) bool {
_, ok := item.Renderable.(*render.Message)
return ok
})
ctx.PutListPromise(allMessagesPrm)

res := &render.AsyncAPI{
AllMessages: allMessagesPrm,
DefaultContentType: a.DefaultContentType,
}
return res
Expand Down
2 changes: 1 addition & 1 deletion internal/asyncapi/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (b *Bindings) build(
) (common.Renderable, error) {
if b.Ref != "" {
ctx.Logger.Trace("Ref", "$ref", b.Ref)
res := lang.NewUserPromise(b.Ref, bindingsKey, nil)
res := lang.NewRef(b.Ref, bindingsKey, nil)
ctx.PutPromise(res)
return res, nil
}
Expand Down
16 changes: 8 additions & 8 deletions internal/asyncapi/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
if c.Ref != "" {
ctx.Logger.Trace("Ref", "$ref", c.Ref)
// Make a promise selectable if it defined in `channels` section
prm := lang.NewUserPromise(c.Ref, channelKey, lo.Ternary(isComponent, nil, lo.ToPtr(true)))
prm := lang.NewRef(c.Ref, channelKey, lo.Ternary(isComponent, nil, lo.ToPtr(true)))
ctx.PutPromise(prm)
return prm, nil
}
Expand All @@ -77,7 +77,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.NewInternalGolangTypeAssignCbPromise(ref, func(obj any) common.GolangType {
prm := lang.NewGolangTypeAssignCbPromise(ref, nil, func(obj any) common.GolangType {
return obj.(*render.Parameter).Type
})
ctx.PutPromise(prm)
Expand All @@ -93,7 +93,7 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
// Empty servers field means "no servers", omitted servers field means "all servers"
if c.Servers != nil {
ctx.Logger.Trace("Channel servers", "names", *c.Servers)
res.SpecServerNames = *c.Servers
res.BoundServerNames = *c.Servers
prm := lang.NewListCbPromise[*render.Server](func(item common.CompileObject, path []string) bool {
srv, ok := item.Renderable.(*render.Server)
if !ok {
Expand All @@ -120,7 +120,7 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
hasBindings = true

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

Expand All @@ -133,13 +133,13 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
hasBindings = true

ref := ctx.PathStackRef("publish", "bindings")
res.BindingsPublishPromise = lang.NewInternalPromise[*render.Bindings](ref)
res.BindingsPublishPromise = lang.NewPromise[*render.Bindings](ref)
ctx.PutPromise(res.BindingsPublishPromise)
}
if c.Publish.Message != nil {
ctx.Logger.Trace("Found publish operation message")
ref := ctx.PathStackRef("publish", "message")
res.PublisherMessageTypePromise = lang.NewInternalPromise[*render.Message](ref)
res.PublisherMessageTypePromise = lang.NewPromise[*render.Message](ref)
ctx.PutPromise(res.PublisherMessageTypePromise)
}
}
Expand All @@ -150,13 +150,13 @@ func (c Channel) build(ctx *common.CompileContext, channelKey string, flags map[
hasBindings = true

ref := ctx.PathStackRef("subscribe", "bindings")
res.BindingsSubscribePromise = lang.NewInternalPromise[*render.Bindings](ref)
res.BindingsSubscribePromise = lang.NewPromise[*render.Bindings](ref)
ctx.PutPromise(res.BindingsSubscribePromise)
}
if c.Subscribe.Message != nil {
ctx.Logger.Trace("Channel subscribe operation message")
ref := ctx.PathStackRef("subscribe", "message")
res.SubscriberMessageTypePromise = lang.NewInternalPromise[*render.Message](ref)
res.SubscriberMessageTypePromise = lang.NewPromise[*render.Message](ref)
ctx.PutPromise(res.SubscriberMessageTypePromise)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/asyncapi/correlationid.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c CorrelationID) build(ctx *common.CompileContext, correlationIDKey string
// TODO: move this ref code from everywhere to single place?
if c.Ref != "" {
ctx.Logger.Trace("Ref", "$ref", c.Ref)
res := lang.NewUserPromise(c.Ref, correlationIDKey, nil)
res := lang.NewRef(c.Ref, correlationIDKey, nil)
ctx.PutPromise(res)
return res, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/asyncapi/http/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func (pb ProtoBuilder) BuildChannel(ctx *common.CompileContext, channel *asyncap
}

return &render.ProtoChannel{
Channel: parent,
Type: chanStruct,
ProtoName: pb.ProtoName,
Channel: parent,
Type: chanStruct,
Protocol: pb.ProtoName,
}, nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/asyncapi/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func (pb ProtoBuilder) BuildServer(ctx *common.CompileContext, server *asyncapi.
return &render.ProtoServer{
Server: parent,
Type: baseServer,
ProtoName: pb.ProtoName,
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/asyncapi/ip/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (pb ProtoBuilder) BuildChannel(ctx *common.CompileContext, channel *asyncap
}

return &render.ProtoChannel{
Channel: parent,
Type: chanStruct,
ProtoName: pb.ProtoName,
Channel: parent,
Type: chanStruct,
Protocol: pb.ProtoName,
}, nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/asyncapi/ip/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func (pb ProtoBuilder) BuildServer(ctx *common.CompileContext, server *asyncapi.
return &render.ProtoServer{
Server: parent,
Type: baseServer,
ProtoName: pb.ProtoName,
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/asyncapi/kafka/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func (pb ProtoBuilder) BuildChannel(ctx *common.CompileContext, channel *asyncap
chanStruct.Fields = append(chanStruct.Fields, lang.GoStructField{Name: "topic", Type: &lang.GoSimple{TypeName: "string"}})

return &render.ProtoChannel{
Channel: parent,
Type: chanStruct,
ProtoName: pb.ProtoName,
Channel: parent,
Type: chanStruct,
Protocol: pb.ProtoName,
}, nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/asyncapi/kafka/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func (pb ProtoBuilder) BuildServer(ctx *common.CompileContext, server *asyncapi.
return &render.ProtoServer{
Server: parent,
Type: baseServer,
ProtoName: pb.ProtoName,
}, nil
}

Expand Down
20 changes: 10 additions & 10 deletions internal/asyncapi/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re

ctx.Logger.Trace("Ref", "$ref", m.Ref)
// Always draw the promises that are located in the `messages` section
prm := lang.NewUserPromise(m.Ref, refName, lo.Ternary(makeSelectable, lo.ToPtr(true), nil))
prm := lang.NewRef(m.Ref, refName, lo.Ternary(makeSelectable, lo.ToPtr(true), nil))
ctx.PutPromise(prm)
return prm, nil
}
Expand Down Expand Up @@ -118,18 +118,18 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
res.AllServersPromise = prm
ctx.PutListPromise(prm)

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

// Link to Headers struct if any
if m.Headers != nil {
ctx.Logger.Trace("Message headers")
ref := ctx.PathStackRef("headers")
res.HeadersTypePromise = lang.NewInternalPromise[*lang.GoStruct](ref)
res.HeadersTypePromise = lang.NewPromise[*lang.GoStruct](ref)
res.HeadersTypePromise.AssignErrorNote = "Probably the headers schema has type other than of 'object'?"
ctx.PutPromise(res.HeadersTypePromise)
}
Expand All @@ -146,15 +146,15 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
}

ref := ctx.PathStackRef("bindings")
res.BindingsPromise = lang.NewInternalPromise[*render.Bindings](ref)
res.BindingsPromise = lang.NewPromise[*render.Bindings](ref)
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.NewInternalPromise[*render.CorrelationID](ref)
res.CorrelationIDPromise = lang.NewPromise[*render.CorrelationID](ref)
ctx.PutPromise(res.CorrelationIDPromise)
}

Expand All @@ -165,8 +165,8 @@ func (m Message) build(ctx *common.CompileContext, messageKey string) (common.Re
for proto := range ProtocolBuilders {
ctx.Logger.Trace("Message", "proto", proto)
protoMessages = append(protoMessages, &render.ProtoMessage{
Message: &res,
ProtoName: proto,
Message: &res,
Protocol: proto,
})
}
res.ProtoMessages = protoMessages
Expand All @@ -180,7 +180,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.NewInternalGolangTypePromise(langMessage.HeadersTypePromise.Ref())
prm := lang.NewGolangTypePromise(langMessage.HeadersTypePromise.Ref())
ctx.PutPromise(prm)
fields = append(fields, lang.GoStructField{Name: "Headers", Type: prm})
} else {
Expand All @@ -196,7 +196,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.NewInternalGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref)
ctx.PutPromise(prm)
return prm
}
Expand Down
6 changes: 3 additions & 3 deletions internal/asyncapi/mqtt/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func (pb ProtoBuilder) BuildChannel(ctx *common.CompileContext, channel *asyncap
chanStruct.Fields = append(chanStruct.Fields, lang.GoStructField{Name: "topic", Type: &lang.GoSimple{TypeName: "string"}})

return &render.ProtoChannel{
Channel: parent,
Type: chanStruct,
ProtoName: pb.ProtoName,
Channel: parent,
Type: chanStruct,
Protocol: pb.ProtoName,
}, nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/asyncapi/mqtt/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func (pb ProtoBuilder) BuildServer(ctx *common.CompileContext, server *asyncapi.
return &render.ProtoServer{
Server: parent,
Type: baseServer,
ProtoName: pb.ProtoName,
}, nil
}

Expand Down
14 changes: 7 additions & 7 deletions internal/asyncapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (o Object) build(ctx *common.CompileContext, flags map[common.SchemaTag]str
}

ctx.Logger.Trace("Ref", "$ref", o.Ref)
res := lang.NewUserPromise(o.Ref, refName, lo.ToPtr(false))
res := lang.NewRef(o.Ref, refName, lo.ToPtr(false))
ctx.PutPromise(res)
return res, nil
}
Expand Down Expand Up @@ -287,7 +287,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.NewInternalGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref)
ctx.PutPromise(prm)

var langObj common.GolangType = prm
Expand Down Expand Up @@ -318,7 +318,7 @@ func (o Object) buildLangStruct(ctx *common.CompileContext, flags map[common.Sch
case 0: // "additionalProperties:" is an object
ctx.Logger.Trace("Object additional properties as an object")
ref := ctx.PathStackRef("additionalProperties")
prm := lang.NewInternalGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref)
ctx.PutPromise(prm)
xTags, xTagNames, xTagVals := o.AdditionalProperties.V0.xGoTagsInfo(ctx)
f := lang.GoStructField{
Expand Down Expand Up @@ -388,7 +388,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 (single type)")
ref := ctx.PathStackRef("items")
prm := lang.NewInternalGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref)
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 @@ -439,19 +439,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.NewInternalGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref)
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.NewInternalGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref)
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.NewInternalGolangTypePromise(ref)
prm := lang.NewGolangTypePromise(ref)
ctx.PutPromise(prm)
return lang.GoStructField{Type: prm}
})...)
Expand Down
Loading

0 comments on commit b696a02

Please sign in to comment.