bq_guess 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86c281546032275e8328f83d05b3ac0921cf6617
4
+ data.tar.gz: f5b00c5f7bc2b2d1ad8d85d93ec4b241a169cacd
5
+ SHA512:
6
+ metadata.gz: 8b8330c80e83918c30fb87572ac632a7d549ad295dfd938782817b97b4bb2f6ea89c1961e3e8cc1f355286d7573561088941fb4b45b17a23b22e92d456956830
7
+ data.tar.gz: 1836e2970824b2bd0e25a08010f71167dad01e14ec2e8b98ba1c1e05d0fec0e0a98b4750136726f3801f1451ba1fa80465ed2b74e55ba6ec707411547f58496a
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,87 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude: []
4
+ DisplayCopNames: true
5
+
6
+ Documentation:
7
+ Enabled: false
8
+
9
+ ######## Lint ########
10
+
11
+ Lint/AssignmentInCondition:
12
+ Enabled: false
13
+
14
+ Lint/HandleExceptions:
15
+ Enabled: false
16
+
17
+ Lint/UnderscorePrefixedVariableName:
18
+ Enabled: false
19
+
20
+ Lint/UnusedBlockArgument:
21
+ Enabled: false
22
+
23
+ Lint/UnusedMethodArgument:
24
+ Enabled: false
25
+
26
+ ######## Metrics ########
27
+
28
+ Metrics/AbcSize:
29
+ Max: 20
30
+
31
+ Metrics/CyclomaticComplexity:
32
+ Max: 10
33
+
34
+ Metrics/LineLength:
35
+ Max: 100
36
+
37
+ Metrics/MethodLength:
38
+ Max: 20
39
+
40
+ Metrics/PerceivedComplexity:
41
+ Max: 10
42
+
43
+ ######## Style ########
44
+
45
+ Style/AccessorMethodName:
46
+ Enabled: false
47
+
48
+ Style/Documentation:
49
+ Enabled: false
50
+
51
+ Style/DoubleNegation:
52
+ Enabled: false
53
+
54
+ Style/FormatString:
55
+ EnforcedStyle: percent
56
+
57
+ Style/GuardClause:
58
+ MinBodyLength: 4
59
+
60
+ Style/IfUnlessModifier:
61
+ Enabled: false
62
+
63
+ Style/MultilineBlockChain:
64
+ Enabled: false
65
+
66
+ Style/NumericLiterals:
67
+ MinDigits: 7
68
+
69
+ Style/PredicateName:
70
+ NamePrefixBlacklist:
71
+ - "is_"
72
+ - "have_"
73
+
74
+ Style/SignalException:
75
+ EnforcedStyle: only_raise
76
+
77
+ Style/SingleLineBlockParams:
78
+ Enabled: false
79
+
80
+ Style/SpaceAroundOperators:
81
+ AllowForAlignment: true
82
+
83
+ Style/StringLiterals:
84
+ EnforcedStyle: double_quotes
85
+
86
+ Style/TrailingCommaInLiteral:
87
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # Specify your gem's dependencies in bq_guess.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # BqGuess
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bq_guess`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bq_guess'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bq_guess
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bq_guess.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bq_guess"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bq_guess.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "bq_guess/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "bq_guess"
9
+ spec.version = BqGuess::VERSION
10
+ spec.authors = ["nownabe"]
11
+ spec.email = ["nownabe@gmail.com"]
12
+
13
+ spec.summary = "Guess BigQuery schema"
14
+ spec.homepage = "https://github.com/nownabe/bq_guess"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "rubocop"
28
+ end
data/exe/bq_guess ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
+ require "bq_guess/cli"
6
+
7
+ BqGuess::Cli.new(ARGV).execute
data/lib/bq_guess.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ require "bq_guess/version"
3
+
4
+ module BqGuess
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "bq_guess/guessers/json_lines"
5
+
6
+ module BqGuess
7
+ class Cli
8
+ attr_reader :options
9
+
10
+ def initialize(args = [])
11
+ @options = parse_option(args)
12
+ end
13
+
14
+ def execute
15
+ result =
16
+ Guessers::JsonLines.new(
17
+ File.read(File.expand_path(options[:input_path]))
18
+ ).guess.as_schema
19
+ puts JSON.pretty_generate(result)
20
+ end
21
+
22
+ private
23
+
24
+ # TODO: ignore error line
25
+ # TODO: default nullable instead of required
26
+ def parse_option(args)
27
+ if args.include?("-h") || args.include?("--help")
28
+ puts "usage: bq_guess input_file"
29
+ exit
30
+ elsif args.include?("-v") || args.include?("--version")
31
+ require "bq_guess/version"
32
+ puts BqGuess::VERSION
33
+ exit
34
+ else
35
+ { input_path: args.first }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BqGuess
4
+ module Fields
5
+ end
6
+ end
7
+
8
+ require "bq_guess/fields/base"
9
+ require "bq_guess/fields/boolean"
10
+ require "bq_guess/fields/float"
11
+ require "bq_guess/fields/integer"
12
+ require "bq_guess/fields/null"
13
+ require "bq_guess/fields/record"
14
+ require "bq_guess/fields/string"
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BqGuess
4
+ module Fields
5
+ class Base
6
+ attr_reader :name, :mode, :type
7
+
8
+ def initialize(name)
9
+ @name = name
10
+ @type = self.class.to_s.split("::").last.downcase.to_sym
11
+ required!
12
+ end
13
+
14
+ def as_schema
15
+ {
16
+ name: name,
17
+ type: type.to_s.upcase,
18
+ mode: mode.to_s.upcase
19
+ }
20
+ end
21
+
22
+ def nullable!
23
+ @mode = :nullable
24
+ end
25
+
26
+ def nullable?
27
+ mode == :nullable
28
+ end
29
+
30
+ def repeated!
31
+ @mode = :repeated
32
+ end
33
+
34
+ def repeated?
35
+ mode == :repeated
36
+ end
37
+
38
+ def required!
39
+ @mode = :required
40
+ end
41
+
42
+ def required?
43
+ mode == :required
44
+ end
45
+
46
+ def to_hash
47
+ {
48
+ name: name,
49
+ type: type,
50
+ mode: mode
51
+ }
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields/base"
4
+
5
+ module BqGuess
6
+ module Fields
7
+ class Boolean < Base
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields/base"
4
+
5
+ module BqGuess
6
+ module Fields
7
+ class Float < Base
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields/base"
4
+
5
+ module BqGuess
6
+ module Fields
7
+ class Integer < Base
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields/base"
4
+
5
+ module BqGuess
6
+ module Fields
7
+ class Null < Base
8
+ def initialize(name)
9
+ super
10
+ @type = :string
11
+ nullable!
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields/base"
4
+
5
+ module BqGuess
6
+ module Fields
7
+ class Record < Base
8
+ attr_reader :fields
9
+
10
+ def initialize(name, fields)
11
+ super(name)
12
+ @fields = fields
13
+ end
14
+
15
+ def as_schema
16
+ super.merge(fields: fields.as_schema)
17
+ end
18
+
19
+ def to_hash
20
+ super.merge(fields: fields.to_a)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields/base"
4
+
5
+ module BqGuess
6
+ module Fields
7
+ class String < Base
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields"
4
+ require "bq_guess/schema"
5
+
6
+ module BqGuess
7
+ class Guesser
8
+ attr_reader :record
9
+
10
+ class << self
11
+ def guess_records(records)
12
+ records[1..-1].each_with_object(guess(records.first)) do |record, schema|
13
+ schema.merge!(guess(record))
14
+ end
15
+ end
16
+
17
+ def guess(record)
18
+ new(record).guess
19
+ end
20
+ end
21
+
22
+ def initialize(record)
23
+ @record = record
24
+ end
25
+
26
+ def guess
27
+ record.each_with_object(Schema.new) do |(key, value), schema|
28
+ schema[key] = guess_field(key, value)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def guess_field(key, value)
35
+ case value
36
+ when TrueClass, FalseClass
37
+ Fields::Boolean.new(key)
38
+ when Float
39
+ Fields::Float.new(key)
40
+ when Integer
41
+ Fields::Integer.new(key)
42
+ when NilClass
43
+ Fields::Null.new(key)
44
+ when Hash
45
+ Fields::Record.new(key, self.class.guess(value))
46
+ when String
47
+ Fields::String.new(key)
48
+ when Array
49
+ guess_field(key, value.first).tap(&:repeated!)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "bq_guess/guesser"
5
+ require "bq_guess/schema"
6
+
7
+ module BqGuess
8
+ module Guessers
9
+ class JsonLines
10
+ attr_reader :json_lines
11
+
12
+ def initialize(json_lines)
13
+ @json_lines = json_lines.lines
14
+ end
15
+
16
+ def guess
17
+ BqGuess::Guesser.guess_records(
18
+ json_lines.map { |l| JSON.parse(l) }
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bq_guess/fields"
4
+
5
+ module BqGuess
6
+ class Schema < Hash
7
+ def as_schema
8
+ values.map(&:as_schema)
9
+ end
10
+
11
+ def merge!(other)
12
+ _merge!(other)
13
+ set_nullable(other)
14
+ end
15
+
16
+ def null?(key)
17
+ !key?(key) || self[key].is_a?(Fields::Null)
18
+ end
19
+
20
+ def nullable!
21
+ each_value(&:nullable!)
22
+ end
23
+
24
+ def repeated?(key)
25
+ !!self[key]&.repeated?
26
+ end
27
+
28
+ def to_a
29
+ values.map(&:to_hash)
30
+ end
31
+
32
+ private
33
+
34
+ def _merge!(other)
35
+ each do |key, field|
36
+ if other.null?(key)
37
+ field.nullable! unless other.repeated?(key)
38
+ else
39
+ if field.is_a?(Fields::Null)
40
+ self[key] = other[key]
41
+ self[key].nullable! unless other.repeated?(key)
42
+ end
43
+ if field.is_a?(Fields::Record) && other[key].is_a?(Fields::Record)
44
+ self[key].fields.merge!(other[key].fields)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def set_nullable(other)
51
+ (other.keys - keys).each do |key|
52
+ self[key] = other[key]
53
+ self[key].nullable!
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module BqGuess
3
+ VERSION = "0.1.0"
4
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bq_guess
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nownabe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - nownabe@gmail.com
72
+ executables:
73
+ - bq_guess
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - bq_guess.gemspec
88
+ - exe/bq_guess
89
+ - lib/bq_guess.rb
90
+ - lib/bq_guess/cli.rb
91
+ - lib/bq_guess/fields.rb
92
+ - lib/bq_guess/fields/base.rb
93
+ - lib/bq_guess/fields/boolean.rb
94
+ - lib/bq_guess/fields/float.rb
95
+ - lib/bq_guess/fields/integer.rb
96
+ - lib/bq_guess/fields/null.rb
97
+ - lib/bq_guess/fields/record.rb
98
+ - lib/bq_guess/fields/string.rb
99
+ - lib/bq_guess/guesser.rb
100
+ - lib/bq_guess/guessers/json_lines.rb
101
+ - lib/bq_guess/schema.rb
102
+ - lib/bq_guess/version.rb
103
+ homepage: https://github.com/nownabe/bq_guess
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Guess BigQuery schema
127
+ test_files: []