Commit 74cee0f5 authored by Laurent Briais's avatar Laurent Briais

Unified parsing of entity_id within `EntitySearchReference`

parent fe0ed89d
......@@ -21,6 +21,7 @@ require 'power_stencil/utils/gem_utils'
require 'power_stencil/utils/file_edit'
require 'power_stencil/utils/graphviz'
require 'power_stencil/utils/completion'
require 'power_stencil/utils/entity_helper'
require 'climatic/script/unimplemented_processor'
require 'power_stencil/project/proxy'
......
......@@ -19,9 +19,9 @@ module PowerStencil
if md = property_def.match(/^\s*(?<prop_name>[^:=]+)\s*[:=]\s*(?<prop_val>.*)\s*$/)
property_value = md['prop_val']
logger.debug "Using extra properties from command line '#{md['prop_name']}' = '#{property_value}'"
if pmd = property_value.match(/^\s*!entity\s+(?<entity_type>[^\/]+)\/(?<entity_name>.*)\s*$/)
type = pmd['entity_type'].to_sym
name = pmd['entity_name']
esr = PowerStencil::Utils::EntityHelper::EntitySearchReference.from_string property_value
if esr
type, name = esr.to_a
referenced_entity = project.engine.entity(type, name)
property_value = referenced_entity.to_reference unless referenced_entity.nil?
puts_and_logs "Could not find entity '#{type.to_s}/#{name}' !", logs_as: :error, check_verbose: false if referenced_entity.nil?
......
......@@ -3,11 +3,7 @@ module PowerStencil
module EntityHelper
EntitySearchReference = Struct.new(:type, :name) do
def as_path
[type, name].map(&:to_s).join '/'
end
end
include PowerStencil::Utils::EntityHelper
def describe_entity_types(entity_types)
report = {}
......@@ -132,9 +128,9 @@ module PowerStencil
criteria = stack
else
stack.each do |param|
if md = param.match(/^\s*(?<type>[^\/]+)\/(?<name>.+)\s*$/)
next_entity_reference.type = md[:type].to_sym
next_entity_reference.name = md[:name]
esr = EntitySearchReference.from_string param
if esr
esr.fill next_entity_reference
else
if next_entity_reference.type.nil? or next_entity_reference.type.empty?
next_entity_reference.type = param.to_sym
......
......@@ -19,9 +19,9 @@ module PowerStencil
def entity(type_or_path, name = nil)
type, name = if name.nil?
md = type_or_path.match /^(?<type>[^\/]+)\/(?<name>.*)$/
if md
[md['type'].to_sym, md['name']]
esr = PowerStencil::Utils::EntityHelper::EntitySearchReference.from_string type_or_path
if esr
esr.to_a
else
raise PowerStencil::Error, "Invalid entity id: '#{type_or_path}'"
end
......
module PowerStencil
module Utils
module EntityHelper
EntitySearchReference = Struct.new(:type, :name) do
def self.from_string(string)
if md = string.match(/^\s*(?<type>[^\/]+)\/\s*(?<name>.+)\s*$/)
return new md[:type].to_sym, md[:name]
end
false
end
def as_path
to_a.map(&:to_s).join '/'
end
def to_a
[type, name]
end
def fill(something)
something.type = type
something.name = name
end
end
end
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment