Skip to content

Commit

Permalink
revert policy marshal type location
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodek committed Jun 14, 2024
1 parent 3b3bbe8 commit e1135a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 2 additions & 8 deletions acp/acp_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/sourcenetwork/acp_core/pkg/runtime"
"github.com/sourcenetwork/acp_core/pkg/types"
"github.com/sourcenetwork/immutable"
"github.com/valyala/fastjson"
)

const localACPStoreName = "local_acp"
Expand Down Expand Up @@ -108,6 +107,7 @@ func (l *ACPLocal) AddPolicy(
ctx context.Context,
creatorID string,
policy string,
marshalType policyMarshalType,
creationTime *protoTypes.Timestamp,
) (string, error) {
principal, err := auth.NewDIDPrincipal(creatorID)
Expand All @@ -116,19 +116,13 @@ func (l *ACPLocal) AddPolicy(
}
ctx = auth.InjectPrincipal(ctx, principal)

marshalType := types.PolicyMarshalingType_SHORT_YAML
if isJSON := fastjson.Validate(policy) == nil; isJSON { // Detect JSON format.
marshalType = types.PolicyMarshalingType_SHORT_JSON
}

createPolicy := types.CreatePolicyRequest{
Policy: policy,
MarshalType: marshalType,
MarshalType: types.PolicyMarshalingType(marshalType),
CreationTime: protoTypes.TimestampNow(),
}

response, err := l.engine.CreatePolicy(ctx, &createPolicy)

if err != nil {
return "", err
}
Expand Down
8 changes: 8 additions & 0 deletions acp/source_hub_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
protoTypes "github.com/cosmos/gogoproto/types"
"github.com/sourcenetwork/corelog"
"github.com/sourcenetwork/immutable"
"github.com/valyala/fastjson"
)

// sourceHubClient is a private abstraction to allow multiple ACP implementations
Expand All @@ -40,6 +41,7 @@ type sourceHubClient interface {
ctx context.Context,
creatorID string,
policy string,
marshalType policyMarshalType,
creationTime *protoTypes.Timestamp,
) (string, error)

Expand Down Expand Up @@ -116,10 +118,16 @@ func (a *sourceHubBridge) AddPolicy(ctx context.Context, creatorID string, polic
return "", ErrPolicyDataMustNotBeEmpty
}

marshalType := policyMarshalType_YAML
if isJSON := fastjson.Validate(policy) == nil; isJSON { // Detect JSON format.
marshalType = policyMarshalType_JSON
}

policyID, err := a.client.AddPolicy(
ctx,
creatorID,
policy,
marshalType,
protoTypes.TimestampNow(),
)

Expand Down
9 changes: 9 additions & 0 deletions acp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const (
RegistrationResult_Unarchived RegistrationResult = 2
)

// policyMarshalType represents the format in which a policy
// is marshaled as
type policyMarshalType int32

const (
policyMarshalType_YAML policyMarshalType = 1
policyMarshalType_JSON policyMarshalType = 2
)

// policy is a data container carrying the necessary data
// to verify whether a policy meets DPI requirements
type policy struct {
Expand Down

0 comments on commit e1135a1

Please sign in to comment.