From ca9fa550452a59235f334b7b5da574c86c22509d Mon Sep 17 00:00:00 2001 From: Stephanie Date: Thu, 25 Feb 2021 17:39:47 -0500 Subject: [PATCH 1/4] use set urimap in context Signed-off-by: Stephanie --- pkg/devfile/parser/context/context.go | 31 +++++++++++++++++++++------ pkg/devfile/parser/parse.go | 27 ++++++++++++++++++----- pkg/devfile/parser/parse_test.go | 1 + 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/pkg/devfile/parser/context/context.go b/pkg/devfile/parser/context/context.go index 252a920c..8ed32c88 100644 --- a/pkg/devfile/parser/context/context.go +++ b/pkg/devfile/parser/context/context.go @@ -9,8 +9,6 @@ import ( "k8s.io/klog" ) -var URIMap = make(map[string]bool) - // DevfileCtx stores context info regarding devfile type DevfileCtx struct { @@ -34,6 +32,9 @@ type DevfileCtx struct { // filesystem for devfile fs filesystem.Filesystem + + // trace of all url referenced + uriMap map[string]bool } // NewDevfileCtx returns a new DevfileCtx type object @@ -70,10 +71,13 @@ func (d *DevfileCtx) Populate() (err error) { return err } klog.V(4).Infof("absolute devfile path: '%s'", d.absPath) - if URIMap[d.absPath] { + if d.uriMap == nil { + d.uriMap= make(map[string]bool) + } + if d.uriMap[d.absPath] { return fmt.Errorf("URI %v is recursively referenced", d.absPath) } - URIMap[d.absPath] = true + d.uriMap[d.absPath] = true // Read and save devfile content if err := d.SetDevfileContent(); err != nil { return err @@ -88,10 +92,13 @@ func (d *DevfileCtx) PopulateFromURL() (err error) { if err != nil { return err } - if URIMap[d.url] { + if d.uriMap == nil { + d.uriMap= make(map[string]bool) + } + if d.uriMap[d.url] { return fmt.Errorf("URI %v is recursively referenced", d.url) } - URIMap[d.url] = true + d.uriMap[d.url] = true // Read and save devfile content if err := d.SetDevfileContent(); err != nil { return err @@ -132,3 +139,15 @@ func (d *DevfileCtx) SetAbsPath() (err error) { return nil } + +// GetURIMap func returns current devfile uri map +func (d *DevfileCtx) GetURIMap() map[string]bool { + return d.uriMap +} + +// GetURIMap func returns current devfile uri map +func (d *DevfileCtx) SetURIMap(uriMap map[string]bool){ + d.uriMap = uriMap +} + + diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index 087b3f3f..18e8bf5c 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -48,9 +48,7 @@ func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) { return DevfileObj{}, err } } - for uri := range devfileCtx.URIMap { - delete(devfileCtx.URIMap, uri) - } + // Successful return d, nil } @@ -191,9 +189,20 @@ func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj, return ParseFromURL(uri) } + // NewDevfileCtx + var d DevfileObj // relative path on disk if curDevfileCtx.GetAbsPath() != "" { - return Parse(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri)) + d.Ctx = devfileCtx.NewDevfileCtx(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri)) + d.Ctx.SetURIMap(curDevfileCtx.GetURIMap()) + + // Fill the fields of DevfileCtx struct + err = d.Ctx.Populate() + if err!= nil { + return DevfileObj{}, err + } + // return Parse(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri)) + return parseDevfile(d, true) } if curDevfileCtx.GetURL() != "" { @@ -203,8 +212,16 @@ func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj, } u.Path = path.Join(path.Dir(u.Path), uri) + d.Ctx = devfileCtx.NewURLDevfileCtx(u.String()) + d.Ctx.SetURIMap(curDevfileCtx.GetURIMap()) + // Fill the fields of DevfileCtx struct + err = d.Ctx.PopulateFromURL() + if err != nil { + return d, err + } + return parseDevfile(d, true) // u.String() is the joint absolute URL path - return ParseFromURL(u.String()) + // return ParseFromURL(u.String()) } return DevfileObj{}, fmt.Errorf("fail to parse from uri: %s", uri) diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index b1df4bd5..0c96d42e 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -2221,6 +2221,7 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T }, }, } + parentDevfile1 := DevfileObj{ Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath), Data: &v2.DevfileV2{ From ac993a8dbbff8e263842b3e93fe9cdb924fe2862 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Mon, 1 Mar 2021 18:20:59 -0500 Subject: [PATCH 2/4] restruct works Signed-off-by: Stephanie --- pkg/devfile/parser/context/context.go | 4 ++++ pkg/devfile/parser/parse.go | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/devfile/parser/context/context.go b/pkg/devfile/parser/context/context.go index 8ed32c88..670e059c 100644 --- a/pkg/devfile/parser/context/context.go +++ b/pkg/devfile/parser/context/context.go @@ -95,6 +95,10 @@ func (d *DevfileCtx) PopulateFromURL() (err error) { if d.uriMap == nil { d.uriMap= make(map[string]bool) } + fmt.Printf("uriMap length is: %v", len(d.uriMap)) + for k,_ := range d.uriMap { + fmt.Printf("uriMap now contains: %s", k) + } if d.uriMap[d.url] { return fmt.Errorf("URI %v is recursively referenced", d.url) } diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index 18e8bf5c..66569a5b 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -183,14 +183,21 @@ func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj, if err != nil { return DevfileObj{}, err } - + // NewDevfileCtx + var d DevfileObj // absolute URL address if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") { - return ParseFromURL(uri) + // return ParseFromURL(uri) + d.Ctx = devfileCtx.NewURLDevfileCtx(uri) + d.Ctx.SetURIMap(curDevfileCtx.GetURIMap()) + // Fill the fields of DevfileCtx struct + err = d.Ctx.PopulateFromURL() + if err != nil { + return DevfileObj{}, err + } + return parseDevfile(d, true) } - // NewDevfileCtx - var d DevfileObj // relative path on disk if curDevfileCtx.GetAbsPath() != "" { d.Ctx = devfileCtx.NewDevfileCtx(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri)) @@ -217,7 +224,7 @@ func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj, // Fill the fields of DevfileCtx struct err = d.Ctx.PopulateFromURL() if err != nil { - return d, err + return DevfileObj{}, err } return parseDevfile(d, true) // u.String() is the joint absolute URL path From 03d1eb5bd55efde5d51988d9a102ed38f6b6f9c5 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 2 Mar 2021 14:41:28 -0500 Subject: [PATCH 3/4] clean up the code Signed-off-by: Stephanie --- pkg/devfile/parser/context/context.go | 12 ++------ pkg/devfile/parser/parse.go | 41 +++++++++------------------ 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/pkg/devfile/parser/context/context.go b/pkg/devfile/parser/context/context.go index 670e059c..56621e91 100644 --- a/pkg/devfile/parser/context/context.go +++ b/pkg/devfile/parser/context/context.go @@ -72,7 +72,7 @@ func (d *DevfileCtx) Populate() (err error) { } klog.V(4).Infof("absolute devfile path: '%s'", d.absPath) if d.uriMap == nil { - d.uriMap= make(map[string]bool) + d.uriMap = make(map[string]bool) } if d.uriMap[d.absPath] { return fmt.Errorf("URI %v is recursively referenced", d.absPath) @@ -93,11 +93,7 @@ func (d *DevfileCtx) PopulateFromURL() (err error) { return err } if d.uriMap == nil { - d.uriMap= make(map[string]bool) - } - fmt.Printf("uriMap length is: %v", len(d.uriMap)) - for k,_ := range d.uriMap { - fmt.Printf("uriMap now contains: %s", k) + d.uriMap = make(map[string]bool) } if d.uriMap[d.url] { return fmt.Errorf("URI %v is recursively referenced", d.url) @@ -150,8 +146,6 @@ func (d *DevfileCtx) GetURIMap() map[string]bool { } // GetURIMap func returns current devfile uri map -func (d *DevfileCtx) SetURIMap(uriMap map[string]bool){ +func (d *DevfileCtx) SetURIMap(uriMap map[string]bool) { d.uriMap = uriMap } - - diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index 66569a5b..9de58979 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -185,51 +185,38 @@ func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj, } // NewDevfileCtx var d DevfileObj - // absolute URL address - if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") { - // return ParseFromURL(uri) - d.Ctx = devfileCtx.NewURLDevfileCtx(uri) - d.Ctx.SetURIMap(curDevfileCtx.GetURIMap()) - // Fill the fields of DevfileCtx struct - err = d.Ctx.PopulateFromURL() - if err != nil { - return DevfileObj{}, err - } - return parseDevfile(d, true) - } + absoluteURL := strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") // relative path on disk - if curDevfileCtx.GetAbsPath() != "" { + if !absoluteURL && curDevfileCtx.GetAbsPath() != "" { d.Ctx = devfileCtx.NewDevfileCtx(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri)) d.Ctx.SetURIMap(curDevfileCtx.GetURIMap()) // Fill the fields of DevfileCtx struct err = d.Ctx.Populate() - if err!= nil { + if err != nil { return DevfileObj{}, err } - // return Parse(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri)) return parseDevfile(d, true) } - if curDevfileCtx.GetURL() != "" { + // absolute URL address + if absoluteURL { + d.Ctx = devfileCtx.NewURLDevfileCtx(uri) + } else if curDevfileCtx.GetURL() != "" { u, err := url.Parse(curDevfileCtx.GetURL()) if err != nil { return DevfileObj{}, err } - u.Path = path.Join(path.Dir(u.Path), uri) d.Ctx = devfileCtx.NewURLDevfileCtx(u.String()) - d.Ctx.SetURIMap(curDevfileCtx.GetURIMap()) - // Fill the fields of DevfileCtx struct - err = d.Ctx.PopulateFromURL() - if err != nil { - return DevfileObj{}, err - } - return parseDevfile(d, true) - // u.String() is the joint absolute URL path - // return ParseFromURL(u.String()) } + d.Ctx.SetURIMap(curDevfileCtx.GetURIMap()) + // Fill the fields of DevfileCtx struct + err = d.Ctx.PopulateFromURL() + if err != nil { + return DevfileObj{}, err + } + return parseDevfile(d, true) - return DevfileObj{}, fmt.Errorf("fail to parse from uri: %s", uri) } From b4e99eb70938436f21c7bb93f455ea10095f0242 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Thu, 4 Mar 2021 14:54:10 -0500 Subject: [PATCH 4/4] fix the incorrect function comment Signed-off-by: Stephanie --- pkg/devfile/parser/context/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/devfile/parser/context/context.go b/pkg/devfile/parser/context/context.go index 56621e91..fa51fa7e 100644 --- a/pkg/devfile/parser/context/context.go +++ b/pkg/devfile/parser/context/context.go @@ -145,7 +145,7 @@ func (d *DevfileCtx) GetURIMap() map[string]bool { return d.uriMap } -// GetURIMap func returns current devfile uri map +// SetURIMap set uri map in the devfile ctx func (d *DevfileCtx) SetURIMap(uriMap map[string]bool) { d.uriMap = uriMap }