• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14require 'ostruct'
15require 'delegate'
16
17module Common
18  module_function
19
20  def plugins
21    @plugins
22  end
23
24  def assign_data_level(cur_hash, key, cur_data)
25    if !cur_data && (cur_data.class == OpenStruct || cur_data.class == Array)
26      return
27    elsif !cur_data
28      #boolean ?
29      cur_data = "false"
30    end
31
32    if cur_data.class == OpenStruct
33      wrap_hash_level(cur_hash, key, cur_data)
34    elsif cur_data.class == Array
35      wrap_array_level(cur_hash, key, cur_data)
36    else
37      cur_hash[key] = cur_data.to_s
38    end
39  end
40
41  def wrap_array_level(cur_hash, key, cur_data)
42    if !cur_data || cur_data.class != Array
43      return
44    end
45
46    cur_data.each do |val|
47      assign_data_level(cur_hash, key, val)
48    end
49  end
50
51  def wrap_hash_level(cur_hash, key, cur_data)
52    if !cur_data || cur_data.class != OpenStruct
53        return
54    end
55
56    if !cur_hash[key]
57      cur_hash[key] = Hash.new()
58    end
59
60    new_h = cur_data.to_h
61
62    new_h.each do |sub_key, val|
63      assign_data_level(cur_hash[key.to_s], sub_key.to_s, val)
64    end
65  end
66
67  def wrap_data(data)
68    @data = data
69    @plugins = Hash.new()
70    if !data || !data.plugins
71      return
72    end
73
74    data.plugins.each do |plugin|
75      h_plugin = plugin.to_h
76      h_plugin_lang = h_plugin.keys.first.to_s
77      lang_enum = h_plugin_lang == "JAVA" ? "JAVA_8" : h_plugin_lang
78      lang_enum = "panda::panda_file::SourceLang::" + lang_enum
79      @plugins[h_plugin_lang] = Hash.new()
80      @plugins[h_plugin_lang]["lang_enum"] = lang_enum
81      assign_data_level(@plugins, h_plugin_lang, h_plugin.values.first)
82    end
83  end
84end
85
86def Gen.on_require(data)
87  Common.wrap_data(data)
88end
89