Is it possible to use two When statements in a Yang model - yang

Trying to use one Yang leaf with two different if-types depending on the value given.
Currently have:
leaf interface_number {
when "boolean(string(/payload/interface_type) != 'ae')";
type isyt:interface_number_value;
when "boolean(string(/payload/interface_type) == 'ae')";
type isyt:interface_lag_value;
description
"Interface Number. Example value: 1/1/1 or 11 for LAG";
mandatory "true";
}
I have also tried:
leaf interface_number {
when "boolean(string(/payload/interface_type) != 'ae')" {
type isyt:interface_number_value;
}
when "boolean(string(/payload/interface_type) == 'ae')" {
type isyt:interface_lag_value;
}
description
"Interface Number. Example value: 1/1/1 or 11 for LAG";
mandatory "true";
}
Yang seems to accept the first when they errors on the second when statements' boolean.
Is this even possible? or is there a better method to use for this.

That's not how the YANG syntax works; the type statement cannot be conditional upon the when. The when affects the whole enclosing node, so if it evaluates to false, your interface_number would not be valid at all.
What you can do here is to create a choice whose case statements are when-conditional depending on the type of the ../interface_type leaf. This will need a different name for each of the leaf that you define within these case statements.

Related

Conditionally execute a funtion, throws error `Conditions must have a static type of 'bool'. Try changing the condition`

I want to feed Two to the function. When function receives Two as the data, it should print success to the console. But seems like it's not the correct way.
Error: Conditions must have a static type of 'bool'. Try changing the condition.
List
enum ButtonList {One, Two, Three}
Calling function
testFunc(ButtonList.Two)),
Function
testFunc( ButtonList type) {
if (type = ButtonList.Two ){print('sucess ')};
}
It should be:
testFunc(ButtonList type) {
if (type == ButtonList.Two) {
print('sucess ')
};
}
There's a big difference between = (assigning a value to a variable) and == (equality comparison). if expects a condition (==), not the assigning operation (=).
Formatting is important to read and understand the code. Please read Dart best code style practices: https://dart.dev/guides/language/effective-dart/style
You are trying to assign with =, use == instead

NDepend: Find fields that are either a given type or use a given type in their generic parameters

How would I go about using NDepend to not only identify JustMyCode.Fields that are exactly a given type, but also indirectly, i.e. fields like IList<MyType>, IDictionary<int, MyType>, Lazy<T> and all those "nice" generic variants/usages?
Is there any helper method similar to .UsedBy(...) available by any chance that provides such a functionality?
Here is a query to get field typed with String or Int32:
let types = Types.WithFullNameIn(
"System.String",
"System.Int32").ToArray()
from f in Application.Fields
where !f.ParentType.IsEnumeration &&
f.FieldType != null &&
types.Contains(f.FieldType)
select new { f, type =f.FieldType }
For now you cannot detect when a type is used in a generic parameter.

Multi if statement in class parameters setting

I know that in the latest version of dart we can use if else statements inside the build method. Does anyone know if we can use also if else statement when we setting class parameters? I know I can do inline statement there but inline is a bit hard to read when there are multiple conditions
const int i = 0;
class Person {
// NewClass n = NewClass(a: i == 0 ? 'a' : 'b'); //<- inline statement working
NewClass n = NewClass(a: if(i == 0) 'a' else 'b'); //<- if statement doesn't
}
class NewClass {
final String a;
const NewClass({this.a});
}
Edit:
Basically in my case I've got an TextField widget where I set its's type parameter from enum (Type.text, Type.numeric...) According to this parameter I want to set The textField parameters (textCapitalization, maxLength and so on)
As per your comment, you are already creating an enum for specifying the type of the fields.
enum Type {text, numeric}
Now for specifying the properties of that particular type, you can add an extension on this enum, as shown below:
extension TextFieldProperties on Type {
int get maxLength {
if (this == Type.text) {
return 10;
}
return 12;
}
}
So in your field class you already have a type defined, you can use that type variable to get the properties of that particular type of field.
Type type = Type.text;
print(type.maxLength); // Will print 10
type = Type.numeric;
print(type.maxLength); // Will print 12
Note: It will work only in Dart 2.7 and above
You want the conditional expression (?:), not the conditional statement or literal entry (if), as you have already discovered.
The reason if doesn't work is that if only works as a statement or as a collection literal entry. It doesn't work in arbitrary expressions.
The reason for the distinction is that the if syntax allows you to omit the else branch. That only makes sense in places where "nothing" is a valid alternative. For a statement, "doing nothing" is fine. For a collection, "adding nothing" is also fine.
In an expression context, you must evaluate to a value or throw. There is no reasonable default that we can use instead of "nothing", so an if is not allowed instead of an expression.
Doesn't work because this syntax doesn't exist in Dart. The only way to do what you would like to do is to use the ternary operator.
If you try it in the DartPad you will get an error.
I suggest you to use a function to return the right value.

How to remove points based on their attributes in Houdini?

I created a curve with several points. Now I want to delete some points based on one of their attribute (will_be_removed).
As is shown in the picture above, those points with i#will_be_removed set to 1 will be removed.
I tried using the VEX code below but it said invalid subscript for type: int.will_be_removed
if(#ptnum.will_be_removed == 1)
{
removepoint(0, #ptnum);
}
How can I correctly reference those points?
The error in this code
if(#ptnum.will_be_removed == 1)
{
removepoint(0, #ptnum);
}
is because #ptnum is a VEX type int. #ptnum can also be written i#ptnum to exlicitly indicate it's type, but since it is a well known attribute (see documentation in link) you can also write it shorthand as #ptnum.
int types are numbers, and do not contain collections of other data.
Regarding attributes, you also want to keep in mind if they are vertex, point, primitive, or detail attributes.
Attribute precedence
When two components in the same geometry have an attribute with the same name, the
attribute on the "lower level" of geometry is used, so:
Vertex attributes, which override:
Point attributes, which override:
Primitive attributes, which override:
Detail (whole geometry) attributes
or one liner wrangler will be
if (#will_be_deleted == 1) removepoint(0, #ptnum);
I think I figure out a way to do it. Use #will_be_removed instead of #ptnum.will_be_removed instead:
if(#will_be_removed == 1)
{
removepoint(0, #ptnum);
}

Yang Model recursive search for must condition

I have a problem with a restriction on my CLI. I've been investigating yang RFC7950 (https://www.rfc-editor.org/rfc/rfc7950) but I've found nothing.
Here is an example.
grouping httpGroup {
list http-list{
key "value";
leaf value {
status current { yexte:preliminary; }
description "value to match";
must "(not(../protocol)) and (not(../network-port)))" {
error-message "Not compatible with protocol or non-TCP ports";
}
type string { length "1..255"; }
}
}
}
This group will be included in several groups with the following structure:
list and {
leaf-list protocol { ..... }
uses A;
list or {
leaf-list protocol { ..... }
uses A;
}
}
grouping A {
status{}
leaf-list protocol { ..... }
leaf-list X { ..... }
uses httpGroup;
}
I need this must condition included in httpGroup to verify that protocol value has not been configured in any level of the hierarchy.
I've made this be adding more relatives paths to search for this node:
// same level
not(../protocol)
// next level
not(../and/protocol)
not(../or/protocol)
// previous level
not(../../protocol)
not(../../protocol)
//recursively down previous level
not(../../and/protocol)
not(../../or/protocol)
// third level
not(../and/or/protocol)
not(../and/and/protocol)
As you can see, this is not a clean solution at all.
Is there any way it can be done for a whole hierarchy like:
if protocol node exists and http-list exists then error.
Thank you in advance.
Groupings are meant to be reusable. It is a bad practice to attempt to create a grouping that may only be used in specific contexts. This is exactly what happens if you define an XPath expression within a grouping and this expression references nodes that are "outside" this grouping (a not yet known ancestor data node, for example, or even worse - an ancestor with a specific name).
The proper way for you to handle this situation would be to use a refine statement in each different context where this grouping is used. You target the value leaf with it, then refine it by adding a must statement, the expression of which of course depends on usage context. You do not define a must statement within grouping http-list.
Within grouping A:
grouping A {
status{}
leaf-list protocol { ..... }
leaf-list X { ..... }
uses httpGroup {refine "http-list/value" {must "not(../../protocol)";}}
}
As you can see, grouping A is now completely self-sufficient and may be used within any context - the must will not have any problems with it.