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

Replace packr with stdlib embed #101

Merged
merged 4 commits into from
Jun 13, 2022
Merged
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
8 changes: 5 additions & 3 deletions credential/exchange/model_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package exchange

import (
"embed"
"testing"

"github.com/gobuffalo/packr/v2"
"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
)
Expand All @@ -21,7 +21,8 @@ const (
)

var (
peBox = packr.New("Presentation Exchange Test Vectors", "../test_vectors")
//go:embed testdata
testVectors embed.FS
)

// Round trip de/serialize to test our object models
Expand Down Expand Up @@ -179,5 +180,6 @@ func TestPresentationSubmission(t *testing.T) {
}

func getTestVector(fileName string) (string, error) {
return peBox.FindString(fileName)
b, err := testVectors.ReadFile("testdata/" + fileName)
return string(b), err
}
21 changes: 6 additions & 15 deletions credential/exchange/schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package exchange

import (
"github.com/gobuffalo/packr/v2"
"github.com/goccy/go-json"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -18,17 +17,13 @@ const (
submissionRequirementsSchema string = "pe-submission-requirements.json"
)

var (
schemaBox = packr.New("Presentation Exchange JSON Schemas", "../known_schemas")
)

// IsValidPresentationDefinition validates a given presentation definition object against its known JSON schema
func IsValidPresentationDefinition(definition PresentationDefinition) error {
jsonBytes, err := json.Marshal(definition)
if err != nil {
return errors.Wrap(err, "could not marshal presentation definition to JSON")
}
s, err := getKnownSchema(presentationDefinitionSchema)
s, err := schema.GetKnownSchema(presentationDefinitionSchema)
if err != nil {
return errors.Wrap(err, "could not get presentation definition schema")
}
Expand All @@ -45,7 +40,7 @@ func IsValidPresentationDefinitionEnvelope(definition PresentationDefinitionEnve
if err != nil {
return errors.Wrap(err, "could not marshal presentation definition to JSON")
}
s, err := getKnownSchema(presentationDefinitionEnvelopeSchema)
s, err := schema.GetKnownSchema(presentationDefinitionEnvelopeSchema)
if err != nil {
return errors.Wrap(err, "could not get presentation definition schema")
}
Expand All @@ -67,7 +62,7 @@ func IsValidPresentationSubmission(submission PresentationSubmission) error {
if err != nil {
return errors.Wrap(err, "could not marshal presentation submission to JSON")
}
s, err := getKnownSchema(presentationSubmissionSchema)
s, err := schema.GetKnownSchema(presentationSubmissionSchema)
if err != nil {
return errors.Wrap(err, "could not get presentation submission schema")
}
Expand All @@ -84,7 +79,7 @@ func IsValidFormatDeclaration(format ClaimFormat) error {
if err != nil {
return errors.Wrap(err, "could not marshal claim format to JSON")
}
s, err := getKnownSchema(formatDeclarationSchema)
s, err := schema.GetKnownSchema(formatDeclarationSchema)
if err != nil {
return errors.Wrap(err, "could not get claim format schema")
}
Expand All @@ -101,7 +96,7 @@ func IsValidSubmissionRequirement(requirement SubmissionRequirement) error {
if err != nil {
return errors.Wrap(err, "could not marshal submission requirement to JSON")
}
s, err := getKnownSchema(submissionRequirementSchema)
s, err := schema.GetKnownSchema(submissionRequirementSchema)
if err != nil {
return errors.Wrap(err, "could not get submission requirement schema")
}
Expand All @@ -123,7 +118,7 @@ func AreValidSubmissionRequirements(requirements []SubmissionRequirement) error
if err != nil {
return errors.Wrap(err, "could not marshal submission requirements to JSON")
}
s, err := getKnownSchema(submissionRequirementsSchema)
s, err := schema.GetKnownSchema(submissionRequirementsSchema)
if err != nil {
return errors.Wrap(err, "could not get submission requirements schema")
}
Expand All @@ -133,7 +128,3 @@ func AreValidSubmissionRequirements(requirements []SubmissionRequirement) error
}
return nil
}

func getKnownSchema(fileName string) (string, error) {
return schemaBox.FindString(fileName)
}
6 changes: 3 additions & 3 deletions credential/exchange/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (

// Get all schemas, make sure they're valid
func TestPresentationExchangeSchemas(t *testing.T) {
pdSchema, err := getKnownSchema(presentationDefinitionSchema)
pdSchema, err := schema.GetKnownSchema(presentationDefinitionSchema)
assert.NoError(t, err)
assert.NotEmpty(t, pdSchema)
err = schema.IsValidJSONSchema(pdSchema)
assert.NoError(t, err)

fdSchema, err := getKnownSchema(formatDeclarationSchema)
fdSchema, err := schema.GetKnownSchema(formatDeclarationSchema)
assert.NoError(t, err)
assert.NotEmpty(t, fdSchema)
err = schema.IsValidJSONSchema(fdSchema)
assert.NoError(t, err)

srSchema, err := getKnownSchema(submissionRequirementsSchema)
srSchema, err := schema.GetKnownSchema(submissionRequirementsSchema)
assert.NoError(t, err)
assert.NotEmpty(t, srSchema)
err = schema.IsValidJSONSchema(srSchema)
Expand Down
8 changes: 5 additions & 3 deletions credential/manifest/model_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package manifest

import (
"embed"
"testing"

"github.com/gobuffalo/packr/v2"
"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
)
Expand All @@ -16,7 +16,8 @@ const (
)

var (
manifestBox = packr.New("Credential Manifest Test Vectors", "../test_vectors")
//go:embed testdata
testVectors embed.FS
)

// Round trip de/serialize to test our object models, and check validity
Expand Down Expand Up @@ -104,5 +105,6 @@ func TestCredentialFulfillment(t *testing.T) {
}

func getTestVector(fileName string) (string, error) {
return manifestBox.FindString(fileName)
b, err := testVectors.ReadFile("testdata/" + fileName)
return string(b), err
}
17 changes: 4 additions & 13 deletions credential/manifest/schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package manifest

import (
"github.com/gobuffalo/packr/v2"
"github.com/goccy/go-json"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -16,17 +15,13 @@ const (
outputDescriptorsSchema string = "cm-output-descriptors.json"
)

var (
schemaBox = packr.New("Credential Manifest JSON Schemas", "../known_schemas")
)

// IsValidCredentialManifest validates a given credential manifest object against its known JSON schema
func IsValidCredentialManifest(manifest CredentialManifest) error {
jsonBytes, err := json.Marshal(manifest)
if err != nil {
return errors.Wrap(err, "could not marshal manifest to JSON")
}
s, err := getKnownSchema(credentialManifestSchema)
s, err := schema.GetKnownSchema(credentialManifestSchema)
if err != nil {
return errors.Wrap(err, "could not get credential manifest schema")
}
Expand All @@ -43,7 +38,7 @@ func IsValidCredentialApplication(application CredentialApplication) error {
if err != nil {
return errors.Wrap(err, "could not marshal application to JSON")
}
s, err := getKnownSchema(credentialApplicationSchema)
s, err := schema.GetKnownSchema(credentialApplicationSchema)
if err != nil {
return errors.Wrap(err, "could not get credential application schema")
}
Expand All @@ -65,7 +60,7 @@ func IsValidCredentialFulfillment(fulfillment CredentialFulfillment) error {
if err != nil {
return errors.Wrap(err, "could not marshal fulfillment to JSON")
}
s, err := getKnownSchema(credentialFulfillmentSchema)
s, err := schema.GetKnownSchema(credentialFulfillmentSchema)
if err != nil {
return errors.Wrap(err, "could not get credential fulfillment schema")
}
Expand All @@ -87,7 +82,7 @@ func AreValidOutputDescriptors(descriptors []OutputDescriptor) error {
if err != nil {
return errors.Wrap(err, "could not marshal output descriptors to JSON")
}
s, err := getKnownSchema(outputDescriptorsSchema)
s, err := schema.GetKnownSchema(outputDescriptorsSchema)
if err != nil {
return errors.Wrap(err, "could not get output descriptors schema")
}
Expand All @@ -97,7 +92,3 @@ func AreValidOutputDescriptors(descriptors []OutputDescriptor) error {
}
return nil
}

func getKnownSchema(fileName string) (string, error) {
return schemaBox.FindString(fileName)
}
8 changes: 4 additions & 4 deletions credential/manifest/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import (

// Get all schemas, make sure they're valid
func TestCredentialManifestSchemas(t *testing.T) {
cmSchema, err := getKnownSchema(credentialManifestSchema)
cmSchema, err := schema.GetKnownSchema(credentialManifestSchema)
assert.NoError(t, err)
assert.NotEmpty(t, cmSchema)
err = schema.IsValidJSONSchema(cmSchema)
assert.NoError(t, err)

caSchema, err := getKnownSchema(credentialApplicationSchema)
caSchema, err := schema.GetKnownSchema(credentialApplicationSchema)
assert.NoError(t, err)
assert.NotEmpty(t, caSchema)
err = schema.IsValidJSONSchema(caSchema)
assert.NoError(t, err)

cfSchema, err := getKnownSchema(credentialFulfillmentSchema)
cfSchema, err := schema.GetKnownSchema(credentialFulfillmentSchema)
assert.NoError(t, err)
assert.NotEmpty(t, cfSchema)
err = schema.IsValidJSONSchema(cfSchema)
assert.NoError(t, err)

odSchema, err := getKnownSchema(outputDescriptorsSchema)
odSchema, err := schema.GetKnownSchema(outputDescriptorsSchema)
assert.NoError(t, err)
assert.NotEmpty(t, odSchema)
err = schema.IsValidJSONSchema(odSchema)
Expand Down
10 changes: 5 additions & 5 deletions credential/model_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package credential

import (
"embed"
"testing"

"github.com/goccy/go-json"

"github.com/gobuffalo/packr/v2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -21,12 +21,12 @@ const (
)

var (
box = packr.New("VC & VP Test Vectors", "/test_vectors")
//go:embed testdata
testVectors embed.FS
vcTestVectors = []string{VCTestVector1, VCTestVector2, VCTestVector3, VCTestVector4}
vpTestVectors = []string{VPTestVector1, VPTestVector2}
)

// Before running, you'll need to execute `mage packr`
func TestVCVectors(t *testing.T) {
// round trip serialize and de-serialize from json to our object model
for _, tv := range vcTestVectors {
Expand All @@ -46,7 +46,6 @@ func TestVCVectors(t *testing.T) {
}
}

// Before running, you'll need to execute `mage packr`
func TestVPVectors(t *testing.T) {
// round trip serialize and de-serialize from json to our object model
for _, tv := range vpTestVectors {
Expand All @@ -67,5 +66,6 @@ func TestVPVectors(t *testing.T) {
}

func getTestVector(fileName string) (string, error) {
return box.FindString(fileName)
b, err := testVectors.ReadFile("testdata/" + fileName)
return string(b), err
}
8 changes: 5 additions & 3 deletions credential/rendering/model_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package rendering

import (
"embed"
"testing"

"github.com/gobuffalo/packr/v2"
"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
)
Expand All @@ -17,7 +17,8 @@ const (
)

var (
wrBox = packr.New("Wallet Rendering Test Vectors", "../test_vectors")
//go:embed testdata
testVectors embed.FS
)

func TestEntityStyleDescriptor(t *testing.T) {
Expand Down Expand Up @@ -103,5 +104,6 @@ func TestLabeledDisplayMappingObject(t *testing.T) {
}

func getTestVector(fileName string) (string, error) {
return wrBox.FindString(fileName)
b, err := testVectors.ReadFile("testdata/" + fileName)
return string(b), err
}
15 changes: 3 additions & 12 deletions credential/rendering/schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rendering

import (
"github.com/gobuffalo/packr/v2"
"github.com/goccy/go-json"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -15,17 +14,13 @@ const (
labeledDisplayMappingObjectSchema string = "wr-labeled-display-mapping-object.json"
)

var (
schemaBox = packr.New("Wallet Rendering JSON Schemas", "../known_schemas")
)

// IsValidEntityStyle validates an entity style descriptor against its known schema
func IsValidEntityStyle(esd EntityStyleDescriptor) error {
jsonBytes, err := json.Marshal(esd)
if err != nil {
return errors.Wrap(err, "could not marshal entity style descriptor")
}
s, err := getKnownSchema(entityStylesSchema)
s, err := schema.GetKnownSchema(entityStylesSchema)
if err != nil {
return errors.Wrap(err, "could not get entity styles schema")
}
Expand All @@ -42,7 +37,7 @@ func IsValidDisplayMappingObject(dmo DisplayMappingObject) error {
if err != nil {
return errors.Wrap(err, "could not marshal display mapping object")
}
s, err := getKnownSchema(displayMappingObjectSchema)
s, err := schema.GetKnownSchema(displayMappingObjectSchema)
if err != nil {
return errors.Wrap(err, "could not get display mapping object schema")
}
Expand All @@ -59,7 +54,7 @@ func IsValidLabeledDisplayMappingObject(ldmo LabeledDisplayMappingObject) error
if err != nil {
return errors.Wrap(err, "could not marshal labeled display mapping object")
}
s, err := getKnownSchema(labeledDisplayMappingObjectSchema)
s, err := schema.GetKnownSchema(labeledDisplayMappingObjectSchema)
if err != nil {
return errors.Wrap(err, "could not get labeled display mapping object schema")
}
Expand All @@ -69,7 +64,3 @@ func IsValidLabeledDisplayMappingObject(ldmo LabeledDisplayMappingObject) error
}
return nil
}

func getKnownSchema(fileName string) (string, error) {
return schemaBox.FindString(fileName)
}
Loading