Kaitai Struct: how to remove 0 offsets from the array? - kaitai-struct

I have probably an ancient .ksy template
seq:
- {id: id_magic, type: u4}
- {id: version, type: u2}
- {id: num_blocks, type: u2}
- {id: block_offsets, type: u4, repeat: expr, repeat-expr: num_blocks}
The block_offsets get offset for frame-blocks but unfortunately, some of them are equal to 0 so I need to remove them somehow to move next. In python, it will look like this but I have no clue how to code it in Kaitai
valid_block_offsets = [offset for offset in block_offsets if offset]
I tried to do something with instances but with no luck.

Related

OpenAPI specifications - how to specify an input parameter that accepts a range of values

Using OpenAPI 3.0.3, I am defining an API spec which accepts two input query parameters.
- name: land_area_llimit
in: query
description: Lower limit for land area comparison
required: false
schema:
type: integer
- name: land_area_ulimit
in: query
description: Upper limit for land area comparison
required: false
schema:
type: integer
Ideally, I would like to combine the two and have just one parameter, that accepts a range, like:
[a,b] where a > 0 and b > a > 0. Say, something like:
- name: land_area
in: query
description: lower and upper bounds for land area comparison
required: false
schema:
type: range
## With some way to specify that this parameter accepts a lower bound and an upper bound.
I am aware of the minimum and maximum. That will preset the ranges. I am looking for the ranges to be provided as input.
Can this be achieved?
You can define the range as a tuple (supported since OpenAPI 3.1) or as an array of 2 elements.
However, there's no way to have a dynamic minimum attribute that's based on another value. You'll need to mention this requirement in the description and verify the values on the backend.
# openapi: 3.1.0
- name: land_area
in: query
description: Lower and upper bounds for land area comparison
required: false
schema:
type: array
prefixItems:
- type: integer
description: Lower bound for land area comparison
- type: integer
description: >-
Upper bound for land area comparison.
Must be greater than the lower bound.
minItems: 2
additionalItems: false
I ended up using an object type for the input:
- name: land_area
in: query
description: Land area ranges for comparison (in sqft). lower_bound < upper_bound. Return 400 otherwise.
required: false
schema:
type: object
properties:
land_area_lower_bound:
type: integer
land_area_upper_bound:
type: integer
Checking in Swagger UI, the request URL will resolve to something like:
http://<url>/<api>?land_area_lower_bound=1234&land_area_upper_bound=3456

Capture a three bytes two's complement signed integer with Kaitai

Kaitai Struct offers predefined types to capture, for example, signed 2-bytes integers (s2be) or signed 4-bytes integers (s4be) but there is no s3be and b24 captures 3-bytes unsigned integer (http://doc.kaitai.io/ksy_reference.html#_bit_size_integers). Is there a way to do it?
field_a:
seq:
- id: two
type: s2be
- id: three
type: ???
- id: four
type: s4be
There are multiple ways to do that. For example, you can use something like this to convert unsigned to signed:
seq:
- id: three
type: s3be
types:
s3be:
seq:
- id: unsigned_value
type: b24
instances:
value:
value: '(unsigned_value & 0x800000 != 0) ? (~(unsigned_value & 0x7fffff)) : unsigned_value'
Note that it will be user type, so to get to the value of the integer, you'll need to use three.value, not just three.

How to limit my swagger definition array to be either 0 or 3

I have my swagger definition like :
someDef:
type: object
properties:
enable:
type: boolean
default: false
nodes:
type: array
maxItems: 3
items:
type: object
properties:
ip:
type: string
default: ''
My nodes are array and it has maxitems: 3.
I want my nodes items length to be either 0 or 3.
Thanks in advance.
"Either 0 or 3 items" can be defined in OpenAPI 3.x (openapi: 3.x.x) but not in OpenAPI 2.0 (swagger: '2.0').
OpenAPI 3.x
You can use oneOf in combination with minItems and maxItems to define the "either 0 or 3 items" condition:
# openapi: 3.0.0
nodes:
type: array
items:
type: object
properties:
ip:
type: string
default: ''
oneOf:
- minItems: 0
maxItems: 0
- minItems: 3
maxItems: 3
Note while oneOf is part of the OpenAPI 3.0 Specification (i.e. you can write API definitions that include oneOf), actual tooling support for oneOf may vary.
OpenAPI 2.0
OAS 2 does not have a way to define "either 0 or 3 items". The most you can do is to use maxItems: 3 to define the upper limit.

Is it better to use number instead of string for enum schema in mongoose?

Is it better to use number instead of string for enum schema in mongoose in terms of search performance?
For example, I have this:
status: {
type: String,
enum: ['active', 'inactive', 'disabled', 'deleted'],
default: 'inactive'
},
//status: {
// type: Number,
// enum: [0, 1, 2, 3],
// default: '1'
//},
will db.col.find({status: 'active'}) slower than db.col.find({status: 1}) ?
As in this and this similar questions it is for the most part negligible from a performance point of view when using an index - but numbers will be a bit faster.
In case you still want to use number, you could add a virtual property to your schema which translates from your numeric enum to a descriptive string so that you have the best of both worlds in some sense.
Mongodb uses BSON(Binary JSON) to store the documents.
Read this for understanding json and bson Mongodb - https://www.mongodb.com/json-and-bson
Numbers (integers and doubles) are "basic types"
in BSON (the binary format in which data is stored in Mongo), and do
not carry the extra overhead with them.
Strings carry a little extra overhead with
them; bits to tell Mongo that they are strings, and bits to tell Mongo
how long they are.
So number would be faster than string.
Reference
http://bsonspec.org/spec.html

Mongo: How do I group documents based on one of two fields?

If I have documents that look like:
{
_id: 1,
val1: x,
val2: aa
},
{
_id: 2,
val1: y,
val2: bb
},
{
_id: 3,
val1: x,
val2: cc
},
{
_id: 4,
val1: z,
val2: bb
}
Is it possible to group them in MongoDB so that docs 1 and 3 are paired and docs 2 and 4 are paired?
In essence, I'm looking to group docs if their val1 OR val2 are the same. Also, there will NOT be the possibility of docs being able to be in two different groups. Meaning, I should be able to partition the set of docs. Is the possible in Mongo?
Ultimately, I want to partition my set of documents based on the aforementioned criteria and then count the size of each subset.
I've tried attacking this problem by grouping on the val1 field and using $addToSet to created an array of val2's. But then I'm stuck because I don't know of a way in Mongo to merge arrays that contain at least one common element. If I did, I could use that list of arrays and aggregate again using $in.
Please let me know if I can clarify my question in any way!