Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path_join function to the stdlib #821

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ internal API changes are not present.
Main (unreleased)
-----------------

### Features

- Add the function `path_join` to the stdlib. (@wildum)

v1.1.0-rc.0
-----------

Expand Down
20 changes: 20 additions & 0 deletions docs/sources/reference/stdlib/path_join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
canonical: https://grafana.com/docs/alloy/latest/reference/stdlib/path_join/
description: Learn about path_join
title: path_join
---

# path_join

`path_join` joins any number of path elements into a single path, separating them with an OS specific separator.

## Examples

```alloy
> path_join("this/is", "a/path")
"this/is/a/path"
> path_join("empty/path", "")
"empty/path"
> join("foo/", "/bar/", "foo/bar", "foo")
"foo/bar/foo/bar/foo"
```
2 changes: 2 additions & 0 deletions syntax/internal/stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/grafana/alloy/syntax/alloytypes"
Expand Down Expand Up @@ -129,4 +130,5 @@ var Identifiers = map[string]interface{}{
"trim_prefix": strings.TrimPrefix,
"trim_suffix": strings.TrimSuffix,
"trim_space": strings.TrimSpace,
"path_join": filepath.Join,
}
1 change: 1 addition & 0 deletions syntax/vm/vm_stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func TestStdlib_StringFunc(t *testing.T) {
{"trim2", `trim(" hello! world.! ", "! ")`, "hello! world."},
{"trim_prefix", `trim_prefix("helloworld", "hello")`, "world"},
{"trim_suffix", `trim_suffix("helloworld", "world")`, "hello"},
{"path_join", `path_join("this/is", "a/path")`, "this/is/a/path"},
}

for _, tc := range tt {
Expand Down
Loading