Class: GoonModelGen::Golang::StructsLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/goon_model_gen/golang/structs_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ StructsLoader

Returns a new instance of StructsLoader.

Parameters:



18
19
20
# File 'lib/goon_model_gen/golang/structs_loader.rb', line 18

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/goon_model_gen/golang/structs_loader.rb', line 15

def config
  @config
end

Instance Method Details

#base_type_hash_from(type_hash) ⇒ Object



69
70
71
# File 'lib/goon_model_gen/golang/structs_loader.rb', line 69

def base_type_hash_from(type_hash)
  type_hash['Elem'].nil? ? type_hash : base_type_name_from(type_hash['Elem'])
end

#build_packages(types) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/goon_model_gen/golang/structs_loader.rb', line 46

def build_packages(types)
  Packages.new.tap do |pkgs|
    types.each do |type_hash|
      next if type_hash['PkgPath'].blank? || type_hash['Name'].blank?
      pkg = pkgs.find_or_new(type_hash['PkgPath'])
      if type_hash['Fields']
        pkg.new_struct(type_hash['Name']).tap do |s|
          type_hash['Fields'].each do |f|
            t = f['Type']
            tags = (f['Tag'] || {}).each_with_object({}){|(k,v), d| d[k] = v.split(',')}
            s.new_field(f['Name'], t['Representation'], tags)
          end
        end
      elsif type_hash['Kind'] == 'slice'
        base_type_hash = base_type_hash_from(type_hash['Elem'])
        pkg.new_named_slice(type_hash['Name'], base_type_hash['Name'], base_type_hash['PkgPath'])
      else
        pkg.new_enum(type_hash['Name'], type_hash['Kind'])
      end
    end
  end
end

#processHash<String,Packages>

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/goon_model_gen/golang/structs_loader.rb', line 23

def process
  path = config.structs_json_path

  erb = ERB.new(::File.read(path), nil, "-")
  erb.filename = path
  txt = erb.result
  raw = YAML.load(txt)

  r = raw.each_with_object({}) do |(key, types), d|
    d[key] = build_packages(types)
  end

  whole_packages = Golang::DatastorePackageFactory.new(config.package_alias_map).packages.dup
  r.values.each do |pkgs|
    whole_packages.add(*pkgs)
  end

  r.values.each do |pkgs|
    pkgs.resolve_type_names(whole_packages)
  end
  return r
end