-
Notifications
You must be signed in to change notification settings - Fork 2
Flexibly infer types #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de553dc
Add some tests and option inferring
Karolina-Bogacka e1146ad
Add type inferrence
Karolina-Bogacka 17b823d
Correct a small bug
Karolina-Bogacka 23e5820
Merge branch 'main' into 40-flexibly-infer-types
Ostrzyciel 560ad35
Have good RdfFormat patterns
Karolina-Bogacka d3077d7
save changes
Karolina-Bogacka dd6898c
Add typetests instead of classtags to make the solution work
Karolina-Bogacka c83fb14
Simplify code
Karolina-Bogacka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/main/scala/eu/neverblink/jelly/cli/command/rdf/RdfCommand.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package eu.neverblink.jelly.cli.command.rdf | ||
|
|
||
| import com.google.protobuf.InvalidProtocolBufferException | ||
| import org.apache.jena.riot.RiotException | ||
| import eu.neverblink.jelly.cli.* | ||
| import caseapp.* | ||
| import eu.ostrzyciel.jelly.core.{RdfProtoSerializationError, RdfProtoDeserializationError} | ||
|
|
||
| import java.io.{InputStream, OutputStream} | ||
|
|
||
| /** This abstract class is responsible for the common logic in both RDF parsing commands | ||
| */ | ||
| abstract class RdfCommand[T <: HasJellyOptions: {Parser, Help}] extends JellyCommand[T]: | ||
|
|
||
| override def group = "rdf" | ||
|
|
||
| /** What is the default action if no formats specified */ | ||
| def defaultAction: (InputStream, OutputStream) => Unit | ||
|
|
||
| /** The print util responsible for handling the specific formats etc the command requires */ | ||
| lazy val printUtil: RdfCommandPrintUtil | ||
|
|
||
| /** The method responsible for matching the format to a given action */ | ||
| def matchToAction(option: RdfFormatOption): Option[(InputStream, OutputStream) => Unit] | ||
|
|
||
| /** This method takes care of proper error handling and takes care of the parameter priorities in | ||
| * matching the input to a given format conversion | ||
| * | ||
| * @param inputStream | ||
| * InputStream | ||
| * @param outputStream | ||
| * OutputStream | ||
| * @param format | ||
| * Option[String] | ||
| * @param fileName | ||
| * Option[String] | ||
| * @throws JellyDeserializationError | ||
| * @throws JenaRiotException | ||
| * @throws InvalidJellyFile | ||
| */ | ||
| def parseFormatArgs( | ||
| inputStream: InputStream, | ||
| outputStream: OutputStream, | ||
| format: Option[String], | ||
| fileName: Option[String], | ||
| ): Unit = | ||
| try { | ||
| val explicitFormat = if (format.isDefined) RdfFormatOption.find(format.get) else None | ||
| val implicitFormat = | ||
| if (fileName.isDefined) RdfFormatOption.inferFormat(fileName.get) else None | ||
| (explicitFormat, implicitFormat) match { | ||
| case (Some(f: RdfFormatOption), _) if matchToAction(f).isDefined => | ||
| matchToAction(f).get(inputStream, outputStream) | ||
| // If format explicitely defined but does not match any available actions or formats, we throw an error | ||
| case (_, _) if format.isDefined => | ||
| throw InvalidFormatSpecified(format.get, printUtil.validFormatsString) | ||
| case (_, Some(f: RdfFormatOption)) if matchToAction(f).isDefined => | ||
| matchToAction(f).get(inputStream, outputStream) | ||
| // If format not explicitely defined but implicitely not understandable we default to this | ||
| case (_, _) => defaultAction(inputStream, outputStream) | ||
| } | ||
| } catch | ||
| case e: RiotException => | ||
| throw JenaRiotException(e) | ||
| case e: InvalidProtocolBufferException => | ||
| throw InvalidJellyFile(e) | ||
| case e: RdfProtoDeserializationError => | ||
| throw JellyDeserializationError(e.getMessage) | ||
| case e: RdfProtoSerializationError => | ||
| throw JellySerializationError(e.getMessage) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.