Skip to content

Commit

Permalink
Use String.length instead of byte_size (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbusi authored Mar 11, 2020
1 parent 750487b commit 61f26c8
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/posexional/field/value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ defmodule Posexional.Field.Value do
Field.positionalize(default, field)
end

def write(field = %Field.Value{size: size}, value) when is_binary(value) and byte_size(value) <= size do
Field.positionalize(value, field)
def write(field = %Field.Value{name: name, size: size}, value) when is_binary(value) do
if String.length(value) <= size do
Field.positionalize(value, field)
else
raise "The value #{value} is too long for the #{name} field. " <>
"The maximum size is #{size} while the value is #{String.length(value)}"
end
end

def write(%Field.Value{name: name}, value) when not is_binary(value) do
def write(%Field.Value{name: name}, _value) do
raise "The value provided for the #{name} field doesn't seem to be a string"
end

def write(%Field.Value{name: name, size: size}, value) do
raise "The value #{value} is too long for the #{name} field. " <>
"The maximum size is #{size} while the value is #{byte_size(value)}"
end
end

defimpl Posexional.Protocol.FieldLength, for: Posexional.Field.Value do
Expand Down

0 comments on commit 61f26c8

Please sign in to comment.