Skip to content

Commit

Permalink
fix(source): prepare payload based on file type - text vs non-text ba…
Browse files Browse the repository at this point in the history
…sed (#238)

# 📥 Pull Request

fix #235

## ❓ What are you trying to address

This pull request includes several changes across multiple files to fix
an error with referencing non-text sources for Fabric Item Definition
parts include testing of non-text definition part (png file).
  • Loading branch information
DariuszPorowski authored Feb 12, 2025
1 parent a0b6ea7 commit 7d47ea2
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20250206-140422.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: fixed
body: Error when referencing non-text source for Fabric Item Definition part.
time: 2025-02-06T14:04:22.220999+01:00
custom:
Issue: "235"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ __debug*
testresults.xml
golangci-report.xml
.wellknown.json
.wellknown.*.json
!.wellknown.template.json
changie.md
.lycheecache
*.env
Expand Down
11 changes: 7 additions & 4 deletions docs/resources/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ resource "fabric_report" "example_bootstrap" {
"SemanticModelID" = "00000000-0000-0000-0000-000000000000"
}
}
"StaticResources/SharedResources/BaseThemes/CY24SU06.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU06.json"
"StaticResources/SharedResources/BaseThemes/CY24SU10.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU10.json"
}
"StaticResources/RegisteredResources/fabric_48_color10148978481469717.png" = {
source = "${local.path}/StaticResources/RegisteredResources/fabric_48_color10148978481469717.png"
}
}
}
Expand All @@ -56,8 +59,8 @@ resource "fabric_report" "example_update" {
"SemanticModelID" = "00000000-0000-0000-0000-000000000000"
}
}
"StaticResources/SharedResources/BaseThemes/CY24SU06.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU06.json"
"StaticResources/SharedResources/BaseThemes/CY24SU10.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU10.json"
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions examples/resources/fabric_report/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ resource "fabric_report" "example_bootstrap" {
"SemanticModelID" = "00000000-0000-0000-0000-000000000000"
}
}
"StaticResources/SharedResources/BaseThemes/CY24SU06.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU06.json"
"StaticResources/SharedResources/BaseThemes/CY24SU10.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU10.json"
}
"StaticResources/RegisteredResources/fabric_48_color10148978481469717.png" = {
source = "${local.path}/StaticResources/RegisteredResources/fabric_48_color10148978481469717.png"
}
}
}
Expand All @@ -35,8 +38,8 @@ resource "fabric_report" "example_update" {
"SemanticModelID" = "00000000-0000-0000-0000-000000000000"
}
}
"StaticResources/SharedResources/BaseThemes/CY24SU06.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU06.json"
"StaticResources/SharedResources/BaseThemes/CY24SU10.json" = {
source = "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU10.json"
}
}
}
7 changes: 3 additions & 4 deletions internal/pkg/fabricitem/models_resource_item_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ func (to *fabricItemDefinition) setParts(ctx context.Context, definition superty
}

if (len(defParts) == 0) && len(definitionPaths) > 0 && update {
content := definitionEmpty

if err := transforms.Base64Encode(&content); err != nil {
contentB64, err := transforms.Base64Encode(definitionEmpty)
if err != nil {
diags.AddError(
common.ErrorBase64EncodeHeader,
err.Error(),
Expand All @@ -67,7 +66,7 @@ func (to *fabricItemDefinition) setParts(ctx context.Context, definition superty

to.Parts = append(to.Parts, fabcore.ItemDefinitionPart{
Path: &definitionPaths[0],
Payload: &content,
Payload: &contentB64,
PayloadType: azto.Ptr(fabcore.PayloadTypeInlineBase64),
})

Expand Down
104 changes: 68 additions & 36 deletions internal/pkg/transforms/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package transforms
import (
"bytes"
"context"
"os"
"path/filepath"
"text/template"
"unicode/utf8"

"github.com/go-sprout/sprout"
sproutchecksum "github.com/go-sprout/sprout/registry/checksum"
Expand Down Expand Up @@ -35,8 +37,8 @@ func getTmplFuncs() (template.FuncMap, error) {
handler := sprout.New()

err := handler.AddRegistries(
sproutconversion.NewRegistry(),
sproutchecksum.NewRegistry(),
sproutconversion.NewRegistry(),
sproutencoding.NewRegistry(),
sproutmaps.NewRegistry(),
sproutnumeric.NewRegistry(),
Expand Down Expand Up @@ -67,15 +69,7 @@ func SourceFileToPayload(ctx context.Context, srcPath types.String, tokens super

source := srcPath.ValueString()

tmplFuncs, err := getTmplFuncs()
if err != nil {
diags.AddError(
"Template functions error",
err.Error(),
)
}

tmpl, err := template.New("tmpl").Funcs(tmplFuncs).ParseFiles(source)
content, err := os.ReadFile(srcPath.ValueString())
if err != nil {
diags.AddError(
common.ErrorFileReadHeader,
Expand All @@ -85,50 +79,88 @@ func SourceFileToPayload(ctx context.Context, srcPath types.String, tokens super
return nil, nil, diags
}

tokensData := map[string]string{}
var contentSha256 string
var contentB64 string

if utf8.Valid(content) { //nolint:nestif
tmplFuncs, err := getTmplFuncs()
if err != nil {
diags.AddError(
"Template functions error",
err.Error(),
)

if !tokens.IsNull() && !tokens.IsUnknown() {
tokensData, diags = tokens.Get(ctx)
if diags.HasError() {
return nil, nil, diags
}
}

var contentBuf bytes.Buffer
if err := tmpl.ExecuteTemplate(&contentBuf, filepath.Base(source), tokensData); err != nil {
diags.AddError(
common.ErrorTmplParseHeader,
err.Error(),
)
tmpl, err := template.New("tmpl").Funcs(tmplFuncs).ParseFiles(source)
if err != nil {
diags.AddError(
common.ErrorFileReadHeader,
err.Error(),
)

return nil, nil, diags
}
return nil, nil, diags
}

content := contentBuf.String()
tokensData := map[string]string{}

if IsJSON(content) {
if err := JSONNormalize(&content); err != nil {
if !tokens.IsNull() && !tokens.IsUnknown() {
tokensData, diags = tokens.Get(ctx)
if diags.HasError() {
return nil, nil, diags
}
}

var contentBuf bytes.Buffer
if err := tmpl.ExecuteTemplate(&contentBuf, filepath.Base(source), tokensData); err != nil {
diags.AddError(
common.ErrorJSONNormalizeHeader,
common.ErrorTmplParseHeader,
err.Error(),
)

return nil, nil, diags
}
}

contentSha256 := utils.Sha256(content)
content := contentBuf.String()

if err := Base64Encode(&content); err != nil {
diags.AddError(
common.ErrorBase64EncodeHeader,
err.Error(),
)
if IsJSON(content) {
if err := JSONNormalize(&content); err != nil {
diags.AddError(
common.ErrorJSONNormalizeHeader,
err.Error(),
)

return nil, nil, diags
return nil, nil, diags
}
}

contentSha256 = utils.Sha256(content)

contentB64, err = Base64Encode(content)
if err != nil {
diags.AddError(
common.ErrorBase64EncodeHeader,
err.Error(),
)

return nil, nil, diags
}
} else {
contentSha256 = utils.Sha256(content)

contentB64, err = Base64Encode(content)
if err != nil {
diags.AddError(
common.ErrorBase64EncodeHeader,
err.Error(),
)

return nil, nil, diags
}
}

return &content, &contentSha256, diags
return &contentB64, &contentSha256, nil
}

func PayloadToGzip(content *string) diag.Diagnostics {
Expand Down
16 changes: 9 additions & 7 deletions internal/pkg/transforms/transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ func Base64Decode(content *string) error {
return nil
}

func Base64Encode(content *string) error {
if content == nil {
return nil
}
func Base64Encode[T string | []byte](content T) (string, error) {
var payload string

payload := byteToBase64([]byte(*content))
*content = strings.TrimSpace(payload)
switch v := any(content).(type) {
case string:
payload = byteToBase64([]byte(v))
case []byte:
payload = byteToBase64(v)
}

return nil
return payload, nil
}

func Base64GzipEncode(content *string) error {
Expand Down
11 changes: 9 additions & 2 deletions internal/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ func SortMapStringByKeys[T any](m map[string]T) map[string]T {
return result
}

func Sha256(content string) string {
hash := sha256.Sum256([]byte(content))
func Sha256[T string | []byte](content T) string {
var hash [32]byte

switch v := any(content).(type) {
case string:
hash = sha256.Sum256([]byte(v))
case []byte:
hash = sha256.Sum256(v)
}

return hex.EncodeToString(hash[:])
}
7 changes: 5 additions & 2 deletions internal/services/report/resource_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ var testHelperDefinition = map[string]any{
"SemanticModelID": "00000000-0000-0000-0000-000000000000",
},
},
`"StaticResources/SharedResources/BaseThemes/CY24SU06.json"`: map[string]any{
"source": "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU06.json",
`"StaticResources/SharedResources/BaseThemes/CY24SU10.json"`: map[string]any{
"source": "${local.path}/StaticResources/SharedResources/BaseThemes/CY24SU10.json",
},
`"StaticResources/RegisteredResources/fabric_48_color10148978481469717.png"`: map[string]any{
"source": "${local.path}/StaticResources/RegisteredResources/fabric_48_color10148978481469717.png",
},
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "CY24SU06",
"name": "CY24SU10",
"dataColors": [
"#118DFF",
"#12239E",
Expand Down Expand Up @@ -326,6 +326,19 @@
{
"maxTiles": 3
}
],
"overflow": [
{
"type": 0
}
],
"image": [
{
"fixedSize": false
},
{
"imageAreaSize": 50
}
]
}
},
Expand Down Expand Up @@ -554,13 +567,34 @@
"responsive": true
}
],
"smallMultiplesLayout": [
{
"backgroundTransparency": 0,
"gridLineType": "inner"
}
],
"valueAxis": [
{
"show": true
}
]
}
},
"hundredPercentStackedAreaChart": {
"*": {
"general": [
{
"responsive": true
}
],
"smallMultiplesLayout": [
{
"backgroundTransparency": 0,
"gridLineType": "inner"
}
]
}
},
"group": {
"*": {
"background": [
Expand Down
Loading

0 comments on commit 7d47ea2

Please sign in to comment.