Skip to content

Commit

Permalink
feat: object type singleton syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkhan committed Aug 18, 2024
1 parent e9a9278 commit 11b7b09
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ module Anchor::Types
end
```

#### `Anchor::Types::Object`

```rb
class CustomPayload < Anchor::Types::Object
property :id, Anchor::Types::String, optional: true, description: "ID of payload."
end
```

#### `Anchor::Types::Enum`

```rb
Expand Down
2 changes: 1 addition & 1 deletion lib/anchor/json_schema/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def type_property(type)
when Anchor::Types::Array then { type: "array", items: type_property(type.type) }
when Anchor::Types::Literal then { enum: [type.value] }
when Anchor::Types::Reference then { "$ref" => "#/$defs/#{type.name}" }
when Anchor::Types::Object then serialize_object(type)
when Anchor::Types::Object, Anchor::Types::Object.singleton_class then serialize_object(type)
when Anchor::Types::Enum.singleton_class then { enum: type.values.map(&:second) }
when Anchor::Types::Unknown.singleton_class then {}
else raise RuntimeError
Expand Down
2 changes: 1 addition & 1 deletion lib/anchor/type_script/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def type_string(type, depth = 1)
when Anchor::Types::Array then "Array<#{type_string(type.type, depth)}>"
when Anchor::Types::Literal then serialize_literal(type.value)
when Anchor::Types::Reference then type.name
when Anchor::Types::Object then serialize_object(type, depth)
when Anchor::Types::Object, Anchor::Types::Object.singleton_class then serialize_object(type, depth)
when Anchor::Types::Enum.singleton_class then type.anchor_schema_name
when Anchor::Types::Unknown.singleton_class then "unknown"
else raise RuntimeError
Expand Down
17 changes: 16 additions & 1 deletion lib/anchor/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@ class Unknown; end
Literal = Struct.new(:value)
Union = Struct.new(:types)
Reference = Struct.new(:name)
Object = Struct.new(:properties)
Property = Struct.new(:name, :type, :optional, :description)
class Object
attr_reader :properties

def initialize(properties)
@properties = properties
end

class << self
attr_reader :properties

def property(name, type, optional: nil, description: nil)
@properties ||= []
@properties.push(Property.new(name, type, optional, description))
end
end
end

Relationship = Struct.new(:resource, :resources, :null, :null_elements, keyword_init: true)

Expand Down
16 changes: 8 additions & 8 deletions spec/example/app/resources/exhaustive_resource.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class ExhaustiveResource < ApplicationResource
object = Types::Object.new([
Types::Property.new(:a, Types::Literal.new("a")),
Types::Property.new("b-dash", Types::Literal.new(1)),
Types::Property.new(:c, Types::Maybe.new(Types::String)),
Types::Property.new(:d_optional, Types::Maybe.new(Types::String), true),
])
class AssertedObject < Types::Object
property :a, Types::Literal.new("a")
property "b-dash", Types::Literal.new(1)
property :c, Types::Maybe.new(Types::String)
property :d_optional, Types::Maybe.new(Types::String), optional: true
end

attribute :asserted_string, Types::String, description: "My asserted string."
attribute :asserted_number, Types::Integer
attribute :asserted_boolean, Types::Boolean
attribute :asserted_null, Types::Null
attribute :asserted_unknown, Types::Unknown
attribute :asserted_object, object
attribute :asserted_maybe_object, Types::Maybe.new(object)
attribute :asserted_object, AssertedObject
attribute :asserted_maybe_object, Types::Maybe.new(AssertedObject)
attribute :asserted_array_record, Types::Array.new(Types::Record.new(Types::Integer))
attribute :asserted_union, Types::Union.new([Types::String, Types::Float])
attribute :inferred_unknown
Expand Down

0 comments on commit 11b7b09

Please sign in to comment.