Aller au contenu
jjacques68

vider une propriété de type tableau dans l'API

Recommended Posts

Bonjour à tous, 

 

Petit soucis LUA / API : (il me semble qu'on avait déjà parlé de ça, un jour à une époque lointaine :) )

 

je gère le panel GARDEN depuis un QA perso.

 

Je souhaite à un moment mettre une propriété de type tableau à null (ou blanc, ou vide, ..., rien quoi :) )

 

il s'agit de cette propriété "days" ci-dessous :

 

image.png.83a9db8b8ea4d139a745ec0483158212.png

 

J'aimerai qu'elle devienne comme ceci : (je réussi à la vider quand j'utilise le Garden Panel, en décochant tous les jours de la semaine)

 

image.png.24cf2bf7d44508634797cd2303bbb9fb.png

 

Dans mon QA, je fais ceci

qui fonctionne très bien si je mets un jour dans la variable, mais pas ne fonctionne pas si le tableau reste vide. Tout le problème est là !

 

local ListeDay = {} --<----- tableau vide

MyPanel = api.get("/panels/sprinklers/11")

res = api.put("/panels/sprinklers/11", {days=ListeDay})

et il me met un erreur : 

 

Citation

Invalid schema: #\/properties\/days. Invalid keyword: type. Invalid document: #\/days","message":""

Clairement ça veut bien dire qu'il veut pas un tableau vide ?! nan je me trompe ?

 

mais alors pourquoi quand j'utilise la console de debug de mon navigateur (F12), et que j'intercepte l'action du bouton "save" après avoir décoché tous les jours de mon panel, je vois passer ça... et qui fonctionne :

 

image.png.85e63ce2b22ae911db44662024bc3c1c.png

 

image.png.ea0f8664b7560da675fc2138aa4be7e8.png

 

Il envoi bien un tableau vide aussi !!

 

qu'est ce qui ne va pas dans le code LUA ??

 

si quelqu'un a  une idée ??

 

merci d'avance !!

 

Modifié par jjacques68

Partager ce message


Lien à poster
Partager sur d’autres sites

You could try

local ListDay = json.util.InitArray({}) --<----- empty array  
MyPanel = api.get("/panels/sprinklers/11" )
res = api.put("/panels/sprinklers/11" , { days = ListDay }) 

 

The reason is that the json.encoder don't know if the Lua table {} should be encoded as an empty json key-value table "{}" or an empty json array "[]"

The json.util.InitArray() function creates an object that the json.encoder always encodes as an array.

 

Of course, the suggestion above only works if api.put uses the QAs built-in json.encode...

Modifié par jang
  • Like 1

Partager ce message


Lien à poster
Partager sur d’autres sites

hmm...

 

I don't know how to use this built-in.

 

But it gave me some ideas, but no success...

for exmple  insert and remove a value in the array,  creat a function that return the array, use json.encode/decode ...

Partager ce message


Lien à poster
Partager sur d’autres sites

Well, you have the tools, the rest is just some list manipulations... ;-) 
 

local days = { monday=0, tuesday=1, wednesday=2, thursday=3, friday=4, saturday=5, sunday=6 }

local function map(list) 
    local r ={} for _,e in ipairs(list) do assert(days[e],"Bad day:"..tostring(e)) r[e]=true end 
    return r
end

local function flatten(list) 
     local r ={} for e,_ in pairs(list) do assert(days[e],"Bad day:"..tostring(e)) r[#r+1]=e end 
     table.sort(r,function(a,b) return days[a] <= days[b] end)
     return r
end

-- self:setSprinklerDays(4,{"monday",wednesday"})
-- will set scheduled days to monday and wednesday for sprinkler schedule 4
function QuickApp:setSprinklerDays(sprinkerId,list) 
    return api.put("/panels/sprinklers/4" ,{ days = json.util.InitArray(flatten(map(list)))}) 
end

-- self:addSprinklerDays(4,{"monday",wednesday"})
-- will add monday and wednesday to currently scheduled days for sprinkler schedule 4
function QuickApp:addSprinklerDays(sprinklerId,list)
    local days = map(api.get("/panels/sprinklers/"..sprinklerId).days or {})
    for _,d in ipairs(list) do days[d]=true end
    return api.put("/panels/sprinklers/4" ,{ days = json.util.InitArray(flatten(days))}) 
end

-- self:removeSprinklerDays(4,{"monday",wednesday"})
-- will remove monday and wednesday from currently scheduled days for sprinkler schedule 4
function QuickApp:removeSprinklerDays(sprinklerId,list)
    local days = map(api.get("/panels/sprinklers/"..sprinklerId).days or {})
    for _,d in ipairs(list) do days[d]=nil end
    return api.put("/panels/sprinklers/4" ,{ days = json.util.InitArray(flatten(days))}) 
end

function QuickApp : onInit ()
    print(self:setSprinklerDays(4,{"monday","friday"}))
    print(self:addSprinklerDays(4,{"saturday"})) 
    print(self:removeSprinklerDays(4,{"monday"}))
end 

 

  • Thanks 1

Partager ce message


Lien à poster
Partager sur d’autres sites

aaaah ok !

 

I didn't no we already had this json.util !!

 

thank you very much for you exemple, I understand.

 

And the function json.util.InitArray()  works perfectly.

 

I just try with this when I declare the variable : 

local ListeDay = json.util.InitArray({})

and it's ok now.

 

But I prefer your three functions ;) 

I will use them.

 

thank you @jang

 

 

  • Like 1

Partager ce message


Lien à poster
Partager sur d’autres sites

×