public CoralLink(CBORObject node, CoralDictionary dictionary) { if (node[0].AsInt32() != 2) { throw new ArgumentException("Not an encoded CoRAL link"); } RelationType = dictionary.Reverse(node[1]).AsString(); Target = dictionary.Reverse(node[2]); if (node.Count == 4) { Body = new CoralBody(node[3], dictionary); } }
public CoralFormField(CBORObject type, CBORObject value, Cori baseCori, CoralDictionary dictionary) { CBORObject o = (CBORObject)dictionary.Reverse(type, false); if (o == null) { FieldTypeInt = type.AsInt32(); } else if (o.Type == CBORType.Array) { FieldType = new Cori(o); if (type.Type == CBORType.Integer) { FieldTypeInt = type.AsInt32(); } } else { throw new ArgumentException("Not a valid form field type"); } o = (CBORObject)dictionary.Reverse(value, true); if (o == null) { LiteralInt = value.AsInt32(); } else if (o.Type == CBORType.Array) { Url = new Cori(o); if (baseCori != null) { Url = Url.ResolveTo(baseCori); } if (value.Type == CBORType.Integer) { LiteralInt = value.Untag().AsInt32(); } } else { Literal = o; if (value.IsTagged && value.HasOneTag(CoralDictionary.DictionaryTag) && value.Type == CBORType.Integer) { LiteralInt = value.Untag().AsInt32(); } } }
/// <summary> /// Decode a CBOR encoded CoRAL link into the object for working with /// </summary> /// <param name="node">CBOR object to be decoded</param> /// <param name="baseCori">a URI to make things relative to</param> /// <param name="dictionary">dictionary to use for decoding</param> public CoralLink(CBORObject node, Cori baseCori, CoralDictionary dictionary) { if (node[0].AsInt32() != 2) { throw new ArgumentException("Not an encoded CoRAL link"); } if (dictionary == null) { throw new ArgumentNullException(nameof(dictionary)); } CBORObject o = (CBORObject)dictionary.Reverse(node[1], false); if (o == null) { RelationTypeInt = node[1].AsInt32(); } else if (o.Type == CBORType.Array) { RelationType = new Cori(o); if (node[1].Type == CBORType.Integer) { RelationTypeInt = node[1].AsInt32(); } } else { throw new ArgumentException("Invalid relation in CoRAL link"); } CBORObject value = (CBORObject)dictionary.Reverse(node[2], true); if (value == null) { if (!node[2].HasOneTag(CoralDictionary.DictionaryTag)) { throw new ArgumentException("Invalid tagging on value"); } TargetInt = node[2].Untag().AsInt32(); } else if (value.Type == CBORType.Array) { Target = new Cori(value).ResolveTo(baseCori); if (node[2].Type == CBORType.Integer) { TargetInt = node[2].Untag().AsInt32(); } baseCori = Target; } else { if (node[2].IsTagged && node[2].Type == CBORType.Integer) { TargetInt = node[2].Untag().AsInt32(); } Value = value; } if (node.Count == 4) { Body = new CoralBody(node[3], baseCori, dictionary); } }
public CoralForm(CBORObject form, Cori baseCori, CoralDictionary dictionary) { if (form.Type != CBORType.Array && !(form.Count == 3 || form.Count == 4)) { throw new ArgumentException("Invalid form descriptor", nameof(form)); } if (baseCori == null || !baseCori.IsAbsolute()) { throw new ArgumentException("Invalid base reference", nameof(baseCori)); } if (form[0].Type != CBORType.Integer || form[0].AsInt32() != 3) { throw new ArgumentException("Not a CoRAL form descriptor", nameof(form)); } CBORObject o = (CBORObject)dictionary.Reverse(form[1], false); if (o == null) { OperationTypeInt = form[1].Untag().AsInt32(); } else if (o.Type == CBORType.Array) { OperationType = new Cori(o); if (form[1].Type == CBORType.Integer) { OperationTypeInt = form[1].Untag().AsInt32(); } } else { throw new ArgumentException("Invalid operation in CoRAL form"); } o = (CBORObject)dictionary.Reverse(form[2], true); if (o == null) { TargetInt = form[2].Untag().AsInt32(); } else if (o.Type != CBORType.Array) { throw new ArgumentException("Invalid submission target", nameof(form)); } else { Target = new Cori(o); if (form[2].Type == CBORType.Integer && form[2].HasTag(CoralDictionary.DictionaryTag)) { TargetInt = form[2].Untag().AsInt32(); } Target = Target.ResolveTo(baseCori); } if (form.Count == 4) { if (form[3].Type != CBORType.Array) { throw new ArgumentException("Invalid form field array", nameof(form)); } for (int i = 0; i < form[3].Count; i += 2) { FormFields.Add(new CoralFormField(form[3][i], form[3][i + 1], baseCori, dictionary)); } } }