Skip to content

Commit

Permalink
Merge pull request #554 from solo-io/jbohanon/json-snapshots
Browse files Browse the repository at this point in the history
implement json.Marshaler in snapshot templates
  • Loading branch information
jbohanon authored May 28, 2024
2 parents ebf2447 + 5fe70e9 commit d93c329
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
6 changes: 6 additions & 0 deletions changelog/v0.35.1/json-apisnapshot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
changelog:
- type: NEW_FEATURE
issueLink: https://github.com/solo-io/gloo/issues/6494
resolvesIssue: false
description: >-
Added code to template for ApiSnapshot that implements JsonMarshaler
11 changes: 10 additions & 1 deletion pkg/code-generator/codegen/templates/snapshot_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"text/template"
)

const backtick = "`"

var ResourceGroupSnapshotTemplate = template.Must(template.New("resource_group_snapshot").Funcs(Funcs).Parse(
`package {{ .Project.ProjectConfig.Version }}
{{/* creating a variable that lets us understand how many resources are hashable input resources. */}}
import (
"encoding/binary"
"encoding/json"
"fmt"
"hash"
"hash/fnv"
Expand All @@ -24,9 +27,11 @@ import (
"go.uber.org/zap"
)
var _ json.Marshaler = new({{ .GoName }}Snapshot)
type {{ .GoName }}Snapshot struct {
{{- range .Resources}}
{{ upper_camel .PluralName }} {{ .ImportPrefix }}{{ .Name }}List
{{ upper_camel .PluralName }} {{ .ImportPrefix }}{{ .Name }}List ` + backtick + `json:"{{ lower_camel .PluralName }}"` + backtick + `
{{- end}}
}
Expand Down Expand Up @@ -83,6 +88,10 @@ func (s {{ .GoName }}Snapshot) HashFields() []zap.Field {
return append(fields, zap.Uint64("snapshotHash", snapshotHash))
}
func (s {{ .GoName }}Snapshot) MarshalJSON() ([]byte, error) {
return json.Marshal(&s)
}
func (s *{{ .GoName }}Snapshot) GetResourcesList(resource resources.Resource) (resources.ResourceList, error) {
switch resource.(type) {
{{- range .Resources }}
Expand Down
23 changes: 15 additions & 8 deletions test/mocks/v1/testing_snapshot.sk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion test/mocks/v1alpha1/testing_snapshot.sk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions test/mocks/v2alpha1/testing_snapshot.sk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d93c329

Please sign in to comment.