/// <summary> /// Returns a flex field by id and name from a collection of fields /// </summary> /// <param name="fields">Fields to search</param> /// <param name="fieldId">Field Id</param> /// <param name="fieldName">Field Name</param> /// <returns>Flex field</returns> public static ScribanFlexFieldField GetFlexField(ICollection <ScribanFlexFieldField> fields, string fieldId, string fieldName) { if (!string.IsNullOrEmpty(fieldId)) { fieldId = fieldId.ToLowerInvariant(); } ScribanFlexFieldField fieldByName = fields.FirstOrDefault(f => f.Id.ToLowerInvariant() == fieldId && f.Name == fieldName); if (fieldByName != null) { return(fieldByName); } if (!string.IsNullOrEmpty(fieldName)) { fieldName = fieldName.ToLowerInvariant(); } fieldByName = fields.FirstOrDefault(f => f.Id.ToLowerInvariant() == fieldId && f.Name.ToLowerInvariant() == fieldName); if (fieldByName != null) { return(fieldByName); } return(fields.FirstOrDefault(f => f.Id == fieldId)); }
/// <summary> /// Maps a value to a field value /// </summary> /// <param name="fieldData">Field for which the data is mapped</param> /// <param name="valueToMap">Value to map</param> /// <param name="exportSettings">Export settings</param> /// <returns>Mapped value</returns> public static object MapValueToFieldValue(ScribanFlexFieldField fieldData, string valueToMap, ExportSettings exportSettings) { if (fieldData != null && fieldData.Type == ExportConstants.Scriban_FlexFieldType_Number) { return(FlexFieldValueCollectorUtil.ConvertFieldValue(ExportConstants.FlexFieldType_Number, valueToMap)); } else { return(ExportUtil.EscapeCharacters(valueToMap, exportSettings.EscapeCharacter, exportSettings.CharactersNeedingEscaping, exportSettings.NewlineCharacter)); } }
/// <summary> /// Converts a single field to an export field with a given anme /// </summary> /// <param name="parentObject">Parent object</param> /// <param name="field">Field to convert</param> /// <param name="fieldName">Field name to use</param> /// <param name="exportSettings">Export settings</param> /// <returns>Converted field</returns> private static ScribanFlexFieldField ConvertFieldWithName(ScribanFlexFieldObject parentObject, FlexField field, string fieldName, ExportSettings exportSettings) { ScribanFlexFieldField convertedField = new ScribanFlexFieldField(parentObject); convertedField.Id = field.Id; convertedField.Name = fieldName; convertedField.Type = ConvertFieldTypeToScribanFieldType(field.FieldType); convertedField.UnescapedValue = ConvertFieldValue(field.FieldType, field.Value); convertedField.Value = convertedField.UnescapedValue; if (convertedField.Value is string) { convertedField.Value = ExportUtil.EscapeCharacters(convertedField.Value.ToString(), exportSettings.EscapeCharacter, exportSettings.CharactersNeedingEscaping, exportSettings.NewlineCharacter); } convertedField.DontExportToScript = field.ScriptSettings != null && field.ScriptSettings.DontExportToScript ? true : false; convertedField.Exists = true; return(convertedField); }
/// <summary> /// Generates a language key for a flex field /// </summary> /// <param name="callerContext">Caller context</param> /// <param name="flexField">Flex field to generate the language key for</param> /// <returns>Language key</returns> private async ValueTask <object> GenerateFlexFieldLanguageKey(ScriptNode callerContext, ScribanFlexFieldField flexField) { if (!flexField.Exists) { string errorValue; ScribanMissingFlexFieldField missingField = flexField as ScribanMissingFlexFieldField; if (missingField != null) { errorValue = missingField.ErrorValue; } else { errorValue = flexField.Value != null?flexField.Value.ToString() : string.Format("MISSING_FIELD_{0}", flexField.Name); } _errorCollection.AddCanNotGenerateLanguageKey(ScribanErrorUtil.FormatScribanSpan(callerContext.Span)); return(errorValue); } if (flexField.ParentObject == null) { _errorCollection.AddCanNotGenerateLanguageKey(ScribanErrorUtil.FormatScribanSpan(callerContext.Span)); return("<<INVALID ARGUMENTS FOR LANGKEY>>"); } return(await _languageKeyGenerator.GetFlexFieldFieldKey(flexField.ParentObject.Id, flexField.ParentObject.Name, flexField.ParentObject.ExportObjectType, flexField.Id, flexField.Name, flexField.UnescapedValue.ToString())); }