forked from cortezaproject/corteza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend envoy to support resource translations
- Loading branch information
Showing
38 changed files
with
1,891 additions
and
57 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
pkg/codegen-v3/assets/templates/gocode/envoy/resource-resource_translation.go.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package {{ .Package }} | ||
|
||
{{ template "header-gentext.tpl" }} | ||
{{ template "header-definitions.tpl" . }} | ||
|
||
import ( | ||
systemTypes "github.com/cortezaproject/corteza-server/system/types" | ||
{{- range .Imports }} | ||
{{ . }} | ||
{{- end }} | ||
) | ||
|
||
{{- range .Def }} | ||
{{ $Component := .Component }} | ||
{{ $Resource := .Resource }} | ||
{{ $GoType := printf "types.%s" .Resource }} | ||
func (r *{{export $Component}}{{$Resource}}) EncodeTranslations() ([]*ResourceTranslation, error) { | ||
out := make([]*ResourceTranslation, 0, 10) | ||
rr := r.Res.EncodeTranslations() | ||
rr.SetLanguage(defaultLanguage) | ||
res, ref, pp := r.ResourceTranslationParts() | ||
out = append(out, NewResourceTranslation(systemTypes.FromLocale(rr), res, ref, pp...)) | ||
{{ if .Locale.Extended }} | ||
tmp, err := r.encodeTranslations() | ||
return append(out, tmp...), err | ||
{{ else }} | ||
return out, nil | ||
{{- end }} | ||
} | ||
{{- end }} |
73 changes: 73 additions & 0 deletions
73
pkg/codegen-v3/assets/templates/gocode/envoy/resource-resource_translation_parse.go.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package {{ .Package }} | ||
|
||
{{ template "header-gentext.tpl" }} | ||
{{ template "header-definitions.tpl" . }} | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
{{- range .Imports }} | ||
{{ . }} | ||
{{- end }} | ||
) | ||
|
||
|
||
// Parse generates resource setting logic for each resource | ||
// | ||
// Resources with "envoy: false" are skipped | ||
// | ||
// This function is auto-generated | ||
func ParseResourceTranslation(res string) (string, *Ref, []*Ref, error) { | ||
if res == "" { | ||
return "", nil, nil, fmt.Errorf("empty resource") | ||
} | ||
|
||
sp := "/" | ||
|
||
if strings.Index(res, "corteza::") == 0 { | ||
res = res[9:] | ||
} | ||
|
||
res = strings.TrimSpace(res) | ||
res = strings.TrimRight(res, sp) | ||
rr := strings.Split(res, sp) | ||
|
||
// only service defined (corteza::system, corteza::compose, ...) | ||
if len(rr) == 1 { | ||
return "", nil, nil, fmt.Errorf("only service defined: %s", res) | ||
} | ||
|
||
// full thing | ||
resourceType, path := rr[0], rr[1:] | ||
for p := 1; p < len(path); p++ { | ||
if path[p] == "*" { | ||
return "", nil, nil, fmt.Errorf("path wildcard not allowed for locale resources: '%s'", res) | ||
} | ||
} | ||
|
||
// make the resource provide the slice of parent resources we should nest under | ||
switch resourceType { | ||
{{- range .Def }} | ||
case {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType: | ||
if len(path) != {{ len .Locale.Resource.References }} { | ||
return "", nil, nil, fmt.Errorf("expecting {{ len .Locale.Resource.References }} reference components in path, got %d", len(path)) | ||
} | ||
{{- if gt (len .Locale.Resource.References) 0 }} | ||
ref, pp, err := {{ export .Component .Resource }}ResourceTranslationReferences( | ||
{{- range $i, $r := .Locale.Resource.References }} | ||
// {{ unexport $r.Resource }} | ||
path[{{ $i }}], | ||
{{ end }} | ||
) | ||
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType, ref, pp, err | ||
{{ else }} | ||
|
||
// Component resource, no path | ||
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType, nil, nil, nil | ||
{{- end }} | ||
{{- end}} | ||
} | ||
|
||
// return unhandled resource as-is | ||
return resourceType, nil, nil, nil | ||
} |
33 changes: 33 additions & 0 deletions
33
pkg/codegen-v3/assets/templates/gocode/envoy/resource-resource_translation_references.go.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package {{ .Package }} | ||
|
||
{{ template "header-gentext.tpl" }} | ||
{{ template "header-definitions.tpl" . }} | ||
|
||
import ( | ||
{{- range .Imports }} | ||
{{ . }} | ||
{{- end }} | ||
) | ||
|
||
{{- range .Def }} | ||
{{- if .Locale }} | ||
{{- if gt (len .Locale.Resource.References) 0 }} | ||
// {{ export .Component .Resource }}ResourceTranslationReferences generates Locale references | ||
// | ||
// Resources with "envoy: false" are skipped | ||
// | ||
// This function is auto-generated | ||
func {{ export .Component .Resource }}ResourceTranslationReferences({{- range .Locale.Resource.References }}{{ unexport .Resource }} string, {{- end }}) (res *Ref, pp []*Ref, err error) { | ||
{{- range .Locale.Resource.References }} | ||
{{- if eq .Field "ID" }} | ||
res = &Ref{ResourceType: types.{{ export .Resource }}ResourceType, Identifiers: MakeIdentifiers({{ unexport .Resource }})} | ||
{{- else }} | ||
pp = append(pp, &Ref{ResourceType: types.{{ export .Resource }}ResourceType, Identifiers: MakeIdentifiers({{ unexport .Resource }})}) | ||
{{- end }} | ||
{{- end }} | ||
|
||
return | ||
} | ||
{{- end }} | ||
{{- end }} | ||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package envoy | ||
|
||
import "github.com/cortezaproject/corteza-server/pkg/envoy/resource" | ||
|
||
// NormalizeResourceTranslations takes the provided resource.ResourceTranslation | ||
// and merges duplicates based on the Priority parameter | ||
func NormalizeResourceTranslations(rr ...resource.Interface) []resource.Interface { | ||
out := make([]resource.Interface, 0, len(rr)) | ||
locales := make([]*resource.ResourceTranslation, 0, len(rr)) | ||
|
||
// - collect all locale resources | ||
for _, r := range rr { | ||
if l, ok := r.(*resource.ResourceTranslation); ok { | ||
locales = append(locales, l) | ||
} else { | ||
out = append(out, r) | ||
} | ||
} | ||
|
||
// make an index | ||
var byResource map[string][2]*resource.ResourceTranslation | ||
byResource = make(map[string][2]*resource.ResourceTranslation) | ||
|
||
for _, locale := range locales { | ||
pp := byResource[locale.RefResource] | ||
pp[locale.Priority] = locale | ||
byResource[locale.RefResource] = pp | ||
} | ||
|
||
// squash index by priority ascending | ||
for _, pp := range byResource { | ||
var aux *resource.ResourceTranslation | ||
seen := make(map[string]bool) | ||
|
||
for _, p := range pp { | ||
if p == nil { | ||
continue | ||
} | ||
|
||
if aux == nil { | ||
aux = p | ||
} else { | ||
for _, r := range p.Res { | ||
if seen[r.Lang.String()+r.K] { | ||
continue | ||
} | ||
|
||
aux.Res = append(aux.Res, r) | ||
} | ||
} | ||
|
||
for _, r := range p.Res { | ||
if r.K != "" { | ||
seen[r.Lang.String()+r.K] = true | ||
} | ||
} | ||
} | ||
|
||
if aux != nil { | ||
out = append(out, aux) | ||
} | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.