Skip to content
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

Support nilable string properties. #160

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ecr/property.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@
<%= convert_to_crystal("value", prop.type_info, [] of ArgInfo, prop.ownership_transfer) %><% if prop_is_object? -%> unless value.null?<% end %>
end
<% end %>

<% if prop_type_name == "::String" %>
<% if prop.flags.writable? -%>
# Set `#<%= prop_getter_method_name %>` property to nil.
def <%= to_call(prop.name) %>=(value : Nil) : Nil
LibGObject.g_object_set(self, "<%= prop.name %>", Pointer(Void).null, Pointer(Void).null)
end
<% end %>

<% if prop.flags.readable? -%>
# Same as `#<%= prop_getter_method_name %>`, but can return nil.
def <%= prop_getter_method_name %>? : ::String?
value = uninitialized Pointer(LibC::Char)
LibGObject.g_object_get(self, "<%= prop.name %>", pointerof(value), Pointer(Void).null)
::String.new(value) if value
end
<% end %>
<% end %>
7 changes: 7 additions & 0 deletions spec/properties_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ describe "GObject properties" do
subject = Test::Subject.new
subject.string = "hey ho"
subject.string.should eq("hey ho")
subject.string?.should eq("hey ho")
end

it "can be nullable strings" do
subject = Test::Subject.new
subject.string = nil
subject.string?.should eq(nil)
end

it "can be boolean" do
Expand Down