Class: GoonModelGen::Builder::ModelBuilder

Inherits:
AbstractBuilder show all
Defined in:
lib/goon_model_gen/builder/model_builder.rb

Instance Attribute Summary

Attributes inherited from AbstractBuilder

#base_package_path, #config, #package_suffix

Instance Method Summary collapse

Methods inherited from AbstractBuilder

#build, #build_sentences, #build_sentences_with, #method_to_template_for, #method_to_template_map, #templates_for

Constructor Details

#initialize(config) ⇒ ModelBuilder

Returns a new instance of ModelBuilder.

Parameters:



21
22
23
# File 'lib/goon_model_gen/builder/model_builder.rb', line 21

def initialize(config)
  super(config, config.model_package_path)
end

Instance Method Details

#build_package(package_path, types) ⇒ Golang::Package, Array<Proc>

Parameters:

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/goon_model_gen/builder/model_builder.rb', line 28

def build_package(package_path, types)
  procs = []
  pkg = Golang::Package.new(package_path).tap do |pkg|
    types.each do |t|
      case t
      when Source::Struct then
        go_type = build_struct(t, pkg)
        kind = (t.id_name && t.id_type) ? 'goon' : 'struct'
        procs << Proc.new{ build_sentences('model', kind, t, go_type) }
      when Source::Enum then
        go_type = pkg.new_enum(t.name, t.base_type).tap do |enum|
          t.elements.each{|i| enum.add(i.value, i.name) }
        end
        procs << Proc.new{ build_sentences('model', 'enum', t, go_type) }
      when Source::NamedSlice then
        go_type = pkg.new_named_slice(t.name, t.base_type_name)
        procs << Proc.new{ build_sentences('model', 'slice', t, go_type) }
      else
        raise "Unsupported type #{t.class.name} #{t.inspect}"
      end
    end
  end
  return pkg, procs
end

#build_struct(t, pkg) ⇒ Golang::Struct

Parameters:

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/goon_model_gen/builder/model_builder.rb', line 61

def build_struct(t, pkg)
  pkg.new_struct(t.name).tap do |s|
    s.ref_name = t.ref_name
    if t.id_name && t.id_type
      tags = {
        'goon' => ['id'],
        'datastore' => ['-'],
        'json' => [t.id_name.underscore],
      }
      s.new_field(t.id_name, t.id_type, tags, goon_id: true)
    end
    t.fields.each do |f|
      s.new_field(f.name, f.type_name, f.build_tags).tap do |field|
        field.unique = f.unique
      end
    end
  end
end

#resolve_type_names(pkgs) ⇒ Object

Parameters:



54
55
56
# File 'lib/goon_model_gen/builder/model_builder.rb', line 54

def resolve_type_names(pkgs)
  pkgs.resolve_type_names(Golang::DatastorePackageFactory.new(config.package_alias_map).packages)
end