-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pkger): add jsonnet support for package files
- Loading branch information
Showing
12 changed files
with
235 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package jsonnet | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/google/go-jsonnet" | ||
) | ||
|
||
// Decoder type can decoce a jsonnet stream into the given output. | ||
type Decoder struct { | ||
r io.Reader | ||
} | ||
|
||
// NewDecoder creates a new decoder. | ||
func NewDecoder(r io.Reader) *Decoder { | ||
return &Decoder{r: r} | ||
} | ||
|
||
// Decode decodes the stream into the provide value. | ||
func (d *Decoder) Decode(v interface{}) error { | ||
b, err := ioutil.ReadAll(d.r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vm := jsonnet.MakeVM() | ||
jsonStr, err := vm.EvaluateSnippet("memory", string(b)) | ||
if err != nil { | ||
return err | ||
} | ||
return json.Unmarshal([]byte(jsonStr), &v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package jsonnet_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/influxdata/influxdb/pkg/jsonnet" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecoder(t *testing.T) { | ||
type ( | ||
person struct { | ||
Name string `json:"name"` | ||
Welcome string `json:"welcome"` | ||
} | ||
|
||
persons struct { | ||
Person1 person `json:"person1"` | ||
Person2 person `json:"person2"` | ||
} | ||
) | ||
|
||
const entry = `{ | ||
person1: { | ||
name: "Alice", | ||
welcome: "Hello " + self.name + "!", | ||
}, | ||
person2: self.person1 { name: "Bob" }, | ||
}` | ||
|
||
var out persons | ||
require.NoError(t, jsonnet.NewDecoder(strings.NewReader(entry)).Decode(&out)) | ||
|
||
expected := persons{ | ||
Person1: person{ | ||
Name: "Alice", | ||
Welcome: "Hello Alice!", | ||
}, | ||
Person2: person{ | ||
Name: "Bob", | ||
Welcome: "Hello Bob!", | ||
}, | ||
} | ||
assert.Equal(t, expected, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.