Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
openmole
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
RoiArthurB
openmole
Commits
147302ed
Commit
147302ed
authored
Apr 16, 2020
by
Romain Reuillon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Plugin] enh: support array in csv sampling
parent
a8ad6338
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
14 deletions
+37
-14
openmole/core/org.openmole.core.context/src/main/scala/org/openmole/core/context/Val.scala
...ontext/src/main/scala/org/openmole/core/context/Val.scala
+7
-0
openmole/core/org.openmole.core.csv/src/main/scala/org/openmole/core/csv/package.scala
...re.csv/src/main/scala/org/openmole/core/csv/package.scala
+30
-14
No files found.
openmole/core/org.openmole.core.context/src/main/scala/org/openmole/core/context/Val.scala
View file @
147302ed
...
...
@@ -17,6 +17,7 @@
package
org.openmole.core.context
import
org.openmole.core.exception.UserBadDataError
import
org.openmole.core.tools.obj.ClassUtils._
import
org.openmole.core.tools.obj.
{
ClassUtils
,
Id
}
import
shapeless.
{
TypeCase
,
Typeable
}
...
...
@@ -41,6 +42,12 @@ object ValType {
rec
(
t
)
}
def
unsecureFromArray
(
t
:
ValType
[
_
])
:
ValType
[
_
]
=
{
val
(
res
,
level
)
=
unArrayify
(
t
)
if
(
level
==
0
)
throw
new
UserBadDataError
(
s
"ValType $t is no an array type"
)
res
}
/**
* Decorate ValType for implicit conversions to array type
* @param p
...
...
openmole/core/org.openmole.core.csv/src/main/scala/org/openmole/core/csv/package.scala
View file @
147302ed
package
org.openmole.core
import
au.com.bytecode.opencsv.CSVReader
import
org.openmole.core.context.ValType
import
scala.reflect.ClassTag
/*
* Copyright (C) 2019 Romain Reuillon
...
...
@@ -141,23 +144,36 @@ package object csv {
Iterator
.
continually
(
reader
.
readNext
).
takeWhile
(
_
!=
null
).
map
{
line
⇒
(
columns
zip
columnsIndexes
).
map
{
case
((
_
,
v
),
i
)
⇒
Variable
.
unsecure
(
v
,
converter
(
v
)(
line
(
i
)
))
case
((
name
,
v
),
i
)
⇒
Variable
.
unsecure
(
v
,
matchConverter
(
v
,
line
(
i
),
name
))
}
}
}
val
conveters
=
Map
[
Class
[
_
]
,
(
String
⇒
_
)](
classOf
[
BigInteger
]
→
(
new
BigInteger
(
_:
String
)),
classOf
[
BigDecimal
]
→
(
new
BigDecimal
(
_:
String
)),
classOf
[
Double
]
→
((
_:
String
).
toDouble
),
classOf
[
String
]
→
((
_:
String
).
toString
),
classOf
[
Boolean
]
→
((
_:
String
).
toBoolean
),
classOf
[
Int
]
→
((
_:
String
).
toInt
),
classOf
[
Float
]
→
((
_:
String
).
toFloat
),
classOf
[
Long
]
→
((
_:
String
).
toLong
)
)
def
converter
[
T
](
p
:
Val
[
_
])
:
String
⇒
_
=
conveters
.
getOrElse
(
p
.
`type`
.
runtimeClass
,
throw
new
UserBadDataError
(
"Unmanaged type for csv sampling for column binded to prototype "
+
p
))
def
matchConverter
(
v
:
Val
[
_
],
s
:
String
,
name
:
String
)
:
Any
=
{
def
matchArray
[
T:
ClassTag
](
s
:
String
,
convert
:
String
⇒
T
)
:
Array
[
T
]
=
{
val
trimed
=
s
.
trim
if
(!
trimed
.
startsWith
(
"["
)
||
!
trimed
.
endsWith
(
"]"
))
throw
new
UserBadDataError
(
s
"Array in CSV files should have the following format [.., .., ..], found $s"
)
s
.
drop
(
1
).
dropRight
(
1
).
split
(
","
).
map
{
s
⇒
convert
(
s
.
trim
)
}
}
v
match
{
case
Val
.
caseDouble
(
v
)
⇒
s
.
toDouble
case
Val
.
caseString
(
v
)
⇒
s
case
Val
.
caseBoolean
(
v
)
⇒
s
.
toBoolean
case
Val
.
caseInt
(
v
)
⇒
s
.
toInt
case
Val
.
caseLong
(
v
)
⇒
s
.
toLong
case
Val
.
caseArrayDouble
(
v
)
⇒
matchArray
(
s
,
_
.
toDouble
)
case
Val
.
caseArrayInt
(
v
)
⇒
matchArray
(
s
,
_
.
toInt
)
case
Val
.
caseArrayLong
(
v
)
⇒
matchArray
(
s
,
_
.
toLong
)
case
Val
.
caseArrayString
(
v
)
⇒
matchArray
(
s
,
identity
)
case
Val
.
caseArrayBoolean
(
v
)
⇒
matchArray
(
s
,
_
.
toBoolean
)
case
Val
.
caseArrayArrayDouble
(
v
)
⇒
matchArray
(
s
,
matchArray
(
_
,
_
.
toDouble
))
case
Val
.
caseArrayArrayInt
(
v
)
⇒
matchArray
(
s
,
matchArray
(
_
,
_
.
toInt
))
case
Val
.
caseArrayArrayLong
(
v
)
⇒
matchArray
(
s
,
matchArray
(
_
,
_
.
toLong
))
case
Val
.
caseArrayArrayString
(
v
)
⇒
matchArray
(
s
,
matchArray
(
_
,
identity
))
case
Val
.
caseArrayArrayBoolean
(
v
)
⇒
matchArray
(
s
,
matchArray
(
_
,
_
.
toBoolean
))
case
_
⇒
throw
new
UserBadDataError
(
s
"Unsupported type in CSV sampling prototype $v mapped to column $name"
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment