keeping straight wall bed leg with shelf

gravity alone can’t accomplish this ,even a little bit of weight of shelf and the leg won’t be vertical
also the it would swing and things would fall