Skip to content

Commit

Permalink
Remove sourceContext from the json when compact is set (#938)
Browse files Browse the repository at this point in the history
* Remove sourceContext from the json when compact

* for loop on 1 line

* simplify

* keep sourceContext for Imports
AriehSchneier authored Aug 21, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 428d0fd commit 81b6e1c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/sysl/cmd_protobuf.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"reflect"
"strings"

"github.com/anz-bank/sysl/pkg/cmdutils"
@@ -75,6 +76,10 @@ func (p *protobufCmd) Execute(args cmdutils.ExecuteArgs) error {
}

if toJSON {
if p.compact {
removeSourceContext(m.Apps)
}

if p.splitappspath != "-" {
return pbutil.OutputSplitApplications(m, "json", opt, p.splitappspath, "data.json", args.Filesystem)
} else if p.output == "-" {
@@ -104,3 +109,39 @@ func (p *protobufCmd) Execute(args cmdutils.ExecuteArgs) error {
}
return pbutil.GeneratePBBinaryMessageFile(m, p.output, args.Filesystem)
}

func removeSourceContext(target any) {
removeSourceContextImpl(reflect.ValueOf(target))
}

func removeSourceContextImpl(v reflect.Value) {
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
switch v.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < v.Len(); i++ {
removeSourceContextImpl(v.Index(i))
}
case reflect.Map:
for iterator := v.MapRange(); iterator.Next(); {
removeSourceContextImpl(iterator.Value())
}
case reflect.Struct:
t := v.Type()
for i := 0; i < v.NumField(); i++ {
fType := t.Field(i)
if fType.IsExported() == false || fType.Name == "SourceContexts" {
continue
}

fValue := v.Field(i)
if fType.Name == "SourceContext" {
fValue.Set(reflect.Zero(fType.Type))
} else {
removeSourceContextImpl(fValue)
}
}
default:
}
}

0 comments on commit 81b6e1c

Please sign in to comment.