Kong -- How do I write my entity_checks for array #6989 - lua

Summary
I am developing my own rate-limiting plugins based on the official one.
The origin schema looks like:
fields = {
{ config = {
type = "record",
fields = {
{ second = { type = "number", gt = 0 }, },
{ minute = { type = "number", gt = 0 }, },
}
...
},
},
My schema looks like:
{ config = {
type = "record",
fields = {
{ plans = {
type = "array",
elements = {
type = "record",
fields = {
{ second = { type = "number", gt = 0 }, },
{ minute = { type = "number", gt = 0 }, },
}
},
},},
},
custom_validator = validate_periods_order,
},
},
You can see that the first field is called "plans" and it's type is an array which means I will have config.plans[0].second, config.plans[0].second, ... and the original one was config.second
Below is the origin entity_checks function of rate-limiting, I have no idea how to re-write it to match my schema since it changed from simple record type to array type
entity_checks = {
{ at_least_one_of = { "config.second", "config.minute", "config.hour", "config.day", "config.month", "config.year" } },
{ conditional = {
if_field = "config.policy", if_match = { eq = "config.redis" },
then_field = "config.redis_host", then_match = { required = true },
} },
{ conditional = {
if_field = "config.policy", if_match = { eq = "config.redis" },
then_field = "config.redis_port", then_match = { required = true },
} },
{ conditional = {
if_field = "config.policy", if_match = { eq = "config.redis" },
then_field = "config.redis_timeout", then_match = { required = true },
} },
},

Create a local schema for plan
local plan_schema = {
type = "record",
fields = {
{ second = { type = "number", gt = 0 }, },
{ minute = { type = "number", gt = 0 }, },
},
custom_validator = validate_periods_order,
entity_checks = {
{ at_least_one_of = { "second", "minute" } },
}
}
Then on your config set plans as an array of plan
return {
name = plugin_name,
fields = {
{ consumer = typedefs.no_consumer },
{ protocols = typedefs.protocols_http },
{ config = {
type = "record",
fields = {
{ plans = {
type = "array",
elements = plan_schema ,
} },
} },
},
}

Related

Textfield that changes readOnly value

I'm making a TextField with ExposedDropDownMenu for selecting a Country.
When i select something i use mutable state variable 'isReadOnly' for setting the readOnly parameter of TextField composable.
So when I select a country, i want it to be readOnly, but when I click on dismiss icon, I want it to not be readOnly.
I works very well, except one part, when I click on dismiss icon, I can type to my TextField, but software keyboard is not showing up. Do you know the solution to this problem?
val selectedFromController = selected.value
var selectedItem by remember {
mutableStateOf(
selectedFromController.ifEmpty { "" }
)
}
var isReadOnly by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded.value,
onExpandedChange = { expanded.value = !expanded.value }
) {
TextField(
value = selectedItem,
onValueChange = {
selectedItem = it
expanded.value = true
},
readOnly = isReadOnly,
modifier = Modifier
.menuAnchor()
.fillMaxWidth(),
label = { Text(stringResource(id = label)) },
trailingIcon = { IconButton(
onClick = {
selectedItem = ""
selected.value = ""
savedId.value = -1
isReadOnly = false
keyboardController.show()
},
content = {
Icon(Icons.Filled.Clear, null)
}
) },
colors = ExposedDropdownMenuDefaults.textFieldColors()
)
val filteringOptions =
menuItems.filter { it.contains(selectedItem, ignoreCase = true) }
if (filteringOptions.isNotEmpty()) {
ExposedDropdownMenu(
expanded = expanded.value,
onDismissRequest = {
expanded.value = false
}
) {
filteringOptions.take(5).map {
DropdownMenuItem(
text = { Text(text = it) },
onClick = {
selectedItem = it
selected.value = it
expanded.value = false
isReadOnly = true
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding
)
}
}
}
}
I tried to bring it up with keyboardController, but it didn't help.

Strange (interpolated maybe) values from openlayers canvas

I create an openlayers map application for creating histogram from COG layer. I using WebGLTile layer and DataTile source. In a map I have only 2 colors but gl.readPixels returns 12 color values.
map screenshot
part of code:
map.on("click", (evt) => {
var buffer = {};
const gl = webgl.getRenderer().helper.getGL();
const pixelData = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);
gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixelData);
let r, g, b, c = 0;
for (let i = 0; i < pixelData.length; i+=4) {
r = pixelData[i];
g = pixelData[i+1];
b = pixelData[i+2];
let item;
item = [r, g, b].reduce((acc, cur) => {
if (!acc.hasOwnProperty(cur)) {
acc[cur] = {};
}
return acc[cur];
}, buffer);
item.count = (item.count || 0) + 1;
}
});
In final buffer I need only
{
"165": {
"1": {
"38": {
"count": 1181613
}
},
},
"255": {
"255": {
"191": {
"count": 351
}
}
}
}
but I get 12 colors between these two intervals.
{
"165": {
"1": {
"38": {
"count": 1181613
}
},
"2": {
"39": {
"count": 130473
}
}
},
"168": {
"10": {
"43": {
"count": 130413
}
}
},
"169": {
"11": {
"44": {
"count": 15151
}
}
},
"174": {
"27": {
"54": {
"count": 130481
}
},
"28": {
"54": {
"count": 14792
}
}
},
"177": {
"36": {
"59": {
"count": 14460
}
}
},
"178": {
"37": {
"59": {
"count": 1631
}
}
},
"242": {
"219": {
"170": {
"count": 130366
}
}
},
"243": {
"220": {
"170": {
"count": 14487
}
}
},
"246": {
"228": {
"175": {
"count": 14884
}
},
"229": {
"175": {
"count": 1574
}
}
},
"251": {
"245": {
"185": {
"count": 15250
}
}
},
"252": {
"246": {
"186": {
"count": 1586
}
}
},
"255": {
"254": {
"190": {
"count": 1528
}
},
"255": {
"191": {
"count": 351
}
}
}
}
I have no idea why is this happenning and I spent days searching for solution.
I found some example, not with openlayers, and this approach was fine. returns only two values from double-color square.
It's some kind of interpolation on the openlayers side?
Thnaks for any help!

Group a dictionary with same type into an array with full key and value using Swift

it's been a long time I don't use Swift.
I have a response data like this and I saved it into an array named responseData:
[
{
"type": "Switch",
"name": "Switch1",
"search_key": "is_promotion",
"key": "is_promotion=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch2",
"search_key": "shop_type",
"key": "shop_type=2",
"icon": ""
},
{
"type": "Switch",
"name": "Switch3",
"search_key": "is_certified",
"key": "is_certified=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch4",
"search_key": "shop_free_shipping",
"key": "shop_free_shipping=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch5",
"search_key": "is_loyalty",
"key": "is_loyalty=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch6",
"search_key": "is_using_instant",
"key": "is_using_instant=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch7",
"search_key": "is_installment",
"key": "is_installment=1",
"icon": ""
},
{
"type": "Range",
"name": "Price Range",
"search_key": "level_Price_Max_Min",
"value": [
{
"option_id": 0,
"option_name": 0
},
{
"option_id": 0,
"option_name": 10000000
}
]
},
{
"type": "ColorTerm",
"name": "Color",
"search_key": "color_key",
"value": [
{
"option_id": 605,
"value": "Black",
"color_id": 13,
"image": "",
"option_name": "Black",
"background": "#000000",
"option_active": "",
"facet_count": 52655
},
Now I wanna group all dictionary with type Switch into one array and I can access to the keys inside it, then present data of both Switch type array and the others type on a UITableView which have 2 section (Switch type in section 0). How can I do it? I have to search some other solution but I don't understand how to apply them to my code for work.
Here is my FilterModel class:
class FilterModel: NSObject, NSCoding, NSCopying {
override func copy(with zone: NSZone? = nil) -> Any {
// This is the reason why `init(_ model: GameModel)`
// must be required, because `GameModel` is not `final`.
let copy = FilterModel(dict: self.dictionary)
if let arrAttribute = NSArray(array: self.value , copyItems: true) as? [AttributeValueModel] {
copy.value = arrAttribute
}
return copy
}
override init(dict: Dictionary<String, Any>) {
super.init(dict: dict);
value = self.valueParse()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var name: String? {
return self.dictionary.getString(forKey: "name")
}
var icon: String? {
return self.dictionary.getString(forKey: "icon")
}
var search_key: String? {
return self.dictionary.getString(forKey: "search_key")
}
var key: String? {
return self.dictionary.getString(forKey: "key")
}
var type: FilterDisplayType {
let type = self.dictionary.getString(forKey: "type")
return self.getType(string: type)
}
var value: [AttributeValueModel] = [];
func valueParse()-> [AttributeValueModel] {
// with switch type, Just set true or false
// Change ParentValue to Child
if type == .Switch {
let dict:Dictionary<String, AnyObject> = [
"option_id": "false" as AnyObject,
"option_name": self.name! as AnyObject,
"name": self.name! as AnyObject,
"icon": self.icon! as AnyObject
]
let item = AttributeValueModel(dict:dict);
return [item]
}
guard let childs = (self.dictionary["value"]) as? [Dictionary<String, AnyObject>]
else { return [] }
var output: [AttributeValueModel] = [];
for aDict in childs {
let item = AttributeValueModel(dict:aDict);
if type == .Range && item.option_id == "0" {
item.setRangeOptionID(aValue: item.option_name!)
}
output.append(item);
}
return output;
}
///get list AttributeValueModel Selected
func selectedValues() -> [AttributeValueModel] {
var output: [AttributeValueModel] = [];
for itemTemp in self.value {
if(itemTemp.selected){
if type == .Switch {
itemTemp.setSelectedOptionID()
}
output.append(itemTemp);
}
}
return output;
}
/// make a Filter Item from attributeValues Seleted
func getFilterItem() -> FilterItem? {
var itemFilter: FilterItem = FilterItem(key: self.search_key!, value: "")
itemFilter.key = self.search_key!
let output: NSMutableArray = [];
for attItem in self.selectedValues() {
if attItem.option_id != "" {
output.add(attItem.option_id!);
}
}
if(output.count == 0) {
return nil;
}
let valueString = output.componentsJoined(by: ",");
itemFilter.value = valueString;
return itemFilter
}
///get list AttributeValueModel Selected
func resetToDefault() -> [AttributeValueModel] {
var output: [AttributeValueModel] = [];
for itemTemp in self.value {
if(itemTemp.selected){
itemTemp.selected = false
if type == .Switch {
itemTemp.setSelectedOptionID()
}
if type == .Range {
itemTemp.setRangeOptionID(aValue: itemTemp.option_name!)
}
output.append(itemTemp);
}
}
return output;
}
//for UI
var wasExpanding = false
var numberOfRow:Int = 0
/************/
var attributeNameLength: Int {
var string = ""
for item in valueParse() {
string += item.option_name!
}
return string.count
}
var lenghtSizeName:Int {
var row:Int = 1
var width:CGFloat = 0
let padding:CGFloat = 8
let screen = screenWidth - 50 - 16
for item in valueParse() {
let size = ((item.option_name ?? "") as NSString).size(withAttributes: [
NSAttributedStringKey.font : UIFont.fontRegular_Big()
])
let totalWidth = size.width + padding + 16
if totalWidth <= CGFloat(32) {
width += 32
if width >= screen {
row += 1
width = 32
}
} else {
width += totalWidth
if width >= screen {
row += 1
width = totalWidth
}
}
}
return row
}
}
You can filter your response data to get only switches in an array.
responseData.filter {($0.type ?? "") == "Switch"}
And of course != would give you non switches.

parsing failed because a } isnt put in?

Setting up a fivem server and trying to get esx_policejobs to work after putting in custom job_grades. Have gotten an error and have been stuck for hours, I don't know if I'm missing something here or not?
Theres also a console line that says stack traceback to a different .lua file, so could that end up being the problem in there instead of the actual config file?
Have looked over and over the config and honestly cant find the problem
recruit = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 1500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 80 },
},
private = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
private-first-class = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 }
},
sergeant = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
lieutenant = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
captain = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
inspector = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
deputy-chief = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
assistant-chief = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
chief-of-police = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
}
Shouldnt be any config errors
Error in console:
Failed to load script config.lua.
Error loading script server/main.lua in resource esx_policejob: #esx_policejob/server/main.lua:5: attempt to index a nil value (global 'Config')
stack traceback:
#esx_policejob/server/main.lua:5: in main chunk```
The problem is with the dashes in the table key names. The manual does give a hint about the form of the table constructor:
A field of the form name = exp is equivalent to ["name"] = exp.
A name must be:
Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit and not being a reserved word. Identifiers are used to name variables, table fields, and labels.
This means you have to use the second form of the table constructor ["name"] = exp for anything that isn't valid identifier.
['chief-of-police'] = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
}
Error in your code "private" reserved word:
>>>>private = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
table must be constructed
myTable = {}
after table exists, you can define key:
myTable["some.key"] = {}
if you nead sub-key, next step will be:
myTable["some.key"]["sub.key"] = {}
myTable["some.key"]["sub.key"]["value_1"] = "123"
myTable["some.key"]["sub.key"]["value_2"] = "456"
at the end, you can use trick:
local myTables = {}
myTables["table_name"] = myTable

how to take data from data base and show the uipickerview

I am fetching the data from sqlite3, now i need to display that values in UIPicker view , Can any one help me please,
I got the response from data base like
stateName: (
{
"state_name" = Alabama;
},
{
"state_name" = Arizona;
},
{
"state_name" = Arkansas;
},
{
"state_name" = California;
},
{
"state_name" = Colorado;
},
{
"state_name" = Connecticut;
},
{
"state_name" = Delaware;
},
{
"state_name" = "District of Columbia";
},
{
"state_name" = Florida;
},
{
"state_name" = Georgia;
},
{
"state_name" = Idaho;
},
{
"state_name" = Illinois;
},
{
"state_name" = Indiana;
},
{
"state_name" = Iowa;
},
{
"state_name" = Kansas;
},
{
"state_name" = Kentucky;
},
{
"state_name" = Louisiana;
},
{
"state_name" = Maine;
},
{
"state_name" = Maryland;
},
{
"state_name" = Massachusetts;
},
{
"state_name" = Michigan;
},
{
"state_name" = Minnesota;
},
{
"state_name" = Mississippi;
},
{
"state_name" = Missouri;
},
{
"state_name" = Montana;
},
{
"state_name" = Nebraska;
},
{
"state_name" = Nevada;
},
{
"state_name" = "New Hampshire";
},
{
"state_name" = "New Jersey";
},
{
"state_name" = "New Mexico";
},
{
"state_name" = "New York";
},
{
"state_name" = "North Carolina";
},
{
"state_name" = "North Dakota";
},
{
"state_name" = Ohio;
},
{
"state_name" = Oklahoma;
},
{
"state_name" = Oregon;
},
{
"state_name" = Pennsylvania;
},
{
"state_name" = "Rhode Island";
},
{
"state_name" = "South Carolina";
},
{
"state_name" = "South Dakota";
},
{
"state_name" = Tennessee;
},
{
"state_name" = Texas;
},
{
"state_name" = Utah;
},
{
"state_name" = Vermont;
},
{
"state_name" = Virginia;
},
{
"state_name" = Washington;
},
{
"state_name" = "West Virginia";
},
{
"state_name" = Wisconsin;
},
{
"state_name" = Wyoming;
}
)
There is the delegate method of UIPickerView. #pragma mark - PickerView Delegates
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//=====================================================================================
//Customize Number of rows in Component
//=====================================================================================
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if([genreList count] != 0)
{
return [genreList count];
}
else
{
return 0;
}
}
//=====================================================================================
//Set title For Row
//=====================================================================================
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row1 forComponent:(NSInteger)component {
if([genreList count] != 0)
{
return [genreList objectAtIndex:row1];
}
else
{
return nil;
}
//return 4;
}

Resources