Module: Ruby::Rego::Builtins::Uri
- Extended by:
- RegistryHelpers
- Defined in:
- lib/ruby/rego/builtins/uri.rb,
lib/ruby/rego/builtins/uri/parser.rb,
lib/ruby/rego/builtins/uri/parser/host.rb,
lib/ruby/rego/builtins/uri/parser/escaping.rb,
lib/ruby/rego/builtins/uri/parser/serialize.rb
Overview
URI builtins (uri.parse, uri.is_valid), matching OPA’s topdown/uri.go — thin wrappers over Go’s net/url.Parse (ported in uri/parser.rb).
uri.parse returns an object with only the fields OPA exposes — scheme, hostname, port, path, raw_path, raw_query, fragment — each present only when non-empty (raw_path is present whenever path is, as RawPath-or-Path). userinfo and the opaque component are dropped. A string that net/url.Parse rejects is undefined.
uri.is_valid is false for a non-string or the empty string (OPA rejects “” deliberately, though RFC 3986 permits it), and otherwise reports whether net/url.Parse succeeds.
Defined Under Namespace
Modules: Parser
Constant Summary collapse
- URI_FUNCTIONS =
{ "uri.parse" => { arity: 1, handler: :parse }, "uri.is_valid" => { arity: 1, handler: :is_valid } }.freeze
Class Method Summary collapse
-
.is_valid(value) ⇒ Ruby::Rego::BooleanValue
:reek:NilCheck – nil is Parser.parse’s error sentinel; valid iff it returns Components.
-
.parse(value) ⇒ Hash, Ruby::Rego::UndefinedValue
:reek:NilCheck – nil is Parser.parse’s error sentinel (Go’s url.Parse failure -> undefined).
-
.register! ⇒ Ruby::Rego::Builtins::BuiltinRegistry
Methods included from RegistryHelpers
Class Method Details
.is_valid(value) ⇒ Ruby::Rego::BooleanValue
:reek:NilCheck – nil is Parser.parse’s error sentinel; valid iff it returns Components.
55 56 57 58 59 60 |
# File 'lib/ruby/rego/builtins/uri.rb', line 55 def self.is_valid(value) return BooleanValue.new(false) unless value.is_a?(StringValue) string = value.value BooleanValue.new(parseable_encoding?(string) && !string.empty? && !Parser.parse(string).nil?) end |
.parse(value) ⇒ Hash, Ruby::Rego::UndefinedValue
:reek:NilCheck – nil is Parser.parse’s error sentinel (Go’s url.Parse failure -> undefined). :reek:TooManyStatements – linear: type guard, encoding guard, parse, shape result.
43 44 45 46 47 48 49 50 |
# File 'lib/ruby/rego/builtins/uri.rb', line 43 def self.parse(value) Base.assert_type(value, expected: StringValue, context: "uri.parse") string = value.value return UndefinedValue.new unless parseable_encoding?(string) components = Parser.parse(string) components.nil? ? UndefinedValue.new : uri_object(components) end |
.register! ⇒ Ruby::Rego::Builtins::BuiltinRegistry
31 32 33 34 35 |
# File 'lib/ruby/rego/builtins/uri.rb', line 31 def self.register! registry = BuiltinRegistry.instance register_configured_functions(registry, URI_FUNCTIONS) registry end |