Skip to content

Commit

Permalink
Added overload for properties-only shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBoyBarney committed Feb 17, 2025
1 parent e535933 commit cffab77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
15 changes: 13 additions & 2 deletions spec/kdl/builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe KDL::Builder do
KDL
end

it "builds a node with a single argument" do
it "builds a node with a single shorthand argument" do
doc = KDL.build do |kdl|
kdl.node "snorlax" do
kdl.node "size", 10_i64
Expand All @@ -69,7 +69,18 @@ describe KDL::Builder do
KDL
end

it "builds a node with multiple arguments and properties" do
it "builds a node with shorthand properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", pokemon_type: "normal", level: 10_i64
end

doc.to_s.should eq <<-KDL
pokemon pokemon_type=normal level=10
KDL
end

it "builds a node with multiple shorthand arguments and properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", "snorlax", "jigglypuff", pokemon_type: "normal", level: 10_i64
end
Expand Down
17 changes: 15 additions & 2 deletions src/kdl/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,26 @@ module KDL
end
end

def node(name : String, *arguments, type : String? = nil, comment : String? = nil)
def node(name : String, *, type : String? = nil, comment : String? = nil)
node name, type: type, comment: comment do
end
end

# Bug: https://github.com/crystal-lang/crystal/issues/15484, separate overload needed for splats
def node(name : String, *arguments : KDL::Value::Type, type : String? = nil, comment : String? = nil)
node name, type: type, comment: comment do
arguments.each &->arg(KDL::Value::Type)
end
end

# Bug: https://github.com/crystal-lang/crystal/issues/15484, separate overload needed for double splat
# :ditto:
def node(name : String, *, type : String? = nil, comment : String? = nil, **properties : KDL::Value::Type)
node name, type: type, comment: comment do
properties.each &->prop(Symbol, KDL::Value::Type)
end
end

# :ditto:
def node(name : String, *arguments : KDL::Value::Type, type : String? = nil, comment : String? = nil, **properties : KDL::Value::Type)
node name, type: type, comment: comment do
arguments.each &->arg(KDL::Value::Type)
Expand Down

0 comments on commit cffab77

Please sign in to comment.