-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,77 @@ | ||
package ipnisync | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
const ( | ||
// CidSchemaHeader is the HTTP header used as an optional hint about the | ||
// type of data requested by a CID. | ||
CidSchemaHeader = "Ipni-Cid-Schema-Type" | ||
// CidSchemaAd is a value for the CidSchemaHeader specifying advertiesement | ||
// data is being requested. | ||
CidSchemaAd = "advertisement" | ||
// CidSchemaAdvertisement is a value for the CidSchemaHeader specifying | ||
// advertiesement data is being requested. Referrs to Advertisement in | ||
// https://github.com/ipni/go-libipni/blob/main/ingest/schema/schema.ipldsch | ||
CidSchemaAdvertisement = "Advertisement" | ||
// CidSchemaEntries is a value for the CidSchemaHeader specifying | ||
// advertisement entries (multihash chunks) data is being requested. | ||
CidSchemaEntries = "entries" | ||
// Referrs to Entry chunk in | ||
// https://github.com/ipni/go-libipni/blob/main/ingest/schema/schema.ipldsch | ||
CidSchemaEntryChunk = "EntryChunk" | ||
) | ||
|
||
var ErrUnknownCidSchema = errors.New("unknown cid schema type value") | ||
|
||
// cidSchemaTypeKey is the type used for the key of CidSchemaHeader when set as | ||
// a context value. | ||
type cidSchemaTypeCtxKey string | ||
|
||
// CidSchemaCtxKey is used as the key when creating a context with a value or extracting the cid schema from a context. Examples: | ||
// cidSchemaCtxKey is used to get the key used to store or extract the cid | ||
// schema value in a context. | ||
const cidSchemaCtxKey cidSchemaTypeCtxKey = CidSchemaHeader | ||
|
||
// CidSchemaFromCtx extracts the CID schema name from the context. If the | ||
// scheam value is not set, then returns "". If the schema value is set, but is | ||
// not recognized, then ErrUnknownCidSchema is returned along with the value. | ||
// | ||
// ctx := context.WithValue(ctx, CidSchemaCtxKey, CidSchemaAd) | ||
// Returning unrecognized values with an error allows consumers to retrieved | ||
// newer values that are not recognized by an older version of this library. | ||
func CidSchemaFromCtx(ctx context.Context) (string, error) { | ||
if ctx == nil { | ||
return "", nil | ||
} | ||
cidSchemaType, ok := ctx.Value(cidSchemaCtxKey).(string) | ||
if !ok { | ||
return "", nil | ||
} | ||
|
||
var err error | ||
switch cidSchemaType { | ||
case CidSchemaAdvertisement, CidSchemaEntryChunk: | ||
default: | ||
err = ErrUnknownCidSchema | ||
} | ||
return cidSchemaType, err | ||
} | ||
|
||
// CtxWithCidSchema creates a derived context that has the specified value for | ||
// the CID schema type. | ||
// | ||
// cidSchemaType, ok := ctx.Value(CidSchemaCtxKey).(string) | ||
const CidSchemaCtxKey cidSchemaTypeCtxKey = CidSchemaHeader | ||
// Setting an unrecognized value, even when an error is retruned, allows | ||
// producers to set context values that are not recognized by an older version | ||
// of this library. | ||
func CtxWithCidSchema(ctx context.Context, cidSchemaType string) (context.Context, error) { | ||
if cidSchemaType == "" { | ||
return ctx, nil | ||
} | ||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
var err error | ||
switch cidSchemaType { | ||
case CidSchemaAdvertisement, CidSchemaEntryChunk: | ||
default: | ||
err = ErrUnknownCidSchema | ||
} | ||
return context.WithValue(ctx, cidSchemaCtxKey, cidSchemaType), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ipnisync_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ipni/go-libipni/dagsync/ipnisync" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCtxWithCidSchema(t *testing.T) { | ||
ctx, err := ipnisync.CtxWithCidSchema(nil, "") | ||
require.NoError(t, err) | ||
require.Nil(t, ctx) | ||
|
||
ctxOrig := context.Background() | ||
ctx, err = ipnisync.CtxWithCidSchema(ctxOrig, "") | ||
require.NoError(t, err) | ||
require.Equal(t, ctxOrig, ctx) | ||
|
||
ctx, err = ipnisync.CtxWithCidSchema(ctxOrig, ipnisync.CidSchemaAdvertisement) | ||
require.NoError(t, err) | ||
require.NotEqual(t, ctxOrig, ctx) | ||
|
||
value, err := ipnisync.CidSchemaFromCtx(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, ipnisync.CidSchemaAdvertisement, value) | ||
|
||
ctx, err = ipnisync.CtxWithCidSchema(nil, ipnisync.CidSchemaEntryChunk) | ||
require.NoError(t, err) | ||
value, err = ipnisync.CidSchemaFromCtx(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, ipnisync.CidSchemaEntryChunk, value) | ||
|
||
value, err = ipnisync.CidSchemaFromCtx(ctxOrig) | ||
require.NoError(t, err) | ||
require.Empty(t, value) | ||
|
||
const unknownVal = "unknown" | ||
|
||
// Setting unknown value returns error as well as context with value. | ||
ctx, err = ipnisync.CtxWithCidSchema(ctxOrig, unknownVal) | ||
require.ErrorIs(t, err, ipnisync.ErrUnknownCidSchema) | ||
require.NotNil(t, ctxOrig, ctx) | ||
|
||
// Getting unknown value returns error as well as value. | ||
value, err = ipnisync.CidSchemaFromCtx(ctx) | ||
require.ErrorIs(t, err, ipnisync.ErrUnknownCidSchema) | ||
require.Equal(t, unknownVal, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters