Skip to content

Commit

Permalink
fix type resolution; add subscribers to context
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Jun 29, 2019
1 parent 19b14cb commit 1d2a012
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions internal/template/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"strings"

"github.com/lukasjarosch/godin/internal/bundle"
"github.com/lukasjarosch/godin/pkg/amqp"

"github.com/lukasjarosch/godin/internal"
"github.com/lukasjarosch/godin/internal/parse"
config "github.com/spf13/viper"
Expand All @@ -14,18 +17,33 @@ type Context struct {
Service Service
Godin Godin
Protobuf Protobuf
Docker Docker
Docker Docker
}

// NewContextFromConfig will initialize the context will all the data from the configuration
// The context is not fully populated after this call, but all configuration values are accessible.
func NewContextFromConfig() Context {
var subscribers []Subscriber

// amqp subscribers
sub := config.GetStringMap(bundle.SubscriberKey)
if len(sub) > 0 {
for _, subscriber := range sub {
sub := subscriber.(amqp.Subscription)
subscribers = append(subscribers, Subscriber{
Subscription: sub,
Handler: bundle.HandlerName(sub.Topic),
})
}
}

ctx := Context{
Service: Service{
Name: config.GetString("service.name"),
Namespace: config.GetString("service.namespace"),
Module: config.GetString("service.module"),
LoggingMiddleware: config.GetBool("service.middleware.logging"),
Subscriber: subscribers,
},
Protobuf: Protobuf{
Package: config.GetString("protobuf.package"),
Expand All @@ -36,7 +54,7 @@ func NewContextFromConfig() Context {
Build: internal.Build,
Commit: internal.Commit,
},
Docker: Docker {
Docker: Docker{
Registry: config.GetString("docker.registry"),
},
}
Expand Down Expand Up @@ -99,22 +117,28 @@ type Service struct {
Methods []Method
Module string
LoggingMiddleware bool
Subscriber []Subscriber
}

type Protobuf struct {
Package string
Service string
}

type Docker struct {
Registry string
}

type Subscriber struct {
Handler string
Subscription amqp.Subscription
}

type Variable struct {
Name string
Type string
}

type Docker struct {
Registry string
}

func (v Variable) resolveType(typ string, prefix string) (string, error) {
if strings.HasPrefix(typ, prefix) {
trimmed := strings.TrimLeft(v.Type, prefix)
Expand All @@ -132,6 +156,10 @@ func (v Variable) resolveType(typ string, prefix string) (string, error) {
func (v Variable) ResolveType() string {
prefixes := []string{"[]*", "*[]", "[]", "*"}

if types.IsBuiltinTypeString(v.Type) {
return v.Type
}

if strings.Contains(v.Type, ".") {
return v.Type
}
Expand All @@ -152,7 +180,7 @@ func (v Variable) NilValue() string {
case "error":
return "nil"
case "string":
return ""
return "\"\""
case "boolean":
return "false"
case "int",
Expand All @@ -167,7 +195,6 @@ func (v Variable) NilValue() string {
}
}


type Method struct {
// required for partials which do not have access to the Service struct
ServiceName string
Expand Down

0 comments on commit 1d2a012

Please sign in to comment.