Skip to content

Commit

Permalink
AST part 1 (blackstork-io#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Morozko authored Oct 11, 2024
1 parent cf3c0d1 commit a10d378
Show file tree
Hide file tree
Showing 50 changed files with 5,705 additions and 295 deletions.
9 changes: 5 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/blackstork-io/fabric/cmd/fabctx"
"github.com/blackstork-io/fabric/cmd/internal/multilog"
"github.com/blackstork-io/fabric/cmd/internal/telemetry"
"github.com/blackstork-io/fabric/pkg/utils/slogutil"
)

var (
Expand Down Expand Up @@ -147,7 +148,7 @@ var rootCmd = &cobra.Command{
}
var logger *slog.Logger
if env.otelpEnabled || rawArgs.debug {
logger = slog.New(multilog.Handler{
handler = multilog.Handler{
Level: level,
Handlers: []slog.Handler{
handler,
Expand All @@ -156,10 +157,10 @@ var rootCmd = &cobra.Command{
otelslog.WithVersion(version),
),
},
})
} else {
logger = slog.New(handler)
}
}
handler = slogutil.NewSourceRewriter(handler)
logger = slog.New(handler)
logger = logger.With("command", cmd.Name())
slog.SetDefault(logger)
slog.SetLogLoggerLevel(slog.LevelDebug)
Expand Down
4 changes: 1 addition & 3 deletions examples/plugins/basic/content_greeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ func renderGreetingMessage(ctx context.Context, params *plugin.ProvideContentPar
// that it exists, non-null and non-empty, with whitespace trimmed
name := params.Args.GetAttrVal("name").AsString()
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: fmt.Sprintf("Hello, %s!", name),
},
Content: plugin.NewElementFromMarkdown(fmt.Sprintf("Hello, %s!", name)),
}, nil
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/Masterminds/sprig/v3 v3.2.3
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
github.com/blackstork-io/goldmark-markdown v0.1.3
github.com/elastic/go-elasticsearch/v8 v8.14.0
github.com/evanphx/go-hclog-slog v0.0.0-20240717231540-be48fc4c4df5
github.com/gobwas/glob v0.2.3
Expand All @@ -34,7 +35,7 @@ require (
github.com/testcontainers/testcontainers-go/modules/elasticsearch v0.32.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.46
github.com/yuin/goldmark v1.7.1
github.com/yuin/goldmark v1.7.4
github.com/zclconf/go-cty v1.14.4
go.opentelemetry.io/contrib/bridges/otelslog v0.4.0
go.opentelemetry.io/contrib/instrumentation/host v0.51.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x0
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/blackstork-io/goldmark-markdown v0.1.3 h1:L8s779mSocytvXAMhypOxqnQFrTUKQlyUjI4ZR+Ni9Q=
github.com/blackstork-io/goldmark-markdown v0.1.3/go.mod h1:0r4jctdZOy9oirGo/NikVBdPL4h+3TSs3e2hYRkGEs8=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
Expand Down Expand Up @@ -304,8 +306,8 @@ github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.46/go.mod h1:YCJyt5TSr4luj4
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8=
Expand Down
4 changes: 1 addition & 3 deletions internal/builtin/content_blockquote.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func genBlockQuoteContent(ctx context.Context, params *plugin.ProvideContentPara
}
text = "> " + strings.ReplaceAll(text, "\n", "\n> ")
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: text,
},
Content: plugin.NewElementFromMarkdown(text),
}, nil
}
27 changes: 12 additions & 15 deletions internal/builtin/content_blockquote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ func (s *BlockQuoteTestSuite) TestCallBlockquote() {
},
})
s.Empty(diags)
s.Equal(&plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: "> Hello World!",
},
}, content)
s.Equal(
plugindata.String("> Hello World!"),
content.Content.AsData().(plugindata.Map)["markdown"],
)
}

func (s *BlockQuoteTestSuite) TestCallBlockquoteMultiline() {
Expand All @@ -80,11 +79,10 @@ func (s *BlockQuoteTestSuite) TestCallBlockquoteMultiline() {
},
})
s.Empty(diags)
s.Equal(&plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: "> Hello\n> World\n> for you!",
},
}, content)
s.Equal(
plugindata.String("> Hello\n> World\n> for you!"),
content.Content.AsData().(plugindata.Map)["markdown"],
)
}

func (s *BlockQuoteTestSuite) TestCallBlockquoteMultilineDoubleNewline() {
Expand All @@ -99,9 +97,8 @@ func (s *BlockQuoteTestSuite) TestCallBlockquoteMultilineDoubleNewline() {
},
})
s.Empty(diags)
s.Equal(&plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: "> Hello\n> World\n> \n> for you!",
},
}, content)
s.Equal(
plugindata.String("> Hello\n> World\n> \n> for you!"),
content.Content.AsData().(plugindata.Map)["markdown"],
)
}
4 changes: 1 addition & 3 deletions internal/builtin/content_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ func genCodeContent(ctx context.Context, params *plugin.ProvideContentParams) (*
}
text = fmt.Sprintf("```%s\n%s\n```", lang.AsString(), text)
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: text,
},
Content: plugin.NewElementFromMarkdown(text),
}, nil
}
18 changes: 8 additions & 10 deletions internal/builtin/content_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ func (s *CodeTestSuite) TestCallCodeDefault() {
},
})
s.Empty(diags)
s.Equal(&plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: "```\nHello World!\n```",
},
}, content)
s.Equal(
plugindata.String("```\nHello World!\n```"),
content.Content.AsData().(plugindata.Map)["markdown"],
)
}

func (s *CodeTestSuite) TestCallCodeWithLanguage() {
Expand All @@ -78,9 +77,8 @@ func (s *CodeTestSuite) TestCallCodeWithLanguage() {
},
})
s.Empty(diags)
s.Equal(&plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: "```json\n{\"hello\": \"world\"}\n```",
},
}, content)
s.Equal(
plugindata.String("```json\n{\"hello\": \"world\"}\n```"),
content.Content.AsData().(plugindata.Map)["markdown"],
)
}
4 changes: 1 addition & 3 deletions internal/builtin/content_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ func genFrontMatterContent(ctx context.Context, params *plugin.ProvideContentPar
}}
}
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: result,
},
Content: plugin.NewElementFromMarkdown(result),
Location: &plugin.Location{
Index: 1,
Effect: plugin.LocationEffectBefore,
Expand Down
4 changes: 1 addition & 3 deletions internal/builtin/content_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ func genImageContent(ctx context.Context, params *plugin.ProvideContentParams) (
srcStr = strings.TrimSpace(strings.ReplaceAll(srcStr, "\n", ""))
altStr = strings.TrimSpace(strings.ReplaceAll(altStr, "\n", ""))
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: fmt.Sprintf("![%s](%s)", altStr, srcStr),
},
Content: plugin.NewElementFromMarkdown(fmt.Sprintf("![%s](%s)", altStr, srcStr)),
}, nil
}

Expand Down
4 changes: 1 addition & 3 deletions internal/builtin/content_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func genListContent(ctx context.Context, params *plugin.ProvideContentParams) (*
}}
}
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: result,
},
Content: plugin.NewElementFromMarkdown(result),
}, nil
}

Expand Down
4 changes: 1 addition & 3 deletions internal/builtin/content_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ func genTableContent(ctx context.Context, params *plugin.ProvideContentParams) (
}}
}
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: result,
},
Content: plugin.NewElementFromMarkdown(result),
}, nil
}

Expand Down
4 changes: 1 addition & 3 deletions internal/builtin/content_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ func genTextContent(ctx context.Context, params *plugin.ProvideContentParams) (*
}}
}
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: text,
},
Content: plugin.NewElementFromMarkdown(text),
}, nil
}

Expand Down
4 changes: 1 addition & 3 deletions internal/builtin/content_title.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ func genTitleContent(ctx context.Context, params *plugin.ProvideContentParams) (
text = strings.ReplaceAll(text, "\n", " ")
text = strings.Repeat("#", int(titleSize)+1) + " " + text
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: text,
},
Content: plugin.NewElementFromMarkdown(text),
}, nil
}

Expand Down
6 changes: 2 additions & 4 deletions internal/builtin/content_toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ func genTOC(ctx context.Context, params *plugin.ProvideContentParams) (*plugin.C
}

return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: titles.render(0, args.ordered),
},
Content: plugin.NewElementFromMarkdown(titles.render(0, args.ordered)),
}, nil
}

Expand Down Expand Up @@ -177,7 +175,7 @@ func extractTitles(section *plugin.ContentSection) []string {
if meta == nil || meta.Plugin != Name || meta.Provider != "title" {
continue
}
titles = append(titles, content.Markdown)
titles = append(titles, string(content.AsMarkdownSrc()))
}
}
return titles
Expand Down
9 changes: 8 additions & 1 deletion internal/elastic/data_elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"net/http"
"os"
"strings"
"testing"

es "github.com/elastic/go-elasticsearch/v8"
Expand Down Expand Up @@ -51,8 +52,14 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.ctx, "docker.io/elasticsearch:8.9.0",
elasticsearch.WithPassword("password123"),
)
if err != nil {
if strings.Contains(err.Error(), "Cannot connect to the Docker daemon") {
s.T().Skip("Docker not available for integration tests")
} else {
s.Require().NoError(err, "failed to start elasticsearch container")
}
}

s.Require().NoError(err, "failed to start elasticsearch container")
s.container = container
client, err := es.NewClient(es.Config{
Addresses: []string{
Expand Down
4 changes: 1 addition & 3 deletions internal/microsoft/content_azure_openai_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ func genOpenAIText(loader AzureOpenaiClientLoadFn) plugin.ProvideContentFunc {
}}
}
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: result,
},
Content: plugin.NewElementFromMarkdown(result),
}, nil
}
}
Expand Down
4 changes: 1 addition & 3 deletions internal/openai/content_openai_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ func genOpenAIText(loader ClientLoadFn) plugin.ProvideContentFunc {
}}
}
return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: result,
},
Content: plugin.NewElementFromMarkdown(result),
}, nil
}
}
Expand Down
9 changes: 8 additions & 1 deletion internal/postgresql/data_postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -49,7 +50,13 @@ func (s *IntegrationTestSuite) SetupSuite() {
WithOccurrence(2).
WithStartupTimeout(5*time.Second)),
)
s.Require().NoError(err, "failed to start postgres container")
if err != nil {
if strings.Contains(err.Error(), "Cannot connect to the Docker daemon") {
s.T().Skip("Docker not available for integration tests")
} else {
s.Require().NoError(err, "failed to start postgres container")
}
}
s.container = container
connURL, err := container.ConnectionString(s.ctx, "sslmode=disable")
s.Require().NoError(err, "failed to get postgres connection string")
Expand Down
9 changes: 5 additions & 4 deletions internal/stixview/content_stixview.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/blackstork-io/fabric/pkg/diagnostics"
"github.com/blackstork-io/fabric/plugin"
"github.com/blackstork-io/fabric/plugin/ast"
"github.com/blackstork-io/fabric/plugin/dataspec"
"github.com/blackstork-io/fabric/plugin/plugindata"
)
Expand Down Expand Up @@ -130,7 +131,7 @@ func renderStixView(ctx context.Context, params *plugin.ProvideContentParams) (*
if rctx.Objects == nil && args.StixURL == nil && args.GistID == nil {
return nil, diagnostics.Diag{{
Severity: hcl.DiagError,
Summary: "Missing arugments",
Summary: "Missing arguments",
Detail: "Must provide either stix_url or gist_id or objects",
}}
}
Expand All @@ -145,9 +146,9 @@ func renderStixView(ctx context.Context, params *plugin.ProvideContentParams) (*
}

return &plugin.ContentResult{
Content: &plugin.ContentElement{
Markdown: buf.String(),
},
Content: plugin.NewElement(
ast.HTMLBlock(buf.Bytes()),
),
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/stixview/content_stixview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *StixViewTestSuite) TestGistID() {
`<script src="https://unpkg.com/stixview/dist/stixview.bundle.js" type="text/javascript"></script>`,
`<div data-stix-gist-id="123">`,
`</div>`,
``,
}, "\n"), mdprint.PrintString(res.Content))
}

Expand All @@ -68,6 +69,7 @@ func (s *StixViewTestSuite) TestStixURL() {
`<script src="https://unpkg.com/stixview/dist/stixview.bundle.js" type="text/javascript"></script>`,
`<div data-stix-url="https://example.com/stix.json">`,
`</div>`,
``,
}, "\n"), mdprint.PrintString(res.Content))
}

Expand Down Expand Up @@ -97,6 +99,7 @@ func (s *StixViewTestSuite) TestAllArgs() {
`<script src="https://unpkg.com/stixview/dist/stixview.bundle.js" type="text/javascript"></script>`,
`<div data-stix-gist-id="123" data-show-sidebar="true" data-show-footer="true" data-show-tlp-as-tags="true" data-caption="test caption" data-show-marking-nodes="true" data-show-labels="true" data-show-idrefs="true" data-graph-width="400" data-graph-height="300">`,
`</div>`,
``,
}, "\n"), mdprint.PrintString(res.Content))
}

Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test-run:

format:
go mod tidy
./gen_code.sh
./codegen/format.sh

format-extra: format
gofumpt -w -extra .
Expand Down
Loading

0 comments on commit a10d378

Please sign in to comment.