Skip to content

Commit

Permalink
Made shorthand properties optinally positional
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBoyBarney committed Feb 18, 2025
1 parent 012e445 commit 5ec7d21
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
81 changes: 57 additions & 24 deletions spec/kdl/builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,43 +52,76 @@ describe KDL::Builder do
KDL
end

it "builds a node with a single shorthand argument" do
doc = KDL.build do |kdl|
kdl.node "snorlax" do
kdl.node "size", 10_i64
kdl.node "state", "asleep"
describe "shorthand" do
it "builds a node with a single argument" do
doc = KDL.build do |kdl|
kdl.node "snorlax" do
kdl.node "size", 10_i64
end
end

doc.to_s.should eq <<-KDL
snorlax {
size 10
}
KDL
end

doc.to_s.should eq <<-KDL
snorlax {
size 10
state asleep
}
it "builds a node with shorthand properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", properties: {"pokemon_type" => "normal", "level" => 10_i64}
end

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

it "builds a node with shorthand properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", properties: {"pokemon_type" => "normal", "level" => 10_i64}
it "builds a node with positional 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

doc.to_s.should eq <<-KDL
pokemon pokemon_type=normal level=10
it "builds a node with positional properties and arguments" do
doc = KDL.build do |kdl|
kdl.node "pokemon", "snorlax", "jigglypuff", {"Pokemon type" => "normal", "Level" => 10_i64}
end

KDL
end
doc.to_s.should eq <<-KDL
pokemon snorlax jigglypuff "Pokemon type"=normal Level=10
it "builds a node with multiple shorthand arguments and properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", "snorlax", "jigglypuff", properties: {"Pokemon type" => "normal", "Level" => 10_i64}
KDL
end

doc.to_s.should eq <<-KDL
pokemon snorlax jigglypuff "Pokemon type"=normal Level=10
it "builds a node with mixed order positional properties and arguments" do
doc = KDL.build do |kdl|
kdl.node "pokemon", "snorlax", {"Pokemon type" => "normal"}, "jigglypuff", {"Level" => 10_i64}
end

KDL
doc.to_s.should eq <<-KDL
pokemon snorlax jigglypuff "Pokemon type"=normal Level=10
KDL
end

it "builds a node with positional arguments, and named properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", "snorlax", "jigglypuff", properties: {"Trainer" => "Sylphrena"}
end

doc.to_s.should eq <<-KDL
pokemon snorlax jigglypuff Trainer=Sylphrena
KDL
end
end
end
end
22 changes: 16 additions & 6 deletions src/kdl/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,31 @@ module KDL
node(name, type: type, comment: comment) { }
end

# Name and shorthand arguments
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)
# Name andshorthand positional arguments + properties
def node(name : String, *positional : KDL::Value::Type | Hash(String, KDL::Value::Type), type : String? = nil, comment : String? = nil)
node(name, type: type, comment: comment) do
positional.each do |argument|
case argument
in Hash
argument.each do |key, value|
raise ArgumentError.new "Invalid hash key or value" unless key.is_a? String && value.is_a? KDL::Value::Type
prop key, value
end
in KDL::Value::Type
arg argument
end
end
end
end

# Name and shorthand properties
# Name and shorthand named properties
def node(name : String, *, type : String? = nil, comment : String? = nil, properties : Hash(String, KDL::Value::Type))
node name, type: type, comment: comment do
properties.each &->prop(String, KDL::Value::Type)
end
end

# Name and shorthand arguments + properties
# Name and shorthand positional arguments + named properties
def node(name : String, *arguments : KDL::Value::Type, type : String? = nil, comment : String? = nil, properties : Hash(String, KDL::Value::Type))
node name, type: type, comment: comment do
arguments.each &->arg(KDL::Value::Type)
Expand Down

0 comments on commit 5ec7d21

Please sign in to comment.