Class: Quartz::GoStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/quartz/go_struct.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(struct_name, method_info, process) ⇒ GoStruct

Returns a new instance of GoStruct.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/quartz/go_struct.rb', line 5

def initialize(struct_name, method_info, process)
  @struct_name = struct_name
  @method_name_to_arg_info = {}
  @process = process

  method_info["NameToMethodMetadata"].each do |method_name, info|
    @method_name_to_arg_info[method_name] = info["ArgumentToType"].keys()
  end

  @struct_methods = @method_name_to_arg_info.keys
end

Instance Attribute Details

#struct_methodsObject (readonly)

Returns the value of attribute struct_methods.



3
4
5
# File 'lib/quartz/go_struct.rb', line 3

def struct_methods
  @struct_methods
end

#struct_nameObject (readonly)

Returns the value of attribute struct_name.



3
4
5
# File 'lib/quartz/go_struct.rb', line 3

def struct_name
  @struct_name
end

Instance Method Details

#call(method_name, args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quartz/go_struct.rb', line 17

def call(method_name, args = {})
  unless @struct_methods.include?(method_name)
    raise Quartz::ArgumentError, "Invalid method: #{method_name}"
  end

  arg_info = @method_name_to_arg_info[method_name]

  # Validate arguments
  args.each do |k, v|
    unless arg_info.include?(k)
      raise Quartz::ArgumentError, "Invalid argument: #{k}"
    end

    # TODO: validate types
  end

  response = @process.call(@struct_name, method_name, args)

  if response['error']
    raise Quartz::ResponseError.new(response['error'])
  end

  response['result']
end