Skip to content

Commit

Permalink
Add new to sources and sinks
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmi committed Jan 14, 2024
1 parent 0e6f054 commit c3f6f3b
Show file tree
Hide file tree
Showing 23 changed files with 4,986 additions and 5,126 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ alias Strom.Source.ReadLines

source =
:lines
|> Source.new(%ReadLines{path: "input.txt"})
|> Source.new(ReadLines.new("input.txt"))
|> Source.start()

%{lines: stream} = Source.call(%{}, source)
Expand Down Expand Up @@ -143,7 +143,7 @@ alias Strom.Sink.WriteLines

sink_short =
:short
|> Sink.new(%WriteLines{path: "short.txt"})
|> Sink.new(WriteLines.new("short.txt"))
|> Sink.start()

sink_long =
Expand Down
2 changes: 2 additions & 0 deletions lib/sink/io_puts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Strom.Sink.IOPuts do

defstruct line_sep: "", prefix: ""

def new, do: %__MODULE__{}

@impl true
def start(%__MODULE__{} = io_puts), do: io_puts

Expand Down
2 changes: 2 additions & 0 deletions lib/sink/null.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Strom.Sink.Null do

defstruct []

def new, do: %__MODULE__{}

@impl true
def start(%__MODULE__{} = null), do: null

Expand Down
10 changes: 7 additions & 3 deletions lib/sink/write_lines.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
defmodule Strom.Sink.WriteLines do
@behaviour Strom.Sink

defstruct path: nil, file: nil

@line_sep "\n"

defstruct path: nil, file: nil, line_sep: @line_sep

def new(path, line_sep \\ @line_sep) when is_binary(path) and is_binary(line_sep) do
%__MODULE__{path: path, line_sep: line_sep}
end

@impl true
def start(%__MODULE__{} = write_lines) do
file = File.open!(write_lines.path, [:write])
Expand All @@ -13,7 +17,7 @@ defmodule Strom.Sink.WriteLines do

@impl true
def call(%__MODULE__{} = write_lines, data) do
:ok = IO.write(write_lines.file, data <> @line_sep)
:ok = IO.write(write_lines.file, data <> write_lines.line_sep)

{:ok, {[], write_lines}}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Strom.Source do
end

def start(%__MODULE__{origin: list} = source) when is_list(list) do
start(%{source | origin: %Strom.Source.Events{events: list}})
start(%{source | origin: Strom.Source.Events.new(list)})
end

def start(%__MODULE__{origin: origin} = source) when is_struct(origin) do
Expand Down
2 changes: 2 additions & 0 deletions lib/source/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Strom.Source.Events do

defstruct infinite: false, events: []

def new(events) when is_list(events), do: %__MODULE__{events: events}

@impl true
def start(%__MODULE__{} = state), do: state

Expand Down
2 changes: 2 additions & 0 deletions lib/source/io_gets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Strom.Source.IOGets do

defstruct infinite: true

def new, do: %__MODULE__{}

@impl true
def start(%__MODULE__{} = io_gets), do: io_gets

Expand Down
2 changes: 2 additions & 0 deletions lib/source/read_lines.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Strom.Source.ReadLines do

defstruct path: nil, file: nil, infinite: false

def new(path) when is_binary(path), do: %__MODULE__{path: path}

@impl true
def start(%__MODULE__{} = read_lines), do: %{read_lines | file: File.open!(read_lines.path)}

Expand Down
7 changes: 3 additions & 4 deletions test/composite_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule Strom.CompositeTest do

defmodule MyComposite do
import Strom.DSL
alias Strom.Sink.Null

def components do
odd_even = %{
Expand All @@ -19,7 +18,7 @@ defmodule Strom.CompositeTest do
mix([:s1, :s2], :s),
transform(:s, &(&1 + 1)),
split(:s, odd_even),
sink(:odd, %Null{})
sink(:odd, Null.new())
]
end
end
Expand All @@ -30,7 +29,7 @@ defmodule Strom.CompositeTest do
def components do
[
split(:numbers, %{more: &(&1 >= 10), less: &(&1 < 10)}),
sink(:less, %Null{})
sink(:less, Null.new())
]
end
end
Expand Down Expand Up @@ -68,7 +67,7 @@ defmodule Strom.CompositeTest do
Mixer.new([:s1, :s2], :s),
Transformer.new(:s, &(&1 + 1)),
Splitter.new(:s, odd_even),
Sink.new(:odd, %Null{})
Sink.new(:odd, Null.new())
]

composite = Composite.start(components)
Expand Down
107 changes: 0 additions & 107 deletions test/data/output.csv
Original file line number Diff line number Diff line change
@@ -1,107 +0,0 @@
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
ORDER_CREATED,2017-04-18T20:00:00.000Z,111,3
ORDER_CREATED,2017-04-20T09:00:00.000Z,222,2
ORDER_CREATED,2017-04-21T09:00:00.000Z,333,2
Loading

0 comments on commit c3f6f3b

Please sign in to comment.