Repository: anz-bank/sysl
Pull request #859 (2020-05-19 03:20:00)

Add ParseString fucntion
- Allows for sysl definitions to be parsed without a file system

codecov[bot]: # [Codecov](https://codecov.io/gh/anz-bank/sysl/pull/859?src=pr&el=h1) Report
> Merging [#859](https://codecov.io/gh/anz-bank/sysl/pull/859?src=pr&el=desc) into [master](https://codecov.io/gh/anz-bank/sysl/commit/ff4f5739b78e40ac9a8304983eb417704c839d8a&el=desc) will **decrease** coverage by `0.02%`.
> The diff coverage is `50.00%`.

[![Impacted file tree graph](https://codecov.io/gh/anz-bank/sysl/pull/859/graphs/tree.svg?width=650&height=150&src=pr&token=P88o1lvi9P)](https://codecov.io/gh/anz-bank/sysl/pull/859?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #859      +/-   ##
==========================================
- Coverage   83.83%   83.81%   -0.03%     
==========================================
  Files          72       72              
  Lines       10402    10410       +8     
==========================================
+ Hits         8721     8725       +4     
- Misses       1360     1362       +2     
- Partials      321      323       +2     
```


| [Impacted Files](https://codecov.io/gh/anz-bank/sysl/pull/859?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/parse/parse.go](https://codecov.io/gh/anz-bank/sysl/pull/859/diff?src=pr&el=tree#diff-cGtnL3BhcnNlL3BhcnNlLmdv) | `83.94% <50.00%> (-0.60%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/anz-bank/sysl/pull/859?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/anz-bank/sysl/pull/859?src=pr&el=footer). Last update [f2866c1...6577a66](https://codecov.io/gh/anz-bank/sysl/pull/859?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go
index e5539e92..28194a6d 100644
--- a/pkg/parse/parse.go
+++ b/pkg/parse/parse.go
@@ -6,6 +6,8 @@ import (
 	"sort"
 	"strings"
 
+	"github.com/spf13/afero"
+
 	"github.com/anz-bank/sysl/pkg/importer"
 
 	"github.com/antlr/antlr4/runtime/Go/antlr"
@@ -15,7 +17,6 @@ import (
 	"github.com/anz-bank/sysl/pkg/syslutil"
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
-	"github.com/spf13/afero"
 )
 
 // TypeData contains referenced type and actual tuple of referenced type
@@ -95,6 +96,19 @@ func (p *Parser) RestrictToLocalImport() {
 	p.allowAbsoluteImport = false
 }
 
+// ParseString parses a sysl definition in string form.
+func (p *Parser) ParseString(content string) (*sysl.Module, error) {
+	fs := afero.NewMemMapFs()
+	file, err := fs.Create("temp.sysl")
+	if err != nil {
+		return nil, err
+	}
+	if _, err := file.Write([]byte(content)); err != nil {
+		return nil, err
+	}
+	return p.Parse("temp.sysl", fs)
+}
+
 func (p *Parser) Parse(filename string, fs afero.Fs) (*sysl.Module, error) {
 	if !strings.HasSuffix(filename, ".sysl") {
 		filename += ".sysl"


Test patch:
diff --git a/pkg/parse/Parser_test.go b/pkg/parse/Parser_test.go
index 2f2cab66..05fa5b74 100644
--- a/pkg/parse/Parser_test.go
+++ b/pkg/parse/Parser_test.go
@@ -793,3 +793,12 @@ func TestInferExprTypeTransform(t *testing.T) {
 		})
 	}
 }
+
+func TestParseSysl(t *testing.T) {
+	content := `
+App:
+	Endpoint:
+		...`
+	_, err := NewParser().ParseString(content)
+	assert.Nil(t, err)
+}