Skip to content

Commit

Permalink
Move cedar_common/corks to main repo
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Mar 4, 2024
1 parent 05924da commit 072078c
Show file tree
Hide file tree
Showing 98 changed files with 18,132 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/cedar_common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
3 changes: 3 additions & 0 deletions packages/cedar_common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

- Initial version.
5 changes: 5 additions & 0 deletions packages/cedar_common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# cedar_common

Base types and utilities of the Cedar language in Dart.

This is separate from `package:cedar_dart` so that the types can be used without bundling the native assets of `package:cedar_dart`.
30 changes: 30 additions & 0 deletions packages/cedar_common/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
13 changes: 13 additions & 0 deletions packages/cedar_common/lib/cedar_common.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// Base types and utilities of the Cedar language in Dart.
///
/// This is separate from `package:cedar_dart` so that the types can be used
/// without bundling the native assets of `package:cedar_dart`.
library;

export 'src/ast/cedar_entity.dart';
export 'src/ast/cedar_entity_id.dart';
export 'src/ast/cedar_node.dart';
export 'src/ast/cedar_schema.dart';
export 'src/policy/cedar_policy.dart';
export 'src/policy/cedar_policy_set.dart';
export 'src/policy/json_expr.dart';
32 changes: 32 additions & 0 deletions packages/cedar_common/lib/src/ast/cedar_entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:cedar_common/src/ast/cedar_entity_id.dart';
import 'package:cedar_common/src/ast/cedar_node.dart';
import 'package:cedar_common/src/policy/json_expr.dart';

final class CedarEntity implements CedarNode {
const CedarEntity({
required this.id,
this.parents = const [],
this.attributes = const {},
});

factory CedarEntity.fromJson(Map<String, Object?> json) => CedarEntity(
id: CedarEntityId.fromJson(json['uid'] as Map<String, Object?>),
parents: (json['parents'] as List<Object?>)
.map((e) => CedarEntityId.fromJson(e as Map<String, Object?>))
.toList(),
attributes: (json['attrs'] as Map<Object?, Object?>)
.cast<String, Object?>()
.map((key, value) => MapEntry(key, CedarValueJson.fromJson(value))),
);

final CedarEntityId id;
final List<CedarEntityId> parents;
final Map<String, CedarValueJson> attributes;

@override
Map<String, Object?> toJson() => {
'uid': id.toJson(),
'parents': parents.map((e) => e.toJson()).toList(),
'attrs': attributes.map((key, value) => MapEntry(key, value.toJson())),
};
}
64 changes: 64 additions & 0 deletions packages/cedar_common/lib/src/ast/cedar_entity_id.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:cedar_common/src/ast/cedar_node.dart';

final class CedarEntityId implements CedarNode {
const CedarEntityId(this.type, this.id);

factory CedarEntityId.fromJson(Map<String, Object?> json) {
switch (json) {
case {'type': final String type, 'id': final String id} ||
{'__entity': {'type': final String type, 'id': final String id}}:
return CedarEntityId(type, id);
default:
throw FormatException('Invalid entity ID JSON: $json');
}
}

final String type;
final String id;

/// Returns a normalized version of this entity ID.
///
/// Cedar prohibits whitespace in entity IDs, so this method removes all
/// whitespace from the [type] and [id].
///
/// See [RFC 9](https://github.com/cedar-policy/rfcs/blob/main/text/0009-disallow-whitespace-in-entityuid.md)
/// for more information.
CedarEntityId get normalized => CedarEntityId(
type,
String.fromCharCodes(
id.runes.expand((char) {
return switch (char) {
0 => '\\0'.codeUnits,
0x9 => '\\t'.codeUnits,
0xa => '\\n'.codeUnits,
0xd => '\\r'.codeUnits,
0x22 => '\\"'.codeUnits,
0x27 => "\\'".codeUnits,
< 0x20 ||
0x7f || // Delete
0x96 || // Non-breaking space
> 0xffff =>
'\\u{${char.toRadixString(16)}}'.codeUnits,
_ => [char],
};
}),
),
);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is CedarEntityId && type == other.type && id == other.id;

@override
int get hashCode => Object.hash(type, id);

@override
String toString() => '$type::"$id"';

@override
Map<String, Object?> toJson() => {
'type': type,
'id': id,
};
}
3 changes: 3 additions & 0 deletions packages/cedar_common/lib/src/ast/cedar_node.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract interface class CedarNode {
Map<String, Object?> toJson();
}
Loading

0 comments on commit 072078c

Please sign in to comment.