-
Notifications
You must be signed in to change notification settings - Fork 208
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
feat!: Discover Features V2 #991
Merged
TimoGlastra
merged 14 commits into
openwallet-foundation:0.3.0-pre
from
2060-io:feat/discover-features-v2
Sep 9, 2022
+2,213
−247
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8af513c
feat: discover features v2 preliminary implementation
genaris 6b8f25e
fix: module tests, lint issues
genaris 284d62b
feat: services as subclasses, add events / config
genaris dc24461
feat: expose featureRegistry & add some e2e tests
genaris 53a73c7
docs: some API updates
genaris 54d6cf1
feat: add synchronous feature query
genaris 46f1393
test: service and e2e tests for v1 and v2
genaris 95304c6
refactor: move FeatureRegistry to agent + module register() args order
genaris 96c6397
feat: add messages to discover features events
genaris 0c00b24
refactor: rename module options
genaris c923d52
refactor: query return type
genaris 9d03b4c
fix: query return type
genaris 91e4c05
Merge branch '0.3.0-pre' into feat/discover-features-v2
genaris 8102ae0
Merge branch '0.3.0-pre' into feat/discover-features-v2
genaris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { FeatureQuery, Feature } from './models' | ||
|
||
import { injectable } from 'tsyringe' | ||
|
||
@injectable() | ||
class FeatureRegistry { | ||
private features: Feature[] = [] | ||
|
||
/** | ||
* Register a single or set of Features on the registry | ||
* | ||
* @param features set of {Feature} objects or any inherited class | ||
*/ | ||
public register(...features: Feature[]) { | ||
for (const feature of features) { | ||
const index = this.features.findIndex((item) => item.type === feature.type && item.id === feature.id) | ||
|
||
if (index > -1) { | ||
this.features[index] = this.features[index].combine(feature) | ||
} else { | ||
this.features.push(feature) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Perform a set of queries in the registry, supporting wildcards (*) as | ||
* expressed in Aries RFC 0557. | ||
* | ||
* @see https://github.com/hyperledger/aries-rfcs/blob/560ffd23361f16a01e34ccb7dcc908ec28c5ddb1/features/0557-discover-features-v2/README.md | ||
* | ||
* @param queries set of {FeatureQuery} objects to query features | ||
* @returns array containing all matching features (can be empty) | ||
*/ | ||
public query(...queries: FeatureQuery[]) { | ||
const output = [] | ||
for (const query of queries) { | ||
const items = this.features.filter((item) => item.type === query.featureType) | ||
// An * will return all features of a given type (e.g. all protocols, all goal codes, all AIP configs) | ||
if (query.match === '*') { | ||
output.push(...items) | ||
// An string ending with * will return a family of features of a certain type | ||
// (e.g. all versions of a given protocol, all subsets of an AIP, etc.) | ||
} else if (query.match.endsWith('*')) { | ||
const match = query.match.slice(0, -1) | ||
output.push(...items.filter((m) => m.id.startsWith(match))) | ||
// Exact matching (single feature) | ||
} else { | ||
output.push(...items.filter((m) => m.id === query.match)) | ||
} | ||
} | ||
return output | ||
} | ||
} | ||
|
||
export { FeatureRegistry } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Expose } from 'class-transformer' | ||
import { IsString } from 'class-validator' | ||
|
||
import { AriesFrameworkError } from '../../../error' | ||
import { JsonTransformer } from '../../../utils/JsonTransformer' | ||
|
||
export interface FeatureOptions { | ||
id: string | ||
type: string | ||
} | ||
|
||
export class Feature { | ||
public id!: string | ||
|
||
public constructor(props: FeatureOptions) { | ||
if (props) { | ||
this.id = props.id | ||
this.type = props.type | ||
} | ||
} | ||
|
||
@IsString() | ||
@Expose({ name: 'feature-type' }) | ||
public readonly type!: string | ||
|
||
/** | ||
* Combine this feature with another one, provided both are from the same type | ||
* and have the same id | ||
* | ||
* @param feature object to combine with this one | ||
* @returns a new object resulting from the combination between this and feature | ||
*/ | ||
public combine(feature: this) { | ||
if (feature.id !== this.id) { | ||
throw new AriesFrameworkError('Can only combine with a feature with the same id') | ||
} | ||
|
||
const obj1 = JsonTransformer.toJSON(this) | ||
const obj2 = JsonTransformer.toJSON(feature) | ||
|
||
for (const key in obj2) { | ||
try { | ||
if (Array.isArray(obj2[key])) { | ||
obj1[key] = [...new Set([...obj1[key], ...obj2[key]])] | ||
} else { | ||
obj1[key] = obj2[key] | ||
} | ||
} catch (e) { | ||
obj1[key] = obj2[key] | ||
} | ||
} | ||
return JsonTransformer.fromJSON(obj1, Feature) | ||
} | ||
|
||
public toJSON(): Record<string, unknown> { | ||
return JsonTransformer.toJSON(this) | ||
} | ||
} |
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,23 @@ | ||
import { Expose } from 'class-transformer' | ||
import { IsString } from 'class-validator' | ||
|
||
export interface FeatureQueryOptions { | ||
featureType: string | ||
match: string | ||
} | ||
|
||
export class FeatureQuery { | ||
public constructor(options: FeatureQueryOptions) { | ||
if (options) { | ||
this.featureType = options.featureType | ||
this.match = options.match | ||
} | ||
} | ||
|
||
@Expose({ name: 'feature-type' }) | ||
@IsString() | ||
public featureType!: string | ||
|
||
@IsString() | ||
public match!: string | ||
} |
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,13 @@ | ||
import type { FeatureOptions } from './Feature' | ||
|
||
import { Feature } from './Feature' | ||
|
||
export type GoalCodeOptions = Omit<FeatureOptions, 'type'> | ||
|
||
export class GoalCode extends Feature { | ||
public constructor(props: GoalCodeOptions) { | ||
super({ ...props, type: GoalCode.type }) | ||
} | ||
|
||
public static readonly type = 'goal-code' | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/core/src/agent/models/features/GovernanceFramework.ts
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,13 @@ | ||
import type { FeatureOptions } from './Feature' | ||
|
||
import { Feature } from './Feature' | ||
|
||
export type GovernanceFrameworkOptions = Omit<FeatureOptions, 'type'> | ||
|
||
export class GovernanceFramework extends Feature { | ||
public constructor(props: GovernanceFrameworkOptions) { | ||
super({ ...props, type: GovernanceFramework.type }) | ||
} | ||
|
||
public static readonly type = 'gov-fw' | ||
} |
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,25 @@ | ||
import type { FeatureOptions } from './Feature' | ||
|
||
import { IsOptional, IsString } from 'class-validator' | ||
|
||
import { Feature } from './Feature' | ||
|
||
export interface ProtocolOptions extends Omit<FeatureOptions, 'type'> { | ||
roles?: string[] | ||
} | ||
|
||
export class Protocol extends Feature { | ||
public constructor(props: ProtocolOptions) { | ||
super({ ...props, type: Protocol.type }) | ||
|
||
if (props) { | ||
this.roles = props.roles | ||
} | ||
} | ||
|
||
public static readonly type = 'protocol' | ||
|
||
@IsString({ each: true }) | ||
@IsOptional() | ||
public roles?: string[] | ||
} |
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,5 @@ | ||
export * from './Feature' | ||
export * from './FeatureQuery' | ||
export * from './GoalCode' | ||
export * from './GovernanceFramework' | ||
export * from './Protocol' |
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,2 @@ | ||
export * from './features' | ||
export * from './InboundMessageContext' |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If feature registry is part of core, I think it should not live in the discover features module.