Class: Fluent::BigQuery::FieldSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/bigquery/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, mode = :nullable) ⇒ FieldSchema

Returns a new instance of FieldSchema.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fluent/plugin/bigquery/schema.rb', line 6

def initialize(name, mode = :nullable)
  unless [:nullable, :required, :repeated].include?(mode)
    raise ConfigError, "Unrecognized mode for #{name}: #{mode}"
  end
  ### https://developers.google.com/bigquery/docs/tables
  # Each field has the following properties:
  #
  # name - The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_),
  #        and must start with a letter or underscore. The maximum length is 128 characters.
  #        https://cloud.google.com/bigquery/docs/reference/v2/tables#schema.fields.name
  unless name =~ /^[_A-Za-z][_A-Za-z0-9]{,127}$/
    raise ConfigError, "invalid bigquery field name: '#{name}'"
  end

  @name = name
  @mode = mode
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



24
25
26
# File 'lib/fluent/plugin/bigquery/schema.rb', line 24

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/fluent/plugin/bigquery/schema.rb', line 24

def name
  @name
end

Instance Method Details

#format(value, is_load: false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/plugin/bigquery/schema.rb', line 26

def format(value, is_load: false)
  case @mode
  when :nullable
    format_one(value, is_load: is_load) unless value.nil?
  when :required
    if value.nil?
      log.warn "Required field #{name} cannot be null"
      nil
    else
      format_one(value, is_load: is_load)
    end
  when :repeated
    value.nil? ? [] : value.each_with_object([]) { |v, arr| arr << format_one(v, is_load: true) if v }
  end
end

#format_one(value, is_load: false) ⇒ Object

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/fluent/plugin/bigquery/schema.rb', line 42

def format_one(value, is_load: false)
  raise NotImplementedError, "Must implement in a subclass"
end

#to_hObject



46
47
48
49
50
51
52
# File 'lib/fluent/plugin/bigquery/schema.rb', line 46

def to_h
  {
    :name => name,
    :type => type.to_s.upcase,
    :mode => mode.to_s.upcase,
  }
end