stevedlawrence commented on a change in pull request #207: Added support for enumerations and TypeValueCalc URL: https://github.com/apache/incubator-daffodil/pull/207#discussion_r279772948
########## File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/TypeCalculator.scala ########## @@ -0,0 +1,453 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.daffodil.processors + +import scala.math.BigInt +import scala.collection.immutable.HashSet +import org.apache.daffodil.dsom.CompiledExpression +import org.apache.daffodil.util.Maybe +import org.apache.daffodil.oolag.OOLAG.OOLAGHost +import org.apache.daffodil.dsom.CompiledExpression +import org.apache.daffodil.dsom.CompiledExpression +import scala.collection.immutable.HashMap +import org.apache.daffodil.processors.parsers.PState +import org.apache.daffodil.processors.parsers.ParseError +import org.apache.daffodil.processors.parsers.GeneralParseFailure +import org.apache.daffodil.exceptions.Assert +import java.lang.{ Long => JLong } +import org.apache.daffodil.processors.unparsers.UState +import org.apache.daffodil.util.Maybe.One +import org.apache.daffodil.dsom.CompiledExpression +import org.apache.daffodil.dpath.DState +import org.apache.daffodil.processors.parsers.ParseError +import org.apache.daffodil.util.Maybe.One +import org.apache.daffodil.dpath.NodeInfo.Kind +import org.apache.daffodil.dpath.NodeInfo + +abstract class TypeCalculator[A <: AnyRef, B <: AnyRef](val srcType: NodeInfo.Kind, val dstType: NodeInfo.Kind) + extends Serializable { + type Error = String + + /* + * We can be used from both a parser directly, and as part of a DPath expression. + * There are 2 main differences that require handling these cases seperatly + * + * 1) A (un)parser expects errors to be reported via state.setFailed, while DPath expects subexpressions to report + * error by throwing + * 2) DPathCompiledExpressions provides a top level interface to initiate evaluation which accepts a ParseOrUnparseState, + * and a second interface to evaluate subexpressions which accepts a DState + */ + + def inputTypeCalc(x: A, xType: NodeInfo.Kind): (Maybe[B], Maybe[Error]) + def outputTypeCalc(x: B, xType: NodeInfo.Kind): (Maybe[A], Maybe[Error]) + + def inputTypeCalcParse(pstate: PState, context: RuntimeData, x: A, xType: NodeInfo.Kind): Maybe[B] = { + val (ans, err) = inputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), Maybe(pstate.currentLocation), err.get) + pstate.setFailed(diag) + } + + //In event of an error, we still want to return Maybe.Nope, which happens + //to be what ans would have + ans + } + def outputTypeCalcUnparse(ustate: UState, context: RuntimeData, x: B, xType: NodeInfo.Kind): Maybe[A] = { + val (ans, err) = outputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), Maybe(ustate.currentLocation), err.get) + ustate.setFailed(diag) + } + + //In event of an error, we still want to return Maybe.Nope, which happens + //to be what ans would have + ans + } + + /* + * It appears that dpath expressions actually throw errors. + * See the definition of DAFError + */ + def inputTypeCalcRun(dstate: DState, x: A, xType: NodeInfo.Kind): Unit = { + val context = dstate.runtimeData.get + val (ans, err) = inputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), dstate.contextLocation, err.get) + throw diag + // throw FNErrorFunctionException(Maybe(context.schemaFileLocation), dstate.contextLocation, err.get) + } + + dstate.setCurrentValue(ans) + + } + def outputTypeCalcRun(dstate: DState, x: B, xType: NodeInfo.Kind): Unit = { + val context = dstate.runtimeData.get + val (ans, err) = outputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), dstate.contextLocation, err.get) + throw diag + } + + dstate.setCurrentValue(ans) + + } + + def supportsParse: Boolean = true + def supportsUnparse: Boolean = true +} + +class KeysetValueTypeCalculatorOrdered[A <: AnyRef, B <: AnyRef](valueMap: HashMap[A, B], rangeTable: Seq[(RangeBound[A], RangeBound[A], B)], unparseMap: HashMap[B, A], + srcType: NodeInfo.Kind, dstType: NodeInfo.Kind) + extends TypeCalculator[A, B](srcType, dstType) { + + override def inputTypeCalc(x: A, xType: NodeInfo.Kind): (Maybe[B], Maybe[Error]) = { Review comment: It this is going to allocate a Tuple, where _1 is success and _2 is an error, we should just use an Either. Allocation is going to happen either way, so we don't gain anything by using two maybes. Though, I'm not even sure the error message return is necessary. The error message only contains the value of ``x` and whenther it was "key" or "value"`, which the all should be able to know. I would argue this should just return a a Maybe[B], and the caller can figure out the appropriate error message if Maybe.Nope is returned. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
