@@ -12,7 +12,6 @@ import (
1212 "fmt"
1313 "io"
1414 "os"
15- "regexp"
1615 "sort"
1716 "strconv"
1817 "strings"
@@ -58,11 +57,6 @@ const (
5857// documents and nodes. It is set to the OpenVEX public namespace by default.
5958var DefaultNamespace = PublicNamespace
6059
61- var (
62- contextRegExpPattern = fmt .Sprintf (`"@context":\s+"(%s\S*)"` , Context )
63- contextRegExp * regexp.Regexp
64- )
65-
6660// The VEX type represents a VEX document and all of its contained information.
6761type VEX struct {
6862 Metadata
@@ -265,28 +259,38 @@ func SortDocuments(docs []*VEX) []*VEX {
265259 return docs
266260}
267261
262+ // parseContext light parses a JSON document to look for the OpenVEX context locator
263+ func parseContext (rawDoc []byte ) (string , error ) {
264+ pd := struct {
265+ Context string `json:"@context"`
266+ }{}
267+
268+ if err := json .Unmarshal (rawDoc , & pd ); err != nil {
269+ return "" , fmt .Errorf ("parsing context from json data: %w" , err )
270+ }
271+
272+ if strings .HasPrefix (pd .Context , Context ) {
273+ return pd .Context , nil
274+ }
275+ return "" , nil
276+ }
277+
268278// Open tries to autodetect the vex format and open it
269279func Open (path string ) (* VEX , error ) {
270280 data , err := os .ReadFile (path )
271281 if err != nil {
272282 return nil , fmt .Errorf ("opening VEX file: %w" , err )
273283 }
274284
275- if bytes .Contains (data , []byte (ContextLocator ())) {
276- logrus .Info ("opening current vex" )
277- return Parse (data )
278- } else if bytes .Contains (data , []byte (Context )) {
279- logrus .Info ("Opening older openvex" )
280- if contextRegExp == nil {
281- contextRegExp = regexp .MustCompile (contextRegExpPattern )
282- }
283-
284- res := contextRegExp .FindSubmatch (data )
285- if len (res ) == 0 {
286- return nil , fmt .Errorf ("unable to parse OpenVEX version in document context" )
287- }
285+ documentContextLocator , err := parseContext (data )
286+ if err != nil {
287+ return nil , err
288+ }
288289
289- version := strings .TrimPrefix (string (res [1 ]), Context )
290+ if documentContextLocator == ContextLocator () {
291+ return Parse (data )
292+ } else if documentContextLocator != "" {
293+ version := strings .TrimPrefix (documentContextLocator , Context )
290294 version = strings .TrimPrefix (version , "/" )
291295
292296 // If version is nil, then we assume v0.0.1
0 commit comments